Skip to content

Commit 706acdf

Browse files
committed
Add quick fix to disable error checking in a .js file
1 parent fe7719f commit 706acdf

14 files changed

+218
-10
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,16 @@
33553355
"category": "Message",
33563356
"code": 90017
33573357
},
3358+
"Disable checking for this file.": {
3359+
"category": "Message",
3360+
"code": 90018
3361+
},
3362+
"Suppress this error message.": {
3363+
"category": "Message",
3364+
"code": 90019
3365+
},
3366+
3367+
33583368
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
33593369
"category": "Error",
33603370
"code": 8017
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* @internal */
2+
namespace ts.codefix {
3+
registerCodeFix({
4+
errorCodes: getApplicableDiagnosticCodes(),
5+
getCodeActions: getDisableJsDiagnosticsCodeActions
6+
});
7+
8+
function getApplicableDiagnosticCodes(): number[] {
9+
const allDiagnostcs = <MapLike<DiagnosticMessage>>Diagnostics;
10+
return Object.keys(allDiagnostcs)
11+
.filter(d => allDiagnostcs[d] && allDiagnostcs[d].category === DiagnosticCategory.Error)
12+
.map(d => allDiagnostcs[d].code);
13+
}
14+
15+
function shouldCheckJsFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
16+
return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs;
17+
}
18+
19+
function getSuppressCommentLocationForLocation(sourceFile: SourceFile, position: number, newLineCharacter: string) {
20+
let { line } = getLineAndCharacterOfPosition(sourceFile, position);
21+
const lineStartPosition = getStartPositionOfLine(line, sourceFile);
22+
const startPosition = getFirstNonSpaceCharacterPosition(sourceFile.text, lineStartPosition);
23+
if (!isInComment(sourceFile, startPosition) && !isInString(sourceFile, startPosition) && !isInTemplateString(sourceFile, startPosition)) {
24+
const token = getTouchingToken(sourceFile, startPosition);
25+
const tokenLeadingCommnets = getLeadingCommentRangesOfNode(token, sourceFile)
26+
if (!tokenLeadingCommnets || !tokenLeadingCommnets.length || tokenLeadingCommnets[0].pos >= startPosition) {
27+
return {
28+
span: { start: startPosition, length: 0 },
29+
newText: `// @ts-suppress${newLineCharacter}`
30+
};
31+
}
32+
}
33+
return {
34+
span: { start: position, length: 0 },
35+
newText: `${position === startPosition ? "" : newLineCharacter}// @ts-suppress${newLineCharacter}`
36+
};
37+
}
38+
39+
function getDisableJsDiagnosticsCodeActions(context: CodeFixContext): CodeAction[] | undefined {
40+
const { sourceFile, program, newLineCharacter, span } = context;
41+
42+
if (!isInJavaScriptFile(sourceFile) || !shouldCheckJsFile(sourceFile, program.getCompilerOptions())) {
43+
return undefined;
44+
}
45+
46+
return [{
47+
description: getLocaleSpecificMessage(Diagnostics.Suppress_this_error_message),
48+
changes: [{
49+
fileName: sourceFile.fileName,
50+
textChanges: [getSuppressCommentLocationForLocation(sourceFile, span.start, newLineCharacter)]
51+
}]
52+
},
53+
{
54+
description: getLocaleSpecificMessage(Diagnostics.Disable_checking_for_this_file),
55+
changes: [{
56+
fileName: sourceFile.fileName,
57+
textChanges: [{
58+
span: {
59+
start: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.pos : 0,
60+
length: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.end - sourceFile.checkJsDirective.pos : 0
61+
},
62+
newText: `// @ts-nocheck${newLineCharacter}`
63+
}]
64+
}]
65+
}];
66+
}
67+
}

src/services/codefixes/fixes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
/// <reference path="fixForgottenThisPropertyAccess.ts" />
88
/// <reference path='unusedIdentifierFixes.ts' />
99
/// <reference path='importFixes.ts' />
10+
/// <reference path='disableJsDiagnostics.ts' />
1011
/// <reference path='helpers.ts' />

src/services/codefixes/unusedIdentifierFixes.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,10 @@ namespace ts.codefix {
105105
else {
106106
// import |d,| * as ns from './file'
107107
const start = importClause.name.getStart();
108-
let end = findFirstNonSpaceCharPosStarting(importClause.name.end);
108+
const text = sourceFile.text;
109+
let end = getFirstNonSpaceCharacterPosition(text, importClause.name.end);
109110
if (sourceFile.text.charCodeAt(end) === CharacterCodes.comma) {
110-
end = findFirstNonSpaceCharPosStarting(end + 1);
111+
end = getFirstNonSpaceCharacterPosition(text, end + 1);
111112
}
112113

113114
return createCodeFix("", start, end - start);
@@ -166,13 +167,6 @@ namespace ts.codefix {
166167
return createCodeFix("", start, end - start);
167168
}
168169

169-
function findFirstNonSpaceCharPosStarting(start: number) {
170-
while (isWhiteSpace(sourceFile.text.charCodeAt(start))) {
171-
start += 1;
172-
}
173-
return start;
174-
}
175-
176170
function createCodeFix(newText: string, start: number, length: number): CodeAction[] {
177171
return [{
178172
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Remove_declaration_for_Colon_0), { 0: token.getText() }),

src/services/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"codefixes/fixes.ts",
9191
"codefixes/helpers.ts",
9292
"codefixes/importFixes.ts",
93-
"codefixes/unusedIdentifierFixes.ts"
93+
"codefixes/unusedIdentifierFixes.ts",
94+
"codefixes/disableJsDiagnostics.ts"
9495
]
9596
}

src/services/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,4 +1388,11 @@ namespace ts {
13881388
// First token is the open curly, this is where we want to put the 'super' call.
13891389
return constructor.body.getFirstToken(sourceFile).getEnd();
13901390
}
1391+
1392+
export function getFirstNonSpaceCharacterPosition(text: string, position: number) {
1393+
while (isWhiteSpace(text.charCodeAt(position))) {
1394+
position += 1;
1395+
}
1396+
return position;
1397+
}
13911398
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowjs: true
4+
// @noEmit: true
5+
6+
// @Filename: a.js
7+
////[|// @ts-check|]
8+
////var x = "";
9+
////x = 1;
10+
11+
verify.rangeAfterCodeFix("// @ts-nocheck", /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowjs: true
4+
// @noEmit: true
5+
// @checkJs: true
6+
7+
// @Filename: a.js
8+
////[|var x = "";
9+
////x = 1;|]
10+
11+
// Disable checking for the whole file
12+
verify.rangeAfterCodeFix(`// @ts-nocheck
13+
var x = "";
14+
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);
15+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowjs: true
4+
// @noEmit: true
5+
// @checkJs: true
6+
7+
// @Filename: a.js
8+
////[|var x = "";
9+
////x = 1;|]
10+
11+
// Disable checking for next line
12+
verify.rangeAfterCodeFix(`var x = "";
13+
// @ts-suppress
14+
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @allowjs: true
4+
// @noEmit: true
5+
// @checkJs: true
6+
7+
// @Filename: a.js
8+
////var x = "";
9+
////
10+
////[|"test \
11+
////"; x = 1;|]
12+
13+
// Disable checking for next line
14+
verify.rangeAfterCodeFix(`"test \\
15+
";
16+
// @ts-suppress
17+
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);
18+

0 commit comments

Comments
 (0)