-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Never use the entire span of a function declaration or function expression when reporting a checker error. #2205
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,32 +218,35 @@ module ts { | |
| } | ||
|
|
||
| export function createDiagnosticForNode(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any): Diagnostic { | ||
| node = getErrorSpanForNode(node); | ||
| var file = getSourceFileOfNode(node); | ||
|
|
||
| var start = getTokenPosOfNode(node, file); | ||
| var length = node.end - start; | ||
|
|
||
| return createFileDiagnostic(file, start, length, message, arg0, arg1, arg2); | ||
| var sourceFile = getSourceFileOfNode(node); | ||
| var span = getErrorSpanForNode(sourceFile, node); | ||
| return createFileDiagnostic(sourceFile, span.start, span.length, message, arg0, arg1, arg2); | ||
| } | ||
|
|
||
| export function createDiagnosticForNodeFromMessageChain(node: Node, messageChain: DiagnosticMessageChain): Diagnostic { | ||
| node = getErrorSpanForNode(node); | ||
| var file = getSourceFileOfNode(node); | ||
| var start = skipTrivia(file.text, node.pos); | ||
| var length = node.end - start; | ||
| var sourceFile = getSourceFileOfNode(node); | ||
| var span = getErrorSpanForNode(sourceFile, node); | ||
| return { | ||
| file, | ||
| start, | ||
| length, | ||
| file: sourceFile, | ||
| start: span.start, | ||
| length: span.length, | ||
| code: messageChain.code, | ||
| category: messageChain.category, | ||
| messageText: messageChain.next ? messageChain : messageChain.messageText | ||
| }; | ||
| } | ||
|
|
||
| export function getErrorSpanForNode(node: Node): Node { | ||
| var errorSpan: Node; | ||
| /* @internal */ | ||
| export function getSpanOfTokenAtPosition(sourceFile: SourceFile, pos: number): TextSpan { | ||
| var scanner = createScanner(sourceFile.languageVersion, /*skipTrivia*/ true, sourceFile.text); | ||
| scanner.setTextPos(pos); | ||
| scanner.scan(); | ||
| var start = scanner.getTokenPos(); | ||
| return createTextSpanFromBounds(start, scanner.getTextPos()); | ||
| } | ||
|
|
||
| export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan { | ||
| var errorNode = node; | ||
| switch (node.kind) { | ||
| // This list is a work in progress. Add missing node kinds to improve their error | ||
| // spans. | ||
|
|
@@ -254,16 +257,23 @@ module ts { | |
| case SyntaxKind.ModuleDeclaration: | ||
| case SyntaxKind.EnumDeclaration: | ||
| case SyntaxKind.EnumMember: | ||
| errorSpan = (<Declaration>node).name; | ||
| case SyntaxKind.FunctionDeclaration: | ||
| case SyntaxKind.FunctionExpression: | ||
|
Contributor
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. Remove this case label
Contributor
Author
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. Done. |
||
| errorNode = (<Declaration>node).name; | ||
| break; | ||
| } | ||
|
|
||
| if (errorNode === undefined) { | ||
| // If we don't have a better node, then just set the error on the first token of | ||
| // construct. | ||
| return getSpanOfTokenAtPosition(sourceFile, node.pos); | ||
| } | ||
|
|
||
| var pos = nodeIsMissing(errorNode) | ||
| ? errorNode.pos | ||
| : skipTrivia(sourceFile.text, errorNode.pos); | ||
|
|
||
| // We now have the ideal error span, but it may be a node that is optional and absent | ||
| // (e.g. the name of a function expression), in which case errorSpan will be undefined. | ||
| // Alternatively, it might be required and missing (e.g. the name of a module), in which | ||
| // case its pos will equal its end (length 0). In either of these cases, we should fall | ||
| // back to the original node that the error was issued on. | ||
| return errorSpan && errorSpan.pos < errorSpan.end ? errorSpan : node; | ||
| return createTextSpanFromBounds(pos, errorNode.end); | ||
| } | ||
|
|
||
| export function isExternalModule(file: SourceFile): boolean { | ||
|
|
||
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
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.
Maybe call this createNodeDiagnostic