-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathdisableJsDiagnostics.ts
81 lines (74 loc) · 3.18 KB
/
disableJsDiagnostics.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
codeFixAll,
createCodeFixAction,
createCodeFixActionWithoutFixAll,
createFileTextChanges,
registerCodeFix,
} from "../_namespaces/ts.codefix.js";
import {
CodeFixAction,
createTextChange,
createTextSpan,
createTextSpanFromBounds,
DiagnosticCategory,
Diagnostics,
getLineAndCharacterOfPosition,
getNewLineOrDefaultFromHost,
isCheckJsEnabledForFile,
isInJSFile,
mapDefined,
SourceFile,
textChanges,
tryAddToSet,
} from "../_namespaces/ts.js";
const fixName = "disableJsDiagnostics";
const fixId = "disableJsDiagnostics";
const errorCodes = mapDefined(Object.keys(Diagnostics) as readonly (keyof typeof Diagnostics)[], key => {
const diag = Diagnostics[key];
return diag.category === DiagnosticCategory.Error ? diag.code : undefined;
});
registerCodeFix({
errorCodes,
getCodeActions: function getCodeActionsToDisableJsDiagnostics(context) {
const { sourceFile, program, span, host, formatContext } = context;
if (!isInJSFile(sourceFile) || !isCheckJsEnabledForFile(sourceFile, program.getCompilerOptions())) {
return undefined;
}
const newLineCharacter = sourceFile.checkJsDirective ? "" : getNewLineOrDefaultFromHost(host, formatContext.options);
const fixes: CodeFixAction[] = [
// fixId unnecessary because adding `// @ts-nocheck` even once will ignore every error in the file.
createCodeFixActionWithoutFixAll(
fixName,
[createFileTextChanges(sourceFile.fileName, [
createTextChange(
sourceFile.checkJsDirective
? createTextSpanFromBounds(sourceFile.checkJsDirective.pos, sourceFile.checkJsDirective.end)
: createTextSpan(0, 0),
`// @ts-nocheck${newLineCharacter}`,
),
])],
Diagnostics.Disable_checking_for_this_file,
),
];
if (textChanges.isValidLocationToAddComment(sourceFile, span.start)) {
fixes.unshift(createCodeFixAction(fixName, textChanges.ChangeTracker.with(context, t => makeChange(t, sourceFile, span.start)), Diagnostics.Ignore_this_error_message, fixId, Diagnostics.Add_ts_ignore_to_all_error_messages));
}
return fixes;
},
fixIds: [fixId],
getAllCodeActions: context => {
const seenLines = new Set<number>();
return codeFixAll(context, errorCodes, (changes, diag) => {
if (textChanges.isValidLocationToAddComment(diag.file, diag.start)) {
makeChange(changes, diag.file, diag.start, seenLines);
}
});
},
});
function makeChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, seenLines?: Set<number>) {
const { line: lineNumber } = getLineAndCharacterOfPosition(sourceFile, position);
// Only need to add `// @ts-ignore` for a line once.
if (!seenLines || tryAddToSet(seenLines, lineNumber)) {
changes.insertCommentBeforeLine(sourceFile, lineNumber, position, " @ts-ignore");
}
}