Import assertion: do no parse } if { is not present (#46388)
Previously, import assertion parsing would try to parse both { and }, even if both were missing. If both were missing, the error for } could occur past the end of the file, causing an assertion. Fixes #46364
This commit is contained in:
parent
7582b1bbae
commit
6a75689a25
|
@ -7277,19 +7277,24 @@ namespace ts {
|
||||||
const pos = getNodePos();
|
const pos = getNodePos();
|
||||||
parseExpected(SyntaxKind.AssertKeyword);
|
parseExpected(SyntaxKind.AssertKeyword);
|
||||||
const openBracePosition = scanner.getTokenPos();
|
const openBracePosition = scanner.getTokenPos();
|
||||||
parseExpected(SyntaxKind.OpenBraceToken);
|
if (parseExpected(SyntaxKind.OpenBraceToken)) {
|
||||||
const multiLine = scanner.hasPrecedingLineBreak();
|
const multiLine = scanner.hasPrecedingLineBreak();
|
||||||
const elements = parseDelimitedList(ParsingContext.AssertEntries, parseAssertEntry, /*considerSemicolonAsDelimiter*/ true);
|
const elements = parseDelimitedList(ParsingContext.AssertEntries, parseAssertEntry, /*considerSemicolonAsDelimiter*/ true);
|
||||||
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
|
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
|
||||||
const lastError = lastOrUndefined(parseDiagnostics);
|
const lastError = lastOrUndefined(parseDiagnostics);
|
||||||
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
if (lastError && lastError.code === Diagnostics._0_expected.code) {
|
||||||
addRelatedInfo(
|
addRelatedInfo(
|
||||||
lastError,
|
lastError,
|
||||||
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
|
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return finishNode(factory.createAssertClause(elements, multiLine), pos);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const elements = createNodeArray([], getNodePos(), /*end*/ undefined, /*hasTrailingComma*/ false);
|
||||||
|
return finishNode(factory.createAssertClause(elements, /*multiLine*/ false), pos);
|
||||||
}
|
}
|
||||||
return finishNode(factory.createAssertClause(elements, multiLine), pos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function tokenAfterImportDefinitelyProducesImportDeclaration() {
|
function tokenAfterImportDefinitelyProducesImportDeclaration() {
|
||||||
|
|
11
tests/baselines/reference/importAssertion4.errors.txt
Normal file
11
tests/baselines/reference/importAssertion4.errors.txt
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
tests/cases/conformance/importAssertion/importAssertion4.ts(1,20): error TS2307: Cannot find module './first' or its corresponding type declarations.
|
||||||
|
tests/cases/conformance/importAssertion/importAssertion4.ts(2,1): error TS1005: '{' expected.
|
||||||
|
|
||||||
|
|
||||||
|
==== tests/cases/conformance/importAssertion/importAssertion4.ts (2 errors) ====
|
||||||
|
import * as f from "./first" assert
|
||||||
|
~~~~~~~~~
|
||||||
|
!!! error TS2307: Cannot find module './first' or its corresponding type declarations.
|
||||||
|
|
||||||
|
|
||||||
|
!!! error TS1005: '{' expected.
|
7
tests/baselines/reference/importAssertion4.js
Normal file
7
tests/baselines/reference/importAssertion4.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
//// [importAssertion4.ts]
|
||||||
|
import * as f from "./first" assert
|
||||||
|
|
||||||
|
|
||||||
|
//// [importAssertion4.js]
|
||||||
|
"use strict";
|
||||||
|
exports.__esModule = true;
|
4
tests/baselines/reference/importAssertion4.symbols
Normal file
4
tests/baselines/reference/importAssertion4.symbols
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
=== tests/cases/conformance/importAssertion/importAssertion4.ts ===
|
||||||
|
import * as f from "./first" assert
|
||||||
|
>f : Symbol(f, Decl(importAssertion4.ts, 0, 6))
|
||||||
|
|
4
tests/baselines/reference/importAssertion4.types
Normal file
4
tests/baselines/reference/importAssertion4.types
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
=== tests/cases/conformance/importAssertion/importAssertion4.ts ===
|
||||||
|
import * as f from "./first" assert
|
||||||
|
>f : any
|
||||||
|
|
12
tests/baselines/reference/importAssertion5.errors.txt
Normal file
12
tests/baselines/reference/importAssertion5.errors.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
tests/cases/conformance/importAssertion/importAssertion5.ts(1,20): error TS2307: Cannot find module './first' or its corresponding type declarations.
|
||||||
|
tests/cases/conformance/importAssertion/importAssertion5.ts(2,1): error TS1005: '}' expected.
|
||||||
|
|
||||||
|
|
||||||
|
==== tests/cases/conformance/importAssertion/importAssertion5.ts (2 errors) ====
|
||||||
|
import * as f from "./first" assert {
|
||||||
|
~~~~~~~~~
|
||||||
|
!!! error TS2307: Cannot find module './first' or its corresponding type declarations.
|
||||||
|
|
||||||
|
|
||||||
|
!!! error TS1005: '}' expected.
|
||||||
|
!!! related TS1007 tests/cases/conformance/importAssertion/importAssertion5.ts:1:37: The parser expected to find a '}' to match the '{' token here.
|
7
tests/baselines/reference/importAssertion5.js
Normal file
7
tests/baselines/reference/importAssertion5.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
//// [importAssertion5.ts]
|
||||||
|
import * as f from "./first" assert {
|
||||||
|
|
||||||
|
|
||||||
|
//// [importAssertion5.js]
|
||||||
|
"use strict";
|
||||||
|
exports.__esModule = true;
|
4
tests/baselines/reference/importAssertion5.symbols
Normal file
4
tests/baselines/reference/importAssertion5.symbols
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
=== tests/cases/conformance/importAssertion/importAssertion5.ts ===
|
||||||
|
import * as f from "./first" assert {
|
||||||
|
>f : Symbol(f, Decl(importAssertion5.ts, 0, 6))
|
||||||
|
|
4
tests/baselines/reference/importAssertion5.types
Normal file
4
tests/baselines/reference/importAssertion5.types
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
=== tests/cases/conformance/importAssertion/importAssertion5.ts ===
|
||||||
|
import * as f from "./first" assert {
|
||||||
|
>f : any
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
import * as f from "./first" assert
|
|
@ -0,0 +1 @@
|
||||||
|
import * as f from "./first" assert {
|
Loading…
Reference in a new issue