Opened 2 weeks ago
Last modified 5 hours ago
#64662 reopened task (blessed)
Add TypeScript static analysis for core JavaScript files
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 7.0 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Build/Test Tools | Keywords: | has-patch changes-requested |
| Focuses: | javascript | Cc: |
Description (last modified by )
For #64661, a PR is proposed which adds TypeScript checking for the scripts related to the CodeMirror code editor integrations. With that in place, core should start expanding the scope of which JS files are checked to ensure code quality and fix subtle bugs. The same is being done for PHP with PHPStan in #61175.
Note that TypeScript is already being used in Gutenberg. See https://make.wordpress.org/core/2021/03/18/proposal-native-typescript-support-in-gutenberg/
To do:
- Commit https://github.com/WordPress/wordpress-develop/pull/10900
- Add
typecheck:jsto theprecommitGrunt task. - Add GHA workflow which runs
typecheck:js
Change History (15)
This ticket was mentioned in PR #11131 on WordPress/wordpress-develop by @westonruter.
31 hours ago
#6
- Keywords has-patch added
This introduces a new GitHub Action workflow for JavaScript type checking, mirroring the implementation for PHPStan. It also adds a typecheck:js Grunt task and includes it in the precommit:js task list.
Trac ticket: https://core.trac.wordpress.org/ticket/64662
## Use of AI Tools
Initial commit authored by Gemini CLI with prompt:
Take a look at https://core.trac.wordpress.org/ticket/64662 and implement. You can see that
typecheck:jsnpm script was added in b6c1bb776b360ef64a00468e6df72792511866d3. Similarly, you can see in 8a82e67cf65766fcb8a11e3fe5c6e2f48083fcdb howtypecheck:phpwas introduced for PHPStan and a GitHub action was added to automatically run. What was done for PHPStan, implement similarly for TypeScript.
@westonruter commented on PR #11131:
31 hours ago
#7
Difference between reusable-phpstan-static-analysis.yml and reusable-javascript-type-checking.yml:
-
.github/workflows/
old new 1 1 ## 2 # A reusable workflow that runs PHP Static Analysis tests.2 # A reusable workflow that runs . 3 3 ## 4 name: PHP Static Analysis4 name: 5 5 6 6 on: 7 7 workflow_call: 8 inputs:9 php-version:10 description: 'The PHP version to use.'11 required: false12 type: 'string'13 default: 'latest'14 8 15 9 # Disable permissions for all available scopes by default. 16 10 # Any needed permissions should be configured at the job level. 17 11 permissions: {} 18 12 19 13 jobs: 20 # Runs PHP static analysis tests.14 # Runs . 21 15 # 22 16 # Violations are reported inline with annotations. 23 17 # 24 18 # Performs the following steps: 25 19 # - Checks out the repository. 26 # - Sets up PHP.20 # - Sets up . 27 21 # - Logs debug information. 28 # - Installs Composer dependencies. 29 # - Configures caching for PHP static analysis scans. 30 # - Make Composer packages available globally. 31 # - Runs PHPStan static analysis (with Pull Request annotations). 32 # - Saves the PHPStan result cache. 22 # - Installs npm dependencies. 23 # - Configures caching for TypeScript build info. 24 # - Runs JavaScript type checking. 25 # - Saves the TypeScript build info. 33 26 # - Ensures version-controlled files are not modified or deleted. 34 phpstan:35 name: Run PHP static analysis27 : 28 name: Run 36 29 runs-on: ubuntu-24.04 37 30 permissions: 38 31 contents: read … … 51 44 node-version-file: '.nvmrc' 52 45 cache: npm 53 46 54 - name: Set up PHP 55 uses: shivammathur/setup-php@20529878ed81ef8e78ddf08b480401e6101a850f # v2.35.3 56 with: 57 php-version: ${{ inputs.php-version }} 58 coverage: none 59 tools: cs2pr 60 61 # This date is used to ensure that the Composer cache is cleared at least once every week. 62 # http://man7.org/linux/man-pages/man1/date.1.html 63 - name: "Get last Monday's date" 64 id: get-date 65 run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> "$GITHUB_OUTPUT" 66 67 - name: General debug information 47 - name: Log debug information 68 48 run: | 69 49 npm --version 70 50 node --version 71 composer --version72 51 73 # Since Composer dependencies are installed using `composer update` and no lock file is in version control,74 # passing a custom cache suffix ensures that the cache is flushed at least once per week.75 - name: Install Composer dependencies76 uses: ramsey/composer-install@3cf229dc2919194e9e36783941438d17239e8520 # v3.1.177 with:78 custom-cache-suffix: ${{ steps.get-date.outputs.date }}79 80 - name: Make Composer packages available globally81 run: echo "${PWD}/vendor/bin" >> "$GITHUB_PATH"82 83 52 - name: Install npm dependencies 84 53 run: npm ci --ignore-scripts 85 54 86 - name: Build WordPress 87 run: npm run build:dev 88 89 - name: Cache PHP Static Analysis scan cache 55 - name: Cache TypeScript build info 90 56 uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 91 57 with: 92 path: .cache # This is defined in the base.neon file. 93 key: "phpstan-result-cache-${{ github.run_id }}" 58 path: | 59 *.tsbuildinfo 60 key: "ts-build-info-${{ github.run_id }}" 94 61 restore-keys: | 95 phpstan-result-cache-62 - 96 63 97 - name: Run PHP static analysis tests 98 id: phpstan 99 run: composer run phpstan -- -vvv --error-format=checkstyle | cs2pr --errors-as-warnings --graceful-warnings 64 - name: Run JavaScript type checking 65 run: npm run typecheck:js 100 66 101 67 - name: "Save result cache" 102 68 uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 103 69 if: ${{ !cancelled() }} 104 70 with: 105 path: .cache 106 key: "phpstan-result-cache-${{ github.run_id }}" 71 path: | 72 *.tsbuildinfo 73 key: "ts-build-info-${{ github.run_id }}" 107 74 108 75 - name: Ensure version-controlled files are not modified or deleted 109 76 run: git diff --exit-code
@westonruter commented on PR #11131:
31 hours ago
#8
Difference between .github/workflows/phpstan-static-analysis.yml and .github/workflows/javascript-type-checking.yml:
-
.github/workflows/
old new 1 name: PHPStan Static Analysis1 name: 2 2 3 3 on: 4 # PHPStan testing was introduced in 7.0.0.4 # ing was introduced in 7.0.0. 5 5 push: 6 6 branches: 7 7 - trunk … … 14 14 - trunk 15 15 - '[7-9].[0-9]' 16 16 paths: 17 # This workflow only scans PHP files. 18 - '**.php' 19 # These files configure Composer. Changes could affect the outcome. 20 - 'composer.*' 21 # These files configure PHPStan. Changes could affect the outcome. 22 - 'phpstan.neon.dist' 23 - 'tests/phpstan/base.neon' 24 - 'tests/phpstan/baseline.php' 17 # This workflow only scans JavaScript files. 18 - '**.js' 19 # These files configure npm. Changes could affect the outcome. 20 - 'package*.json' 21 - '.nvmrc' 22 # This file configures TypeScript. Changes could affect the outcome. 23 - 'tsconfig.json' 24 # This directory contains TypeScript definitions. Changes could affect the outcome. 25 - 'typings/**' 25 26 # Confirm any changes to relevant workflow files. 26 - '.github/workflows/ phpstan-static-analysis.yml'27 - '.github/workflows/reusable- phpstan-static-analysis.yml'27 - '.github/workflows/.yml' 28 - '.github/workflows/reusable-.yml' 28 29 workflow_dispatch: 29 30 30 31 # Cancels all previous workflow runs for pull requests that have not completed. … … 39 40 permissions: {} 40 41 41 42 jobs: 42 # Runs PHPStan Static Analysis.43 phpstan:44 name: PHP static analysis45 uses: ./.github/workflows/reusable- phpstan-static-analysis.yml43 # Runs . 44 : 45 name: 46 uses: ./.github/workflows/reusable-.yml 46 47 permissions: 47 48 contents: read 48 49 if: ${{ github.repository == 'WordPress/wordpress-develop' || ( github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' ) }} … … 53 54 permissions: 54 55 actions: read 55 56 contents: read 56 needs: [ phpstan]57 needs: [ ] 57 58 if: ${{ github.repository == 'WordPress/wordpress-develop' && github.event_name != 'pull_request' && always() }} 58 59 with: 59 60 calling_status: ${{ contains( needs.*.result, 'cancelled' ) && 'cancelled' || contains( needs.*.result, 'failure' ) && 'failure' || 'success' }} … … 81 82 82 83 steps: 83 84 - name: Dispatch workflow run 84 uses: actions/github-script@ 60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.185 uses: actions/github-script@ 85 86 with: 86 87 retries: 2 87 88 retry-exempt-status-codes: 418
@westonruter commented on PR #11131:
31 hours ago
#9
When a TypeScript error occurs (e.g. d2ad278), the job fails as expected:
And the errors are annotated in the PR:
This is in contrast with PHPStan's analysis which does _not_ fail its job when there are errors, but instead the errors are annotated as warnings.
I think this is fine given that TypeScript checking is being incrementally added to files on an opt-in basis. (Personally I would have wanted PHPStan errors to also cause jobs to fail with error annotations.)
@westonruter commented on PR #11131:
31 hours ago
#11
#13
@
7 hours ago
- Keywords changes-requested added
- Resolution fixed deleted
- Status changed from closed to reopened
Thanks for this, everyone!
I have a few small points of feedback for a follow up commit. Can the reusable workflow file be renamed to include a -v1 suffix? Initially only the PHPUnit reusable workflow contained the -v1 suffix, but all new ones introduced since then have included it.
I know it's inconsistent, but I would like to update all the others eventually. It's just a pain to backport to every single branch calling the file. I do plan to try and tackle that the next time a given workflow requires a new -v2 file.
It just makes it more apparent which one is the most recent. It's reasonable to assume that the file without any suffix is the current or active version and all with a suffix are old.
( github.event_name == 'pull_request' && github.actor != 'dependabot[bot]' )
Can we remove the dependabot check here? The bot was deactivated in [61049] because it stopped working when GitHub switched over to their new runner architecture, and all bot specific checks were removed in [61050].
If we ever decide to try and get that working again, we can re-add this check with the others.
#14
@
5 hours ago
@desrosj Would this also apply to the new PHPStan reusable workflow .github/workflows/reusable-phpstan-static-analysis.yml added in #61175?
This ticket was mentioned in PR #11163 on WordPress/wordpress-develop by @westonruter.
5 hours ago
#15
Trac ticket: https://core.trac.wordpress.org/ticket/64662
- Add -v1 suffix to reusable workflows for JavaScript type checking and PHPStan analysis: This follows the established convention of including a version suffix for reusable workflow files, ensuring consistency with other workflows like PHPUnit.
- Remove Dependabot exclusion from type checking workflows: Since Dependabot was deactivated in b60c06c67e4a5a2b1351cb8bb54ed4cdd2e8e49e and other bot-specific checks were removed in 26230c33261cf559907a90547b4827ea66ff9f30, this exclusion is no longer necessary.
## Use of AI Tools
Feedback from @desrosj fed into Gemini CLI to implement.
PHPStan static analysis (exposed via
npm run typecheck:phpandcomposer phpstan) has landed in r61699.