-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to report errors in js files #14496
Merged
Merged
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
91571f0
Add support for handeling .js file correctelly in fixAddMissingMember…
mhegazy 0b1fff7
Add `--checkJsFiles`
mhegazy 9f0c5ce
Add support for `//@check` directives
mhegazy 0b247b1
Add tests
mhegazy 1f9bb69
Add --noEmit to tests
mhegazy b015c1d
Allow @check directives to switch on/off checking in a file
mhegazy 9305d4d
Change flag name to `checkJs`
mhegazy fb218b7
Error if `--checkJs` is used without `--allowJs`
mhegazy e9f8214
Code review comments
mhegazy a202fa4
add es6 to buildProtocol
mhegazy 3d03f8d
Merge branch 'fixBuildBreak' into checkJSFiles
mhegazy fe7719f
Disable check diagnostics per line
mhegazy 706acdf
Add quick fix to disable error checking in a .js file
mhegazy 13e80b9
Fix building webTestServer
mhegazy 936a91d
Add comment
mhegazy cc6affa
Merge remote-tracking branch 'origin/updateCodeFixForAddMissingMember…
mhegazy 6e86596
Add debugging utilities
mhegazy fd9fb8f
Support static properties
mhegazy 509b2dc
Add disableJsDiagnostics codefixes to harnes
mhegazy 1fbbead
Merge pull request #14568 from Microsoft/checkJSFiles_QuickFixes
mhegazy 7980629
Code review comments
mhegazy 0dac29f
Merge branch 'master' into checkJSFiles
mhegazy 3b57b5d
Refactor checking for checkJs value in a common helper
mhegazy e408cad
Merge branch 'master' into checkJSFiles
mhegazy db6c969
Change ingore diagonstic comment to `// @ts-ignore`
mhegazy 3378f5c
Merge branch 'master' into checkJSFiles
mhegazy e630ab1
Report semantic errors for JS files if checkJs is enabled
mhegazy 0637f24
Merge remote-tracking branch 'origin/master' into checkJSFiles
mhegazy 8ea9617
Merge remote-tracking branch 'origin/master' into checkJSFiles
mhegazy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace ts { | ||
const emptyArray: any[] = []; | ||
const suppressDiagnosticCommentRegEx = /(^\s*$)|(^\s*\/\/\/?\s*(@ts-suppress)?)/; | ||
|
||
export function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName = "tsconfig.json"): string { | ||
while (true) { | ||
|
@@ -916,17 +917,43 @@ namespace ts { | |
|
||
Debug.assert(!!sourceFile.bindDiagnostics); | ||
const bindDiagnostics = sourceFile.bindDiagnostics; | ||
// For JavaScript files, we don't want to report semantic errors. | ||
// Instead, we'll report errors for using TypeScript-only constructs from within a | ||
// JavaScript file when we get syntactic diagnostics for the file. | ||
const checkDiagnostics = isSourceFileJavaScript(sourceFile) ? [] : typeChecker.getDiagnostics(sourceFile, cancellationToken); | ||
// For JavaScript files, we don't want to report semantic errors unless explicitly requested. | ||
const includeCheckDiagnostics = !isSourceFileJavaScript(sourceFile) || | ||
(sourceFile.checkJsDirective ? sourceFile.checkJsDirective.enabled : options.checkJs); | ||
const checkDiagnostics = includeCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : []; | ||
const fileProcessingDiagnosticsInFile = fileProcessingDiagnostics.getDiagnostics(sourceFile.fileName); | ||
const programDiagnosticsInFile = programDiagnostics.getDiagnostics(sourceFile.fileName); | ||
|
||
return bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile); | ||
const diagnostics = bindDiagnostics.concat(checkDiagnostics, fileProcessingDiagnosticsInFile, programDiagnosticsInFile); | ||
return isSourceFileJavaScript(sourceFile) | ||
? filter(diagnostics, shouldReportDiagnostic) | ||
: diagnostics; | ||
}); | ||
} | ||
|
||
/** | ||
* Skip errors if previous line start with '// @ts-suppress' comment, not counting non-empty non-comment lines | ||
*/ | ||
function shouldReportDiagnostic(diagnostic: Diagnostic) { | ||
const { file, start } = diagnostic; | ||
const lineStarts = getLineStarts(file); | ||
let { line } = computeLineAndCharacterOfPosition(lineStarts, start); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
while (line > 0) { | ||
const previousLineText = file.text.slice(lineStarts[line - 1], lineStarts[line]); | ||
const result = suppressDiagnosticCommentRegEx.exec(previousLineText); | ||
if (!result) { | ||
// non-empty line | ||
return true; | ||
} | ||
if (result[3]) { | ||
// @ts-suppress | ||
return false; | ||
} | ||
line--; | ||
} | ||
return true; | ||
} | ||
|
||
function getJavaScriptSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] { | ||
return runWithCancellationToken(() => { | ||
const diagnostics: Diagnostic[] = []; | ||
|
@@ -1727,6 +1754,10 @@ namespace ts { | |
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "allowJs", "declaration")); | ||
} | ||
|
||
if (options.checkJs && !options.allowJs) { | ||
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "checkJs", "allowJs")); | ||
} | ||
|
||
if (options.emitDecoratorMetadata && | ||
!options.experimentalDecorators) { | ||
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_without_specifying_option_1, "emitDecoratorMetadata", "experimentalDecorators")); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the same snipped with this one. Can we share this? |
||
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); | ||
|
||
// First try to see if we can put the '// @ts-suppress' on the previous line. | ||
// We need to make sure that we are not in the middle of a string literal or a comment. | ||
// We also want to check if the previous line holds a comment for a node on the next line | ||
// if so, we do not want to separate the node from its comment if we can. | ||
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}` | ||
}; | ||
} | ||
} | ||
|
||
// If all fails, add an extra new line immediatlly before the error span. | ||
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}` | ||
}] | ||
}] | ||
}]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend you pull this out of the function and remove the
g
option.g
is only really useful if you want to preservelastIndex
when using a regular expression in a loop. Also I would remove them
option as it should be unnecessary.Consider this instead:
Capturing only the
no
allows you to avoid the additional case-insensitive string comparison.