Skip to content

Allow trailing commas after import attributes in ImportType #61920

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4564,6 +4564,7 @@ namespace Parser {
}
parseExpected(SyntaxKind.ColonToken);
attributes = parseImportAttributes(currentToken as SyntaxKind.WithKeyword | SyntaxKind.AssertKeyword, /*skipKeyword*/ true);
parseOptional(SyntaxKind.CommaToken);
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
const lastError = lastOrUndefined(parseDiagnostics);
if (lastError && lastError.code === Diagnostics._0_expected.code) {
Expand Down
49 changes: 49 additions & 0 deletions tests/baselines/reference/importAttributes10.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
b.ts(22,5): error TS1005: '}' expected.
b.ts(23,1): error TS1128: Declaration or statement expected.
b.ts(23,2): error TS1128: Declaration or statement expected.
b.ts(27,18): error TS1478: Identifier or string literal expected.


==== ./a.json (0 errors) ====
{ "key": "value" }

==== ./b.ts (4 errors) ====
declare global {
interface ImportAttributes {
type: "json"
}
}

export type Test1 = typeof import("./a.json", {
with: {
type: "json"
},
});

export type Test2 = typeof import("./a.json", {
with: {
type: "json",
}
});

export type Test3 = typeof import("./a.json", {
with: {
type: "json"
},,
~
!!! error TS1005: '}' expected.
!!! related TS1007 b.ts:19:47: The parser expected to find a '}' to match the '{' token here.
});
~
!!! error TS1128: Declaration or statement expected.
~
!!! error TS1128: Declaration or statement expected.

export type Test4 = typeof import("./a.json", {
with: {
type: "json",,
~
!!! error TS1478: Identifier or string literal expected.
}
});

41 changes: 41 additions & 0 deletions tests/baselines/reference/importAttributes10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/conformance/importAttributes/importAttributes10.ts] ////

//// [a.json]
{ "key": "value" }

//// [b.ts]
declare global {
interface ImportAttributes {
type: "json"
}
}

export type Test1 = typeof import("./a.json", {
with: {
type: "json"
},
});

export type Test2 = typeof import("./a.json", {
with: {
type: "json",
}
});

export type Test3 = typeof import("./a.json", {
with: {
type: "json"
},,
});

export type Test4 = typeof import("./a.json", {
with: {
type: "json",,
}
});


//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
;
50 changes: 50 additions & 0 deletions tests/baselines/reference/importAttributes10.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/conformance/importAttributes/importAttributes10.ts] ////

=== ./a.json ===
{ "key": "value" }
>"key" : Symbol("key", Decl(a.json, 0, 1))

=== ./b.ts ===
declare global {
>global : Symbol(global, Decl(b.ts, 0, 0))

interface ImportAttributes {
>ImportAttributes : Symbol(ImportAttributes, Decl(lib.es5.d.ts, --, --), Decl(b.ts, 0, 16))

type: "json"
>type : Symbol(ImportAttributes.type, Decl(b.ts, 1, 32))
}
}

export type Test1 = typeof import("./a.json", {
>Test1 : Symbol(Test1, Decl(b.ts, 4, 1))

with: {
type: "json"
},
});

export type Test2 = typeof import("./a.json", {
>Test2 : Symbol(Test2, Decl(b.ts, 10, 3))

with: {
type: "json",
}
});

export type Test3 = typeof import("./a.json", {
>Test3 : Symbol(Test3, Decl(b.ts, 16, 3))

with: {
type: "json"
},,
});

export type Test4 = typeof import("./a.json", {
>Test4 : Symbol(Test4, Decl(b.ts, 22, 3))

with: {
type: "json",,
}
});

69 changes: 69 additions & 0 deletions tests/baselines/reference/importAttributes10.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//// [tests/cases/conformance/importAttributes/importAttributes10.ts] ////

=== ./a.json ===
{ "key": "value" }
>{ "key": "value" } : { key: string; }
> : ^^^^^^^^^^^^^^^^
>"key" : string
> : ^^^^^^
>"value" : "value"
> : ^^^^^^^

=== ./b.ts ===
declare global {
>global : any
> : ^^^

interface ImportAttributes {
type: "json"
>type : "json"
> : ^^^^^^
}
}

export type Test1 = typeof import("./a.json", {
>Test1 : { key: string; }
> : ^^^^^^^^^^^^^^^^

with: {
type: "json"
>type : any
> : ^^^

},
});

export type Test2 = typeof import("./a.json", {
>Test2 : { key: string; }
> : ^^^^^^^^^^^^^^^^

with: {
type: "json",
>type : any
> : ^^^
}
});

export type Test3 = typeof import("./a.json", {
>Test3 : { key: string; }
> : ^^^^^^^^^^^^^^^^

with: {
type: "json"
>type : any
> : ^^^

},,
});

export type Test4 = typeof import("./a.json", {
>Test4 : { key: string; }
> : ^^^^^^^^^^^^^^^^

with: {
type: "json",,
>type : any
> : ^^^
}
});

36 changes: 36 additions & 0 deletions tests/cases/conformance/importAttributes/importAttributes10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @module: nodenext
// @target: esnext
// @moduleResolution: nodenext
// @filename: ./a.json
{ "key": "value" }

// @filename: ./b.ts
declare global {
interface ImportAttributes {
type: "json"
}
}

export type Test1 = typeof import("./a.json", {
with: {
type: "json"
},
});

export type Test2 = typeof import("./a.json", {
with: {
type: "json",
}
});

export type Test3 = typeof import("./a.json", {
with: {
type: "json"
},,
});

export type Test4 = typeof import("./a.json", {
with: {
type: "json",,
}
});