-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Prevent detached diagnostics from running off the end of the file #55381
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2150,19 +2150,16 @@ export function createDiagnosticForNodeArrayFromMessageChain(sourceFile: SourceF | |
return createFileDiagnosticFromMessageChain(sourceFile, start, nodes.end - start, messageChain, relatedInformation); | ||
} | ||
|
||
function assertDiagnosticLocation(file: SourceFile | undefined, start: number, length: number) { | ||
function assertDiagnosticLocation(sourceText: string, start: number, length: number) { | ||
Debug.assertGreaterThanOrEqual(start, 0); | ||
Debug.assertGreaterThanOrEqual(length, 0); | ||
|
||
if (file) { | ||
Debug.assertLessThanOrEqual(start, file.text.length); | ||
Debug.assertLessThanOrEqual(start + length, file.text.length); | ||
} | ||
Debug.assertLessThanOrEqual(start, sourceText.length); | ||
Debug.assertLessThanOrEqual(start + length, sourceText.length); | ||
} | ||
|
||
/** @internal */ | ||
export function createFileDiagnosticFromMessageChain(file: SourceFile, start: number, length: number, messageChain: DiagnosticMessageChain, relatedInformation?: DiagnosticRelatedInformation[]): DiagnosticWithLocation { | ||
assertDiagnosticLocation(file, start, length); | ||
assertDiagnosticLocation(file.text, start, length); | ||
return { | ||
file, | ||
start, | ||
|
@@ -8152,8 +8149,12 @@ export function getLocaleSpecificMessage(message: DiagnosticMessage) { | |
} | ||
|
||
/** @internal */ | ||
export function createDetachedDiagnostic(fileName: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation { | ||
assertDiagnosticLocation(/*file*/ undefined, start, length); | ||
export function createDetachedDiagnostic(fileName: string, sourceText: string, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithDetachedLocation { | ||
if ((start + length) > sourceText.length) { | ||
length = sourceText.length - start; | ||
} | ||
Comment on lines
+8153
to
+8155
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 actual fix. |
||
|
||
assertDiagnosticLocation(sourceText, start, length); | ||
let text = getLocaleSpecificMessage(message); | ||
|
||
if (some(args)) { | ||
|
@@ -8222,7 +8223,7 @@ export function attachFileToDiagnostics(diagnostics: DiagnosticWithDetachedLocat | |
|
||
/** @internal */ | ||
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: DiagnosticArguments): DiagnosticWithLocation { | ||
assertDiagnosticLocation(file, start, length); | ||
assertDiagnosticLocation(file.text, start, length); | ||
|
||
let text = getLocaleSpecificMessage(message); | ||
|
||
|
21 changes: 21 additions & 0 deletions
21
tests/baselines/reference/parseUnmatchedTypeAssertion.errors.txt
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,21 @@ | ||
parseUnmatchedTypeAssertion.ts(1,2): error TS1109: Expression expected. | ||
parseUnmatchedTypeAssertion.ts(1,12): error TS1141: String literal expected. | ||
parseUnmatchedTypeAssertion.ts(1,12): error TS2304: Cannot find name 'obju2c77'. | ||
parseUnmatchedTypeAssertion.ts(1,21): error TS1109: Expression expected. | ||
parseUnmatchedTypeAssertion.ts(2,1): error TS1005: '{' expected. | ||
|
||
|
||
==== parseUnmatchedTypeAssertion.ts (5 errors) ==== | ||
@<[[import(obju2c77, | ||
~ | ||
!!! error TS1109: Expression expected. | ||
~~~~~~~~ | ||
!!! error TS1141: String literal expected. | ||
~~~~~~~~ | ||
!!! error TS2304: Cannot find name 'obju2c77'. | ||
|
||
!!! error TS1109: Expression expected. | ||
|
||
|
||
!!! error TS1005: '{' expected. | ||
!!! related TS1007 parseUnmatchedTypeAssertion.ts:2:1: The parser expected to find a '}' to match the '{' token here. |
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,8 @@ | ||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] //// | ||
|
||
//// [parseUnmatchedTypeAssertion.ts] | ||
@<[[import(obju2c77, | ||
|
||
|
||
//// [parseUnmatchedTypeAssertion.js] | ||
; |
6 changes: 6 additions & 0 deletions
6
tests/baselines/reference/parseUnmatchedTypeAssertion.symbols
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,6 @@ | ||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] //// | ||
|
||
=== parseUnmatchedTypeAssertion.ts === | ||
@<[[import(obju2c77, | ||
>obju2c77 : Symbol(obju2c77) | ||
|
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,9 @@ | ||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] //// | ||
|
||
=== parseUnmatchedTypeAssertion.ts === | ||
@<[[import(obju2c77, | ||
> : any | ||
><[[import(obju2c77, : [[any]] | ||
|
||
> : any | ||
|
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 @@ | ||
@<[[import(obju2c77, |
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.
This set of three detached diagnostics are the trouble;
openPosition
can totally be at the end of the file.