-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow skipping diagnostics in .js file using comments and quick fixes to add them #14568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
91571f0
Add support for handeling .js file correctelly in fixAddMissingMember…
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 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) { | ||
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}` | ||
}] | ||
}] | ||
}]; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Do you need to add an entry in
src/harness/tsconfig.json
as well?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.
added.