mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-11 17:41:26 -06:00
Prevent detached diagnostics from running off the end of the file (#55381)
This commit is contained in:
parent
c5281bf700
commit
f37d2ad669
@ -1803,7 +1803,7 @@ namespace Parser {
|
||||
return sourceFile;
|
||||
|
||||
function reportPragmaDiagnostic(pos: number, end: number, diagnostic: DiagnosticMessage) {
|
||||
parseDiagnostics.push(createDetachedDiagnostic(fileName, pos, end, diagnostic));
|
||||
parseDiagnostics.push(createDetachedDiagnostic(fileName, sourceText, pos, end, diagnostic));
|
||||
}
|
||||
}
|
||||
|
||||
@ -2118,7 +2118,7 @@ namespace Parser {
|
||||
const lastError = lastOrUndefined(parseDiagnostics);
|
||||
let result: DiagnosticWithDetachedLocation | undefined;
|
||||
if (!lastError || start !== lastError.start) {
|
||||
result = createDetachedDiagnostic(fileName, start, length, message, ...args);
|
||||
result = createDetachedDiagnostic(fileName, sourceText, start, length, message, ...args);
|
||||
parseDiagnostics.push(result);
|
||||
}
|
||||
|
||||
@ -2470,7 +2470,7 @@ namespace Parser {
|
||||
if (lastError) {
|
||||
addRelatedInfo(
|
||||
lastError,
|
||||
createDetachedDiagnostic(fileName, openPosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, tokenToString(openKind), tokenToString(closeKind))
|
||||
createDetachedDiagnostic(fileName, sourceText, openPosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, tokenToString(openKind), tokenToString(closeKind))
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -4486,7 +4486,7 @@ namespace Parser {
|
||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||
addRelatedInfo(
|
||||
lastError,
|
||||
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
|
||||
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -8326,7 +8326,7 @@ namespace Parser {
|
||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||
addRelatedInfo(
|
||||
lastError,
|
||||
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
|
||||
createDetachedDiagnostic(fileName, sourceText, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_1_to_match_the_0_token_here, "{", "}")
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -9429,7 +9429,7 @@ namespace Parser {
|
||||
if (childTypeTag) {
|
||||
const lastError = parseErrorAtCurrentToken(Diagnostics.A_JSDoc_typedef_comment_may_not_contain_multiple_type_tags);
|
||||
if (lastError) {
|
||||
addRelatedInfo(lastError, createDetachedDiagnostic(fileName, 0, 0, Diagnostics.The_tag_was_first_specified_here));
|
||||
addRelatedInfo(lastError, createDetachedDiagnostic(fileName, sourceText, 0, 0, Diagnostics.The_tag_was_first_specified_here));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@ -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.
|
||||
8
tests/baselines/reference/parseUnmatchedTypeAssertion.js
Normal file
8
tests/baselines/reference/parseUnmatchedTypeAssertion.js
Normal file
@ -0,0 +1,8 @@
|
||||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
|
||||
|
||||
//// [parseUnmatchedTypeAssertion.ts]
|
||||
@<[[import(obju2c77,
|
||||
|
||||
|
||||
//// [parseUnmatchedTypeAssertion.js]
|
||||
;
|
||||
@ -0,0 +1,6 @@
|
||||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
|
||||
|
||||
=== parseUnmatchedTypeAssertion.ts ===
|
||||
@<[[import(obju2c77,
|
||||
>obju2c77 : Symbol(obju2c77)
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
//// [tests/cases/compiler/parseUnmatchedTypeAssertion.ts] ////
|
||||
|
||||
=== parseUnmatchedTypeAssertion.ts ===
|
||||
@<[[import(obju2c77,
|
||||
> : any
|
||||
><[[import(obju2c77, : [[any]]
|
||||
|
||||
> : any
|
||||
|
||||
1
tests/cases/compiler/parseUnmatchedTypeAssertion.ts
Normal file
1
tests/cases/compiler/parseUnmatchedTypeAssertion.ts
Normal file
@ -0,0 +1 @@
|
||||
@<[[import(obju2c77,
|
||||
Loading…
x
Reference in New Issue
Block a user