Skip to content

Commit 4b4977a

Browse files
authored
Fix dynamic import grammar check (#2138)
1 parent e027a2a commit 4b4977a

9 files changed

+8
-89
lines changed

internal/checker/grammarchecks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ func (c *Checker) checkGrammarImportCallExpression(node *ast.Node) bool {
21912191

21922192
nodeArguments := nodeAsCall.Arguments
21932193
argumentNodes := nodeArguments.Nodes
2194-
if c.moduleKind != core.ModuleKindESNext && c.moduleKind != core.ModuleKindNodeNext && c.moduleKind != core.ModuleKindNode16 && c.moduleKind != core.ModuleKindPreserve {
2194+
if !(core.ModuleKindNode16 <= c.moduleKind && c.moduleKind <= core.ModuleKindNodeNext) && c.moduleKind != core.ModuleKindESNext && c.moduleKind != core.ModuleKindPreserve {
21952195
// We are allowed trailing comma after proposal-import-assertions.
21962196
c.checkGrammarForDisallowedTrailingComma(nodeArguments, diagnostics.Trailing_comma_not_allowed)
21972197

testdata/baselines/reference/submodule/conformance/nodeModulesImportAssertions(module=node18).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
otherc.cts(1,35): error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
2-
otherc.cts(2,40): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
32

43

54
==== index.ts (0 errors) ====
65
import json from "./package.json" assert { type: "json" };
7-
==== otherc.cts (2 errors) ====
6+
==== otherc.cts (1 errors) ====
87
import json from "./package.json" assert { type: "json" }; // should error, cjs mode imports don't support assertions
98
~~~~~~~~~~~~~~~~~~~~~~~
109
!!! error TS2836: Import assertions are not allowed on statements that compile to CommonJS 'require' calls.
1110
const json2 = import("./package.json", { assert: { type: "json" } }); // should be fine
12-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
1411
==== package.json (0 errors) ====
1512
{
1613
"name": "pkg",

testdata/baselines/reference/submodule/conformance/nodeModulesImportAssertions(module=node18).errors.txt.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/nodeModulesImportAssertions(module=node20).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
index.ts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
22
otherc.cts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
3-
otherc.cts(2,40): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
43

54

65
==== index.ts (1 errors) ====
76
import json from "./package.json" assert { type: "json" };
87
~~~~~~~~~~~~~~~~~~~~~~~
98
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
10-
==== otherc.cts (2 errors) ====
9+
==== otherc.cts (1 errors) ====
1110
import json from "./package.json" assert { type: "json" }; // should error, cjs mode imports don't support assertions
1211
~~~~~~~~~~~~~~~~~~~~~~~
1312
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
1413
const json2 = import("./package.json", { assert: { type: "json" } }); // should be fine
15-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
1714
==== package.json (0 errors) ====
1815
{
1916
"name": "pkg",
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
--- old.nodeModulesImportAssertions(module=node20).errors.txt
22
+++ new.nodeModulesImportAssertions(module=node20).errors.txt
3-
@@= skipped -0, +0 lines =@@
4-
index.ts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
5-
otherc.cts(1,35): error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
6-
+otherc.cts(2,40): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
7-
3+
@@= skipped -3, +3 lines =@@
84

95
==== index.ts (1 errors) ====
106
import json from "./package.json" assert { type: "json" };
117
- ~~~~~~
128
+ ~~~~~~~~~~~~~~~~~~~~~~~
139
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
14-
-==== otherc.cts (1 errors) ====
15-
+==== otherc.cts (2 errors) ====
10+
==== otherc.cts (1 errors) ====
1611
import json from "./package.json" assert { type: "json" }; // should error, cjs mode imports don't support assertions
1712
- ~~~~~~
1813
+ ~~~~~~~~~~~~~~~~~~~~~~~
1914
!!! error TS2880: Import assertions have been replaced by import attributes. Use 'with' instead of 'assert'.
2015
const json2 = import("./package.json", { assert: { type: "json" } }); // should be fine
21-
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22-
+!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
23-
==== package.json (0 errors) ====
24-
{
25-
"name": "pkg",
16+
==== package.json (0 errors) ====

testdata/baselines/reference/submodule/conformance/nodeModulesImportAttributes(module=node18).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
otherc.cts(1,35): error TS2856: Import attributes are not allowed on statements that compile to CommonJS 'require' calls.
2-
otherc.cts(2,40): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
32

43

54
==== index.ts (0 errors) ====
65
import json from "./package.json" with { type: "json" };
7-
==== otherc.cts (2 errors) ====
6+
==== otherc.cts (1 errors) ====
87
import json from "./package.json" with { type: "json" }; // should error, cjs mode imports don't support attributes
98
~~~~~~~~~~~~~~~~~~~~~
109
!!! error TS2856: Import attributes are not allowed on statements that compile to CommonJS 'require' calls.
1110
const json2 = import("./package.json", { with: { type: "json" } }); // should be fine
12-
~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
1411
==== package.json (0 errors) ====
1512
{
1613
"name": "pkg",

testdata/baselines/reference/submodule/conformance/nodeModulesImportAttributes(module=node18).errors.txt.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

testdata/baselines/reference/submodule/conformance/nodeModulesImportAttributes(module=node20).errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
otherc.cts(1,35): error TS2856: Import attributes are not allowed on statements that compile to CommonJS 'require' calls.
2-
otherc.cts(2,40): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
32

43

54
==== index.ts (0 errors) ====
65
import json from "./package.json" with { type: "json" };
7-
==== otherc.cts (2 errors) ====
6+
==== otherc.cts (1 errors) ====
87
import json from "./package.json" with { type: "json" }; // should error, cjs mode imports don't support attributes
98
~~~~~~~~~~~~~~~~~~~~~
109
!!! error TS2856: Import attributes are not allowed on statements that compile to CommonJS 'require' calls.
1110
const json2 = import("./package.json", { with: { type: "json" } }); // should be fine
12-
~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext', 'node16', 'node18', 'node20', 'nodenext', or 'preserve'.
1411
==== package.json (0 errors) ====
1512
{
1613
"name": "pkg",

testdata/baselines/reference/submodule/conformance/nodeModulesImportAttributes(module=node20).errors.txt.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)