Skip to content

Commit 6a75689

Browse files
authored
Import assertion: do no parse } if { is not present (microsoft#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 microsoft#46364
1 parent 7582b1b commit 6a75689

File tree

11 files changed

+71
-11
lines changed

11 files changed

+71
-11
lines changed

src/compiler/parser.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7277,19 +7277,24 @@ namespace ts {
72777277
const pos = getNodePos();
72787278
parseExpected(SyntaxKind.AssertKeyword);
72797279
const openBracePosition = scanner.getTokenPos();
7280-
parseExpected(SyntaxKind.OpenBraceToken);
7281-
const multiLine = scanner.hasPrecedingLineBreak();
7282-
const elements = parseDelimitedList(ParsingContext.AssertEntries, parseAssertEntry, /*considerSemicolonAsDelimiter*/ true);
7283-
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
7284-
const lastError = lastOrUndefined(parseDiagnostics);
7285-
if (lastError && lastError.code === Diagnostics._0_expected.code) {
7286-
addRelatedInfo(
7287-
lastError,
7288-
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
7289-
);
7280+
if (parseExpected(SyntaxKind.OpenBraceToken)) {
7281+
const multiLine = scanner.hasPrecedingLineBreak();
7282+
const elements = parseDelimitedList(ParsingContext.AssertEntries, parseAssertEntry, /*considerSemicolonAsDelimiter*/ true);
7283+
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
7284+
const lastError = lastOrUndefined(parseDiagnostics);
7285+
if (lastError && lastError.code === Diagnostics._0_expected.code) {
7286+
addRelatedInfo(
7287+
lastError,
7288+
createDetachedDiagnostic(fileName, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
7289+
);
7290+
}
72907291
}
7292+
return finishNode(factory.createAssertClause(elements, multiLine), pos);
7293+
}
7294+
else {
7295+
const elements = createNodeArray([], getNodePos(), /*end*/ undefined, /*hasTrailingComma*/ false);
7296+
return finishNode(factory.createAssertClause(elements, /*multiLine*/ false), pos);
72917297
}
7292-
return finishNode(factory.createAssertClause(elements, multiLine), pos);
72937298
}
72947299

72957300
function tokenAfterImportDefinitelyProducesImportDeclaration() {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/importAssertion/importAssertion4.ts(1,20): error TS2307: Cannot find module './first' or its corresponding type declarations.
2+
tests/cases/conformance/importAssertion/importAssertion4.ts(2,1): error TS1005: '{' expected.
3+
4+
5+
==== tests/cases/conformance/importAssertion/importAssertion4.ts (2 errors) ====
6+
import * as f from "./first" assert
7+
~~~~~~~~~
8+
!!! error TS2307: Cannot find module './first' or its corresponding type declarations.
9+
10+
11+
!!! error TS1005: '{' expected.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [importAssertion4.ts]
2+
import * as f from "./first" assert
3+
4+
5+
//// [importAssertion4.js]
6+
"use strict";
7+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/importAssertion/importAssertion4.ts ===
2+
import * as f from "./first" assert
3+
>f : Symbol(f, Decl(importAssertion4.ts, 0, 6))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/importAssertion/importAssertion4.ts ===
2+
import * as f from "./first" assert
3+
>f : any
4+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/importAssertion/importAssertion5.ts(1,20): error TS2307: Cannot find module './first' or its corresponding type declarations.
2+
tests/cases/conformance/importAssertion/importAssertion5.ts(2,1): error TS1005: '}' expected.
3+
4+
5+
==== tests/cases/conformance/importAssertion/importAssertion5.ts (2 errors) ====
6+
import * as f from "./first" assert {
7+
~~~~~~~~~
8+
!!! error TS2307: Cannot find module './first' or its corresponding type declarations.
9+
10+
11+
!!! error TS1005: '}' expected.
12+
!!! related TS1007 tests/cases/conformance/importAssertion/importAssertion5.ts:1:37: The parser expected to find a '}' to match the '{' token here.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [importAssertion5.ts]
2+
import * as f from "./first" assert {
3+
4+
5+
//// [importAssertion5.js]
6+
"use strict";
7+
exports.__esModule = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/importAssertion/importAssertion5.ts ===
2+
import * as f from "./first" assert {
3+
>f : Symbol(f, Decl(importAssertion5.ts, 0, 6))
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
=== tests/cases/conformance/importAssertion/importAssertion5.ts ===
2+
import * as f from "./first" assert {
3+
>f : any
4+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import * as f from "./first" assert
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import * as f from "./first" assert {

0 commit comments

Comments
 (0)