Add quick fix to disable error checking in a .js file

This commit is contained in:
Mohamed Hegazy 2017-03-07 13:40:15 -08:00
parent fe7719f0a9
commit 706acdf138
14 changed files with 218 additions and 10 deletions

View file

@ -3355,6 +3355,16 @@
"category": "Message",
"code": 90017
},
"Disable checking for this file.": {
"category": "Message",
"code": 90018
},
"Suppress this error message.": {
"category": "Message",
"code": 90019
},
"Octal literal types must use ES2015 syntax. Use the syntax '{0}'.": {
"category": "Error",
"code": 8017

View file

@ -0,0 +1,67 @@
/* @internal */
namespace ts.codefix {
registerCodeFix({
errorCodes: getApplicableDiagnosticCodes(),
getCodeActions: getDisableJsDiagnosticsCodeActions
});
function getApplicableDiagnosticCodes(): number[] {
const allDiagnostcs = <MapLike<DiagnosticMessage>>Diagnostics;
return Object.keys(allDiagnostcs)
.filter(d => allDiagnostcs[d] && allDiagnostcs[d].category === DiagnosticCategory.Error)
.map(d => allDiagnostcs[d].code);
}
function shouldCheckJsFile(sourceFile: SourceFile, compilerOptions: CompilerOptions) {
return sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : compilerOptions.checkJs;
}
function getSuppressCommentLocationForLocation(sourceFile: SourceFile, position: number, newLineCharacter: string) {
let { line } = getLineAndCharacterOfPosition(sourceFile, position);
const lineStartPosition = getStartPositionOfLine(line, sourceFile);
const startPosition = getFirstNonSpaceCharacterPosition(sourceFile.text, lineStartPosition);
if (!isInComment(sourceFile, startPosition) && !isInString(sourceFile, startPosition) && !isInTemplateString(sourceFile, startPosition)) {
const token = getTouchingToken(sourceFile, startPosition);
const tokenLeadingCommnets = getLeadingCommentRangesOfNode(token, sourceFile)
if (!tokenLeadingCommnets || !tokenLeadingCommnets.length || tokenLeadingCommnets[0].pos >= startPosition) {
return {
span: { start: startPosition, length: 0 },
newText: `// @ts-suppress${newLineCharacter}`
};
}
}
return {
span: { start: position, length: 0 },
newText: `${position === startPosition ? "" : newLineCharacter}// @ts-suppress${newLineCharacter}`
};
}
function getDisableJsDiagnosticsCodeActions(context: CodeFixContext): CodeAction[] | undefined {
const { sourceFile, program, newLineCharacter, span } = context;
if (!isInJavaScriptFile(sourceFile) || !shouldCheckJsFile(sourceFile, program.getCompilerOptions())) {
return undefined;
}
return [{
description: getLocaleSpecificMessage(Diagnostics.Suppress_this_error_message),
changes: [{
fileName: sourceFile.fileName,
textChanges: [getSuppressCommentLocationForLocation(sourceFile, span.start, newLineCharacter)]
}]
},
{
description: getLocaleSpecificMessage(Diagnostics.Disable_checking_for_this_file),
changes: [{
fileName: sourceFile.fileName,
textChanges: [{
span: {
start: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.pos : 0,
length: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.end - sourceFile.checkJsDirective.pos : 0
},
newText: `// @ts-nocheck${newLineCharacter}`
}]
}]
}];
}
}

View file

@ -7,4 +7,5 @@
/// <reference path="fixForgottenThisPropertyAccess.ts" />
/// <reference path='unusedIdentifierFixes.ts' />
/// <reference path='importFixes.ts' />
/// <reference path='disableJsDiagnostics.ts' />
/// <reference path='helpers.ts' />

View file

@ -105,9 +105,10 @@ namespace ts.codefix {
else {
// import |d,| * as ns from './file'
const start = importClause.name.getStart();
let end = findFirstNonSpaceCharPosStarting(importClause.name.end);
const text = sourceFile.text;
let end = getFirstNonSpaceCharacterPosition(text, importClause.name.end);
if (sourceFile.text.charCodeAt(end) === CharacterCodes.comma) {
end = findFirstNonSpaceCharPosStarting(end + 1);
end = getFirstNonSpaceCharacterPosition(text, end + 1);
}
return createCodeFix("", start, end - start);
@ -166,13 +167,6 @@ namespace ts.codefix {
return createCodeFix("", start, end - start);
}
function findFirstNonSpaceCharPosStarting(start: number) {
while (isWhiteSpace(sourceFile.text.charCodeAt(start))) {
start += 1;
}
return start;
}
function createCodeFix(newText: string, start: number, length: number): CodeAction[] {
return [{
description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Remove_declaration_for_Colon_0), { 0: token.getText() }),

View file

@ -90,6 +90,7 @@
"codefixes/fixes.ts",
"codefixes/helpers.ts",
"codefixes/importFixes.ts",
"codefixes/unusedIdentifierFixes.ts"
"codefixes/unusedIdentifierFixes.ts",
"codefixes/disableJsDiagnostics.ts"
]
}

View file

@ -1388,4 +1388,11 @@ namespace ts {
// First token is the open curly, this is where we want to put the 'super' call.
return constructor.body.getFirstToken(sourceFile).getEnd();
}
export function getFirstNonSpaceCharacterPosition(text: string, position: number) {
while (isWhiteSpace(text.charCodeAt(position))) {
position += 1;
}
return position;
}
}

View file

@ -0,0 +1,11 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @Filename: a.js
////[|// @ts-check|]
////var x = "";
////x = 1;
verify.rangeAfterCodeFix("// @ts-nocheck", /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);

View file

@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////[|var x = "";
////x = 1;|]
// Disable checking for the whole file
verify.rangeAfterCodeFix(`// @ts-nocheck
var x = "";
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 1);

View file

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////[|var x = "";
////x = 1;|]
// Disable checking for next line
verify.rangeAfterCodeFix(`var x = "";
// @ts-suppress
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

View file

@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////var x = "";
////
////[|"test \
////"; x = 1;|]
// Disable checking for next line
verify.rangeAfterCodeFix(`"test \\
";
// @ts-suppress
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

View file

@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////var x = "";
////
////[|/** comment */
////x = 1;|]
// Disable checking for next line
verify.rangeAfterCodeFix(`/** comment */
// @ts-suppress
x = 1;`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

View file

@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////var x = 0;
////
////function f(_a) {
//// [|f(x());|]
////}
// Disable checking for next line
verify.rangeAfterCodeFix(`// @ts-suppress
f(x());`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

View file

@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////var x = 0;
////
////function f(_a) {
//// [|x();|]
////}
// Disable checking for next line
verify.rangeAfterCodeFix(`// @ts-suppress
x();`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);

View file

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />
// @allowjs: true
// @noEmit: true
// @checkJs: true
// @Filename: a.js
////var x = 0;
////
////function f(_a) {
//// /** comment for f */
//// [|f(x());|]
////}
// Disable checking for next line
verify.rangeAfterCodeFix(`f(
// @ts-suppress
x());`, /*includeWhiteSpace*/ false, /*errorCode*/ undefined, /*index*/ 0);