Skip to content

Commit

Permalink
test: enable suppressTypeErrors on all integration tests (#1549)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1548
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/TypeStat/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/TypeStat/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Enables `suppressTypeErrors` on all integration tests to track potential
type errors on generated code.

Also, makes sure that we do not add absolute module path to the ts
ignore comment as it would be different when run on different machines.
  • Loading branch information
rubiesonthesky authored Jan 3, 2025
1 parent a669242 commit aa1d480
Show file tree
Hide file tree
Showing 98 changed files with 3,940 additions and 3,445 deletions.
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

0 comments on commit aa1d480

Please sign in to comment.