Skip to content
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

test: enable suppressTypeErrors on all integration tests #1549

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/cleanups/builtin/suppressTypeErrors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
DiagnosticWithStart,
getLineForDiagnostic,
isDiagnosticWithStart,
stringifyDiagnosticMessageText,
userFriendlyDiagnosticMessageText,
} from "../../../shared/diagnostics.js";
import { FileMutator } from "../../../shared/fileMutator.js";

Expand Down Expand Up @@ -33,8 +33,14 @@ export const suppressRemainingTypeIssues: FileMutator = (request) => {
}
}

const currentDir = request.services.program.getCurrentDirectory();

return Array.from(diagnosticsPerLine).map(([line, diagnostics]) => {
const messages = diagnostics.map(stringifyDiagnosticMessageText).join(" ");
const messages = diagnostics
.map((diagnostic) =>
userFriendlyDiagnosticMessageText(diagnostic, currentDir),
)
.join(" ");
return {
insertion: `// @ts-expect-error -- TODO: ${messages}\n`,
range: {
Expand Down
16 changes: 16 additions & 0 deletions src/shared/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ export const stringifyDiagnosticMessageText = (diagnostic: ts.Diagnostic) => {
? diagnostic.messageText
: diagnostic.messageText.messageText;
};

export const userFriendlyDiagnosticMessageText = (
diagnostic: ts.Diagnostic,
currentDir: string,
) => {
const diagnosticMessage = stringifyDiagnosticMessageText(diagnostic);
if (diagnostic.code === 1192 || diagnostic.code === 1259) {
// = Module_0_can_only_be_default_imported_using_the_1_flag
return diagnosticMessage.replace(
/Module '"[^"]*"' (can only be default-imported|has no default export)/,
"This module $1",
);
} else {
return diagnosticMessage.replace(currentDir, "<rootDir>");
}
};
53 changes: 33 additions & 20 deletions src/tests/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import path from "node:path";

import { fillOutRawOptions } from "../options/fillOutRawOptions.js";
import { parseRawCompilerOptions } from "../options/parseRawCompilerOptions.js";
import { RawTypeStatOptions } from "../options/types.js";
import {
PendingTypeStatOptions,
RawTypeStatOptions,
} from "../options/types.js";
import { createTypeStatProvider } from "../runtime/createTypeStatProvider.js";

export interface MutationTestResult {
Expand Down Expand Up @@ -34,7 +37,10 @@ export const runMutationTest = async (
await fs.copyFile(originalFile, actualFile);

const rawTypeStatOptions = await readFile("typestat.json");
const rawOptions = JSON.parse(rawTypeStatOptions) as RawTypeStatOptions;
const rawOptions = JSON.parse(rawTypeStatOptions) as
| RawTypeStatOptions
| RawTypeStatOptions[];
const rawOptionsList = Array.isArray(rawOptions) ? rawOptions : [rawOptions];

const projectPath = path.join(dirPath, "tsconfig.json");

Expand All @@ -48,31 +54,38 @@ export const runMutationTest = async (
stdout: () => {},
};

const pendingOptions = fillOutRawOptions({
compilerOptions,
cwd: dirPath,
output,
projectPath,
rawOptions,
});
const pendingOptionsList: PendingTypeStatOptions[] = [];

const provider = createTypeStatProvider({
...pendingOptions,
fileNames: [actualFile],
});
for (const mutationOption of rawOptionsList) {
const pendingOptions = fillOutRawOptions({
compilerOptions,
cwd: dirPath,
output,
projectPath,
rawOptions: mutationOption,
});

await runMutations({
mutationsProvider: provider,
});
pendingOptionsList.push(pendingOptions);

const provider = createTypeStatProvider({
...pendingOptions,
fileNames: [actualFile],
});

await runMutations({
mutationsProvider: provider,
});
}

const actualContent = await readFile(actualFileName);
const expectFileName = `expected.ts${fileNameSuffix}`;
const expectedFilePath = path.join(dirPath, expectFileName);

const optionsSnapshot = JSON.stringify(pendingOptions, null, 2).replaceAll(
dirPath,
"<rootDir>",
);
const optionsSnapshot = JSON.stringify(
pendingOptionsList,
null,
2,
).replaceAll(dirPath, "<rootDir>");

return { actualContent, expectedFilePath, options: optionsSnapshot };
};
234 changes: 160 additions & 74 deletions test/__snapshots__/cleanups.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,81 +1,167 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Cleanups > suppressTypeErrors > options 1`] = `
"{
"cleanups": {
"suppressTypeErrors": true
},
"compilerOptions": {
exports[`Cleanups > non-TypeErrors > options 1`] = `
"[
{
"cleanups": {
"suppressTypeErrors": true
},
"compilerOptions": {
"strictNullChecks": true
"compilerOptions": {
"esModuleInterop": true,
"strictNullChecks": true,
"resolveJsonModule": true
},
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"fixImportExtensions",
null
],
[
"fixIncompleteTypes",
null
],
[
"fixMissingProperties",
null
],
[
"fixNoImplicitAny",
null
],
[
"fixNoImplicitThis",
null
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"fixImportExtensions",
null
],
[
"fixIncompleteTypes",
null
],
[
"fixMissingProperties",
null
],
[
"fixNoImplicitAny",
null
],
[
"fixNoImplicitThis",
null
],
[
"fixNoInferableTypes",
null
],
[
"fixStrictNonNullAssertions",
null
]
],
[
"fixNoInferableTypes",
null
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}
]"
`;

exports[`Cleanups > suppressTypeErrors > options 1`] = `
"[
{
"cleanups": {
"suppressTypeErrors": true
},
"compilerOptions": {
"compilerOptions": {
"strictNullChecks": true
},
"files": [
"actual.ts"
],
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"files": {
"above": "",
"below": "",
"renameExtensions": false
},
"filters": [],
"fixes": {
"importExtensions": false,
"incompleteTypes": false,
"missingProperties": false,
"noImplicitAny": false,
"noImplicitThis": false,
"noInferableTypes": false,
"strictNonNullAssertions": false
},
"hints": {
"react": {
"propTypes": "whenRequired",
"propTypesOptionality": "asWritten"
}
},
"mutators": [
[
"fixImportExtensions",
null
],
[
"fixIncompleteTypes",
null
],
[
"fixMissingProperties",
null
],
[
"fixNoImplicitAny",
null
],
[
"fixNoImplicitThis",
null
],
[
"fixNoInferableTypes",
null
],
[
"fixStrictNonNullAssertions",
null
]
],
[
"fixStrictNonNullAssertions",
null
]
],
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}"
"output": {},
"package": {
"directory": "<rootDir>",
"file": "<rootDir>/package.json"
},
"postProcess": {
"shell": []
},
"projectPath": "<rootDir>/tsconfig.json",
"types": {}
}
]"
`;
Loading
Loading