-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add post-fix cleanups * Standardized infra a bit
- Loading branch information
1 parent
9334294
commit dfa87d3
Showing
108 changed files
with
497 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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 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,23 @@ | ||
# Cleanups | ||
|
||
After TypeStat has applied all the fixes it can to files, there may still be some general cleanups that need to be applied. | ||
Most commonly, any remaining TypeScript type errors may need to be suppressed with `// @ts-expect-error`. | ||
|
||
Each classification of cleanups can be individually configured in your `typestat.json` file. | ||
These all default to `false` but can be enabled by being set to `true`. | ||
|
||
```json | ||
{ | ||
"cleanups": { | ||
"suppressTypeErrors": true | ||
} | ||
} | ||
``` | ||
|
||
## Cleaners | ||
|
||
### `suppressTypeErrors` | ||
|
||
Whether to add a `// @ts-expect-error` comment directive before each remaining type error. | ||
|
||
See [suppressTypeErrors/README.md](../src/cleanups/builtin/suppressTypeErrors/README.md). |
This file contains 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,26 @@ | ||
# `suppressTypeErrors` | ||
|
||
Whether to add a `// @ts-expect-error` comment directive before each remaining type error. | ||
|
||
## Use Cases | ||
|
||
* Your existing code has type errors that are too complex or context-dependent for TypeStat to clean up. | ||
|
||
## Configuration | ||
|
||
```json | ||
{ | ||
"cleanups": { | ||
"suppressTypeErrors": true | ||
} | ||
} | ||
``` | ||
|
||
## Mutations | ||
|
||
For each type error still remaining in files, a `// @ts-expect-error` comment will be added with the text of the error: | ||
|
||
```diff | ||
+ // @ts-expect-error: Type 'number' is not assignable to type 'string'. | ||
let incorrect: string = 0; | ||
``` |
This file contains 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,44 @@ | ||
import * as ts from "typescript"; | ||
|
||
import { | ||
DiagnosticWithStart, | ||
getLineForDiagnostic, | ||
isDiagnosticWithStart, | ||
stringifyDiagnosticMessageText, | ||
} from "../../../shared/diagnostics"; | ||
import { FileMutator } from "../../../shared/fileMutator"; | ||
|
||
export const suppressRemainingTypeIssues: FileMutator = (request) => { | ||
if (!request.options.cleanups.suppressTypeErrors) { | ||
return undefined; | ||
} | ||
|
||
const allDiagnostics = request.services.program.getSemanticDiagnostics(request.sourceFile).filter(isDiagnosticWithStart); | ||
if (!allDiagnostics.length) { | ||
return undefined; | ||
} | ||
|
||
const diagnosticsPerLine = new Map<number, DiagnosticWithStart[]>(); | ||
|
||
for (const diagnostic of allDiagnostics) { | ||
const line = getLineForDiagnostic(diagnostic, request.sourceFile); | ||
const existing = diagnosticsPerLine.get(line); | ||
|
||
if (existing) { | ||
existing.push(diagnostic); | ||
} else { | ||
diagnosticsPerLine.set(line, [diagnostic]); | ||
} | ||
} | ||
|
||
return Array.from(diagnosticsPerLine).map(([line, diagnostics]) => { | ||
const messages = diagnostics.map(stringifyDiagnosticMessageText).join(" "); | ||
return { | ||
insertion: `// @ts-expect-error -- TODO: ${messages}\n`, | ||
range: { | ||
begin: ts.getPositionOfLineAndCharacter(request.sourceFile, line, 0), | ||
}, | ||
type: "text-insert", | ||
}; | ||
}); | ||
}; |
This file contains 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,20 @@ | ||
import { prompt } from "enquirer"; | ||
|
||
export enum InitializationCleanups { | ||
No = "No", | ||
Yes = "Yes", | ||
} | ||
|
||
export const initializeCleanups = async () => { | ||
const { cleanups } = await prompt<{ cleanups: InitializationCleanups }>([ | ||
{ | ||
choices: [InitializationCleanups.No, InitializationCleanups.Yes], | ||
initial: 1, | ||
message: "Would you like to suppress remaining type errors with // @ts-expect-error comments?", | ||
name: "cleanups", | ||
type: "select", | ||
}, | ||
]); | ||
|
||
return cleanups; | ||
}; |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.