Skip to content

Commit dfe8cba

Browse files
Support trailing commas in typeof import()
1 parent 29e6d66 commit dfe8cba

14 files changed

+148
-1
lines changed

Diff for: playground/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "playground",
3+
"version": "1.0.0",
4+
"main": "test.js",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"keywords": [],
9+
"author": "",
10+
"license": "ISC",
11+
"description": "",
12+
"dependencies": {
13+
"typescript": "file:.."
14+
}
15+
}

Diff for: playground/src/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Enum without initializers have first member = 0 and successive members = N + 1
2+
// Enum literal syntax does not implement auto-incrementing behaviour.
3+
let ExistingShorthand = "exists";
4+
const E1 = {
5+
Int: 1// Enum without initializers have first member = 0 and successive members = N + 1
6+
// Enum literal syntax does not implement auto-incrementing behaviour.
7+
, // Enum without initializers have first member = 0 and successive members = N + 1
8+
String: "string"// Enum without initializers have first member = 0 and successive members = N + 1
9+
// Enum literal syntax does not implement auto-incrementing behaviour.
10+
, // Enum without initializers have first member = 0 and successive members = N + 1
11+
Flag: 8// Enum without initializers have first member = 0 and successive members = N + 1
12+
// Enum literal syntax does not implement auto-incrementing behaviour.
13+
};

Diff for: playground/src/test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Enum without initializers have first member = 0 and successive members = N + 1
2+
3+
// Enum literal syntax does not implement auto-incrementing behaviour.
4+
let ExistingShorthand = "exists";
5+
const E1: enum = {
6+
Int: 1, // ok
7+
String: "string", // ok
8+
Flag: 8, // ok
9+
};

Diff for: playground/tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"skipLibCheck": true,
6+
},
7+
"include": ["./src/**/*"]
8+
}

Diff for: src/compiler/parser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4551,7 +4551,7 @@ namespace Parser {
45514551
parseExpected(SyntaxKind.OpenParenToken);
45524552
const type = parseType();
45534553
let attributes: ImportAttributes | undefined;
4554-
if (parseOptional(SyntaxKind.CommaToken)) {
4554+
if (parseOptional(SyntaxKind.CommaToken) && token() !== SyntaxKind.CloseParenToken) {
45554555
const openBracePosition = scanner.getTokenStart();
45564556
parseExpected(SyntaxKind.OpenBraceToken);
45574557
const currentToken = token();
@@ -4572,6 +4572,7 @@ namespace Parser {
45724572
);
45734573
}
45744574
}
4575+
parseOptional(SyntaxKind.CommaToken);
45754576
}
45764577
parseExpected(SyntaxKind.CloseParenToken);
45774578
const qualifier = parseOptional(SyntaxKind.DotToken) ? parseEntityNameOfTypeReference() : undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
main.ts(1,39): error TS1005: '{' expected.
2+
3+
4+
==== input.ts (0 errors) ====
5+
export type X = 1;
6+
7+
==== main.ts (1 errors) ====
8+
type T2 = typeof import('./input.js', ,);
9+
~
10+
!!! error TS1005: '{' expected.
11+
!!! related TS1007 main.ts:1:39: The parser expected to find a '}' to match the '{' token here.
12+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/typeofImportInvalidElision.ts] ////
2+
3+
//// [input.ts]
4+
export type X = 1;
5+
6+
//// [main.ts]
7+
type T2 = typeof import('./input.js', ,);
8+
9+
10+
//// [input.js]
11+
"use strict";
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
//// [main.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [tests/cases/compiler/typeofImportInvalidElision.ts] ////
2+
3+
=== input.ts ===
4+
export type X = 1;
5+
>X : Symbol(X, Decl(input.ts, 0, 0))
6+
7+
=== main.ts ===
8+
type T2 = typeof import('./input.js', ,);
9+
>T2 : Symbol(T2, Decl(main.ts, 0, 0))
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/typeofImportInvalidElision.ts] ////
2+
3+
=== input.ts ===
4+
export type X = 1;
5+
>X : 1
6+
> : ^
7+
8+
=== main.ts ===
9+
type T2 = typeof import('./input.js', ,);
10+
>T2 : typeof import("input")
11+
> : ^^^^^^^^^^^^^^^^^^^^^^
12+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/typeofImportTrailingCommas.ts] ////
2+
3+
//// [input.ts]
4+
export type X = 1;
5+
6+
//// [main.ts]
7+
type T1 = typeof import('./input.js',)
8+
type T2 = typeof import('./input.js', { with: { "resolution-mode": "import" } },);
9+
10+
11+
//// [input.js]
12+
"use strict";
13+
Object.defineProperty(exports, "__esModule", { value: true });
14+
//// [main.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/typeofImportTrailingCommas.ts] ////
2+
3+
=== input.ts ===
4+
export type X = 1;
5+
>X : Symbol(X, Decl(input.ts, 0, 0))
6+
7+
=== main.ts ===
8+
type T1 = typeof import('./input.js',)
9+
>T1 : Symbol(T1, Decl(main.ts, 0, 0))
10+
11+
type T2 = typeof import('./input.js', { with: { "resolution-mode": "import" } },);
12+
>T2 : Symbol(T2, Decl(main.ts, 0, 38))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/typeofImportTrailingCommas.ts] ////
2+
3+
=== input.ts ===
4+
export type X = 1;
5+
>X : 1
6+
> : ^
7+
8+
=== main.ts ===
9+
type T1 = typeof import('./input.js',)
10+
>T1 : typeof import("input")
11+
> : ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
type T2 = typeof import('./input.js', { with: { "resolution-mode": "import" } },);
14+
>T2 : typeof import("input")
15+
> : ^^^^^^^^^^^^^^^^^^^^^^
16+

Diff for: tests/cases/compiler/typeofImportInvalidElision.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @filename: input.ts
2+
export type X = 1;
3+
4+
// @filename: main.ts
5+
type T2 = typeof import('./input.js', ,);

Diff for: tests/cases/compiler/typeofImportTrailingCommas.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @filename: input.ts
2+
export type X = 1;
3+
4+
// @filename: main.ts
5+
type T1 = typeof import('./input.js',)
6+
type T2 = typeof import('./input.js', { with: { "resolution-mode": "import" } },);

0 commit comments

Comments
 (0)