-
-
Notifications
You must be signed in to change notification settings - Fork 308
chore: suppress existing type errors so lint:tsc can run in CI #10251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cryptodev-2s
wants to merge
8
commits into
main
Choose a base branch
from
tsc-suppressions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,330
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a7a76d
chore: suppress existing type errors so lint:tsc can run in CI
cryptodev-2s 90f8538
Merge remote-tracking branch 'origin/main' into tsc-suppressions
cryptodev-2s a2ba1e0
chore: refresh type error suppressions after merging main
cryptodev-2s f26338a
Merge remote-tracking branch 'origin/main' into tsc-suppressions
cryptodev-2s 21d2726
chore: refresh type error suppressions after merging main
cryptodev-2s b396a87
refactor: simplify the suppressions check and fail loudly when tsc br…
cryptodev-2s 1ec7429
chore: format lint-tsc test
cryptodev-2s 6247710
fix: fail when tsc reports a diagnostic that belongs to no file
cryptodev-2s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| import { jest } from '@jest/globals'; | ||
|
|
||
| // `jest.mock` does not apply to ES modules, so the module registry is stubbed | ||
| // with `jest.unstable_mockModule` and the modules under test are imported | ||
| // dynamically afterwards. | ||
| jest.unstable_mockModule('execa', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn(), | ||
| })); | ||
|
|
||
| jest.unstable_mockModule('./tsc-suppressions.js', () => ({ | ||
| parseTscOutput: jest.fn(), | ||
| findFilelessDiagnostics: jest.fn(), | ||
| buildSuppressions: jest.fn(), | ||
| compareErrorsToSuppressions: jest.fn(), | ||
| readSuppressions: jest.fn(), | ||
| writeSuppressions: jest.fn(), | ||
| printReport: jest.fn(), | ||
| })); | ||
|
|
||
| const { default: execa } = await import('execa'); | ||
| const tscSuppressions = await import('./tsc-suppressions.js'); | ||
| const { lintTsc } = await import('./lint-tsc.js'); | ||
|
|
||
| const ERROR = { filePath: 'a.ts', code: 'TS2322', message: 'Nope.' }; | ||
|
|
||
| const PASSING_REPORT = { | ||
| unsuppressedErrors: [], | ||
| staleSuppressions: [], | ||
| didPass: true, | ||
| }; | ||
|
|
||
| /** | ||
| * Stubs a `tsc` run which produces the given output and one parsed error. | ||
| * | ||
| * @param output - The output that `tsc` should produce. | ||
| * @param exitCode - The code that `tsc` should exit with. It exits non-zero | ||
| * whenever it reports errors, so that is the default. | ||
| */ | ||
| function mockTscRun(output: string, exitCode = 1): void { | ||
| jest.mocked(execa).mockResolvedValue({ all: output, exitCode } as never); | ||
| jest.mocked(tscSuppressions.parseTscOutput).mockReturnValue([ERROR]); | ||
| jest.mocked(tscSuppressions.findFilelessDiagnostics).mockReturnValue([]); | ||
| } | ||
|
|
||
| describe('lintTsc', () => { | ||
| let originalProcess: typeof globalThis.process; | ||
|
|
||
| beforeEach(() => { | ||
| originalProcess = globalThis.process; | ||
| // The exit code is reset because it is global state that another test file | ||
| // may have set. | ||
| globalThis.process = { ...globalThis.process, exitCode: undefined }; | ||
| jest.spyOn(console, 'log').mockReturnValue(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| globalThis.process = originalProcess; | ||
| }); | ||
|
|
||
| it('typechecks every package in the repo, capturing errors rather than throwing', async () => { | ||
| mockTscRun(''); | ||
| jest | ||
| .mocked(tscSuppressions.compareErrorsToSuppressions) | ||
| .mockReturnValue(PASSING_REPORT); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(execa).toHaveBeenCalledWith( | ||
| 'tsc', | ||
| ['--build', 'tsconfig.lint.json', '--pretty', 'false'], | ||
| expect.objectContaining({ reject: false, all: true }), | ||
| ); | ||
| }); | ||
|
|
||
| it('checks the parsed errors against the suppressions file', async () => { | ||
| mockTscRun('a.ts(1,1): error TS2322: Nope.'); | ||
| const suppressions = { 'a.ts': { TS2322: { count: 1 } } }; | ||
| jest | ||
| .mocked(tscSuppressions.readSuppressions) | ||
| .mockResolvedValue(suppressions); | ||
| jest | ||
| .mocked(tscSuppressions.compareErrorsToSuppressions) | ||
| .mockReturnValue(PASSING_REPORT); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(tscSuppressions.parseTscOutput).toHaveBeenCalledWith( | ||
| 'a.ts(1,1): error TS2322: Nope.', | ||
| ); | ||
| expect(tscSuppressions.compareErrorsToSuppressions).toHaveBeenCalledWith({ | ||
| errors: [ERROR], | ||
| suppressions, | ||
| }); | ||
| }); | ||
|
|
||
| it('prints the report and leaves the exit code alone when the check passes', async () => { | ||
| mockTscRun(''); | ||
| jest | ||
| .mocked(tscSuppressions.compareErrorsToSuppressions) | ||
| .mockReturnValue(PASSING_REPORT); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(tscSuppressions.printReport).toHaveBeenCalledWith(PASSING_REPORT); | ||
| expect(process.exitCode).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('exits with a non-zero code when the check fails', async () => { | ||
| mockTscRun(''); | ||
| jest.mocked(tscSuppressions.compareErrorsToSuppressions).mockReturnValue({ | ||
| unsuppressedErrors: [ | ||
| { | ||
| filePath: 'a.ts', | ||
| code: 'TS2322', | ||
| count: 1, | ||
| suppressedCount: 0, | ||
| messages: ['Nope.'], | ||
| }, | ||
| ], | ||
| staleSuppressions: [], | ||
| didPass: false, | ||
| }); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(process.exitCode).toBe(1); | ||
| }); | ||
|
|
||
| it('treats a successful run that produces no output as having no errors', async () => { | ||
| jest | ||
| .mocked(execa) | ||
| .mockResolvedValue({ all: undefined, exitCode: 0 } as never); | ||
| jest.mocked(tscSuppressions.parseTscOutput).mockReturnValue([]); | ||
| jest.mocked(tscSuppressions.findFilelessDiagnostics).mockReturnValue([]); | ||
| jest | ||
| .mocked(tscSuppressions.compareErrorsToSuppressions) | ||
| .mockReturnValue(PASSING_REPORT); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(tscSuppressions.parseTscOutput).toHaveBeenCalledWith(''); | ||
| }); | ||
|
|
||
| it('throws when tsc fails without reporting any type errors', async () => { | ||
| jest.mocked(execa).mockResolvedValue({ | ||
| all: 'boom', | ||
| exitCode: 1, | ||
| } as never); | ||
| jest.mocked(tscSuppressions.parseTscOutput).mockReturnValue([]); | ||
| jest.mocked(tscSuppressions.findFilelessDiagnostics).mockReturnValue([]); | ||
|
|
||
| await expect(lintTsc([])).rejects.toThrow( | ||
| '`tsc` failed for a reason other than the type errors it reported.', | ||
| ); | ||
| expect(tscSuppressions.compareErrorsToSuppressions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not throw when tsc exits non-zero but reports type errors, as those may be suppressed', async () => { | ||
| mockTscRun('a.ts(1,1): error TS2322: Nope.'); | ||
| jest | ||
| .mocked(tscSuppressions.compareErrorsToSuppressions) | ||
| .mockReturnValue(PASSING_REPORT); | ||
|
|
||
| await lintTsc([]); | ||
|
|
||
| expect(tscSuppressions.printReport).toHaveBeenCalledWith(PASSING_REPORT); | ||
| }); | ||
|
|
||
| it('throws when tsc reports a diagnostic that belongs to no file, even though type errors were reported too', async () => { | ||
| mockTscRun('a.ts(1,1): error TS2322: Nope.'); | ||
| jest | ||
| .mocked(tscSuppressions.findFilelessDiagnostics) | ||
| .mockReturnValue(["error TS6053: File 'nope.json' not found."]); | ||
|
|
||
| await expect(lintTsc([])).rejects.toThrow( | ||
| '`tsc` failed for a reason other than the type errors it reported.', | ||
| ); | ||
| expect(tscSuppressions.compareErrorsToSuppressions).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('rewrites the suppressions file when given --update, without failing', async () => { | ||
| mockTscRun(''); | ||
| const suppressions = { 'a.ts': { TS2322: { count: 1 } } }; | ||
| jest | ||
| .mocked(tscSuppressions.buildSuppressions) | ||
| .mockReturnValue(suppressions); | ||
|
|
||
| await lintTsc(['--update']); | ||
|
|
||
| expect(tscSuppressions.buildSuppressions).toHaveBeenCalledWith([ERROR]); | ||
| expect(tscSuppressions.writeSuppressions).toHaveBeenCalledWith( | ||
| expect.objectContaining({ suppressions }), | ||
| ); | ||
| expect(tscSuppressions.compareErrorsToSuppressions).not.toHaveBeenCalled(); | ||
| expect(process.exitCode).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import execa from 'execa'; | ||
| import path from 'path'; | ||
|
|
||
| import { | ||
| buildSuppressions, | ||
| compareErrorsToSuppressions, | ||
| findFilelessDiagnostics, | ||
| parseTscOutput, | ||
| printReport, | ||
| readSuppressions, | ||
| writeSuppressions, | ||
| } from './tsc-suppressions.js'; | ||
|
|
||
| const REPO_ROOT = path.join(import.meta.dirname, '..', '..'); | ||
|
|
||
| const SUPPRESSIONS_FILE_NAME = 'tsc-suppressions.json'; | ||
|
|
||
| /** | ||
| * Typechecks every package in the repo and compares the type errors it finds | ||
| * against `tsc-suppressions.json`, failing if any error is not suppressed there | ||
| * or if any suppression no longer covers an error. This keeps packages that are | ||
| * free of type errors from regressing while the existing errors are worked | ||
| * through. | ||
| * | ||
| * Passing `--update` rewrites the suppressions file from the errors that | ||
| * currently exist rather than checking against it. | ||
| * | ||
| * @param argv - The arguments passed to this script. | ||
| */ | ||
| export async function lintTsc(argv: readonly string[]): Promise<void> { | ||
| const suppressionsFilePath = path.join(REPO_ROOT, SUPPRESSIONS_FILE_NAME); | ||
|
|
||
| const { all, exitCode } = await execa( | ||
| 'tsc', | ||
| ['--build', 'tsconfig.lint.json', '--pretty', 'false'], | ||
| { cwd: REPO_ROOT, reject: false, all: true, preferLocal: true }, | ||
| ); | ||
| const output = all ?? ''; | ||
| const errors = parseTscOutput(output); | ||
|
|
||
| // `tsc` exits non-zero whenever it reports type errors, which are expected | ||
| // here and may well be suppressed, so the exit code alone says little. A run | ||
| // is only genuinely broken if it reported a diagnostic that belongs to no | ||
| // file, or if it failed without reporting any type errors at all. Neither can | ||
| // be suppressed, and neither may be mistaken for a clean run. | ||
| const filelessDiagnostics = findFilelessDiagnostics(output); | ||
| if ( | ||
| filelessDiagnostics.length > 0 || | ||
| (exitCode !== 0 && errors.length === 0) | ||
| ) { | ||
| console.log( | ||
| filelessDiagnostics.length > 0 ? filelessDiagnostics.join('\n') : output, | ||
| ); | ||
| throw new Error( | ||
| '`tsc` failed for a reason other than the type errors it reported.', | ||
| ); | ||
| } | ||
|
|
||
| if (argv.includes('--update')) { | ||
| const suppressions = buildSuppressions(errors); | ||
| await writeSuppressions({ | ||
| filePath: suppressionsFilePath, | ||
| suppressions, | ||
| }); | ||
| console.log( | ||
| `✅ Updated ${SUPPRESSIONS_FILE_NAME}: now suppressing ${errors.length} type error(s) across ${Object.keys(suppressions).length} file(s).`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const report = compareErrorsToSuppressions({ | ||
| errors, | ||
| suppressions: await readSuppressions(suppressionsFilePath), | ||
| }); | ||
| printReport(report); | ||
|
|
||
| if (!report.didPass) { | ||
| process.exitCode = 1; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.