Skip to content

Commit de23842

Browse files
Allow import assertions on esm imports under module: nodenext (microsoft#46630)
* Allow import assertions on esm imports under module: nodenext * Changes to copy Co-authored-by: Andrew Branch <[email protected]>
1 parent b8f8fd7 commit de23842

17 files changed

+307
-74
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39695,8 +39695,12 @@ namespace ts {
3969539695

3969639696
function checkAssertClause(declaration: ImportDeclaration | ExportDeclaration) {
3969739697
if (declaration.assertClause) {
39698-
if (moduleKind !== ModuleKind.ESNext) {
39699-
return grammarErrorOnNode(declaration.assertClause, Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext);
39698+
const mode = (moduleKind === ModuleKind.NodeNext) && declaration.moduleSpecifier && getUsageModeForExpression(declaration.moduleSpecifier);
39699+
if (mode !== ModuleKind.ESNext && moduleKind !== ModuleKind.ESNext) {
39700+
return grammarErrorOnNode(declaration.assertClause,
39701+
moduleKind === ModuleKind.NodeNext
39702+
? Diagnostics.Import_assertions_are_not_allowed_on_statements_that_transpile_to_commonjs_require_calls
39703+
: Diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_or_nodenext);
3970039704
}
3970139705

3970239706
if (isImportDeclaration(declaration) ? declaration.importClause?.isTypeOnly : declaration.isTypeOnly) {
@@ -43906,13 +43910,13 @@ namespace ts {
4390643910
}
4390743911

4390843912
const nodeArguments = node.arguments;
43909-
if (moduleKind !== ModuleKind.ESNext) {
43913+
if (moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.NodeNext) {
4391043914
// We are allowed trailing comma after proposal-import-assertions.
4391143915
checkGrammarForDisallowedTrailingComma(nodeArguments);
4391243916

4391343917
if (nodeArguments.length > 1) {
4391443918
const assertionArgument = nodeArguments[1];
43915-
return grammarErrorOnNode(assertionArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext);
43919+
return grammarErrorOnNode(assertionArgument, Diagnostics.Dynamic_imports_only_support_a_second_argument_when_the_module_option_is_set_to_esnext_or_nodenext);
4391643920
}
4391743921
}
4391843922

src/compiler/diagnosticMessages.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@
924924
"category": "Error",
925925
"code": 1323
926926
},
927-
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.": {
927+
"Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.": {
928928
"category": "Error",
929929
"code": 1324
930930
},
@@ -3333,7 +3333,7 @@
33333333
"category": "Error",
33343334
"code": 2820
33353335
},
3336-
"Import assertions are only supported when the '--module' option is set to 'esnext'.": {
3336+
"Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.": {
33373337
"category": "Error",
33383338
"code": 2821
33393339
},
@@ -3353,6 +3353,10 @@
33533353
"category": "Error",
33543354
"code": 2835
33553355
},
3356+
"Import assertions are not allowed on statements that transpile to commonjs 'require' calls.": {
3357+
"category": "Error",
3358+
"code": 2836
3359+
},
33563360

33573361
"Import declaration '{0}' is using private name '{1}'.": {
33583362
"category": "Error",

tests/baselines/reference/importAssertion1(module=commonjs).errors.txt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/conformance/importAssertion/1.ts(1,14): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
2-
tests/cases/conformance/importAssertion/1.ts(2,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
3-
tests/cases/conformance/importAssertion/1.ts(3,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
4-
tests/cases/conformance/importAssertion/2.ts(1,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
5-
tests/cases/conformance/importAssertion/2.ts(2,38): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
6-
tests/cases/conformance/importAssertion/3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
7-
tests/cases/conformance/importAssertion/3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
8-
tests/cases/conformance/importAssertion/3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
9-
tests/cases/conformance/importAssertion/3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
10-
tests/cases/conformance/importAssertion/3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
1+
tests/cases/conformance/importAssertion/1.ts(1,14): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2+
tests/cases/conformance/importAssertion/1.ts(2,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
3+
tests/cases/conformance/importAssertion/1.ts(3,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4+
tests/cases/conformance/importAssertion/2.ts(1,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
5+
tests/cases/conformance/importAssertion/2.ts(2,38): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
6+
tests/cases/conformance/importAssertion/3.ts(2,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
7+
tests/cases/conformance/importAssertion/3.ts(3,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
8+
tests/cases/conformance/importAssertion/3.ts(4,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
9+
tests/cases/conformance/importAssertion/3.ts(5,26): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
10+
tests/cases/conformance/importAssertion/3.ts(7,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
1111
tests/cases/conformance/importAssertion/3.ts(8,11): message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments
12-
tests/cases/conformance/importAssertion/3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
13-
tests/cases/conformance/importAssertion/3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
12+
tests/cases/conformance/importAssertion/3.ts(9,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
13+
tests/cases/conformance/importAssertion/3.ts(10,25): error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
1414
tests/cases/conformance/importAssertion/3.ts(10,52): error TS1009: Trailing comma not allowed.
1515

1616

@@ -21,13 +21,13 @@ tests/cases/conformance/importAssertion/3.ts(10,52): error TS1009: Trailing comm
2121
==== tests/cases/conformance/importAssertion/1.ts (3 errors) ====
2222
import './0' assert { type: "json" }
2323
~~~~~~~~~~~~~~~~~~~~~~~
24-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
24+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2525
import { a, b } from './0' assert { "type": "json" }
2626
~~~~~~~~~~~~~~~~~~~~~~~~~
27-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
27+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2828
import * as foo from './0' assert { type: "json" }
2929
~~~~~~~~~~~~~~~~~~~~~~~
30-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
30+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
3131
a;
3232
b;
3333
foo.a;
@@ -36,10 +36,10 @@ tests/cases/conformance/importAssertion/3.ts(10,52): error TS1009: Trailing comm
3636
==== tests/cases/conformance/importAssertion/2.ts (2 errors) ====
3737
import { a, b } from './0' assert {}
3838
~~~~~~~~~
39-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
39+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4040
import { a as c, b as d } from './0' assert { a: "a", b: "b", c: "c" }
4141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
42+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4343
a;
4444
b;
4545
c;
@@ -49,29 +49,29 @@ tests/cases/conformance/importAssertion/3.ts(10,52): error TS1009: Trailing comm
4949
const a = import('./0')
5050
const b = import('./0', { assert: { type: "json" } })
5151
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
52+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
5353
const c = import('./0', { assert: { type: "json", ttype: "typo" } })
5454
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
55+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
5656
const d = import('./0', { assert: {} })
5757
~~~~~~~~~~~~~~
58-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
58+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
5959
const dd = import('./0', {})
6060
~~
61-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
61+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
6262
declare function foo(): any;
6363
const e = import('./0', foo())
6464
~~~~~
65-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
65+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
6666
const f = import()
6767
~~~~~~~~
6868
!!! message TS1450: Dynamic imports can only accept a module specifier and an optional assertion as arguments
6969
const g = import('./0', {}, {})
7070
~~
71-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
71+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
7272
const h = import('./0', { assert: { type: "json" }},)
7373
~~~~~~~~~~~~~~~~~~~~~~~~~~~
74-
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext'.
74+
!!! error TS1324: Dynamic imports only support a second argument when the '--module' option is set to 'esnext' or 'nodenext'.
7575
~
7676
!!! error TS1009: Trailing comma not allowed.
7777

tests/baselines/reference/importAssertion1(module=es2015).errors.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/importAssertion/1.ts(1,14): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
2-
tests/cases/conformance/importAssertion/1.ts(2,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
3-
tests/cases/conformance/importAssertion/1.ts(3,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
4-
tests/cases/conformance/importAssertion/2.ts(1,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
5-
tests/cases/conformance/importAssertion/2.ts(2,38): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
1+
tests/cases/conformance/importAssertion/1.ts(1,14): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2+
tests/cases/conformance/importAssertion/1.ts(2,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
3+
tests/cases/conformance/importAssertion/1.ts(3,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4+
tests/cases/conformance/importAssertion/2.ts(1,28): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
5+
tests/cases/conformance/importAssertion/2.ts(2,38): error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
66
tests/cases/conformance/importAssertion/3.ts(1,11): error TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node12', or 'nodenext'.
77
tests/cases/conformance/importAssertion/3.ts(2,11): error TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node12', or 'nodenext'.
88
tests/cases/conformance/importAssertion/3.ts(3,11): error TS1323: Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'es2022', 'esnext', 'commonjs', 'amd', 'system', 'umd', 'node12', or 'nodenext'.
@@ -21,13 +21,13 @@ tests/cases/conformance/importAssertion/3.ts(10,11): error TS1323: Dynamic impor
2121
==== tests/cases/conformance/importAssertion/1.ts (3 errors) ====
2222
import './0' assert { type: "json" }
2323
~~~~~~~~~~~~~~~~~~~~~~~
24-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
24+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2525
import { a, b } from './0' assert { "type": "json" }
2626
~~~~~~~~~~~~~~~~~~~~~~~~~
27-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
27+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
2828
import * as foo from './0' assert { type: "json" }
2929
~~~~~~~~~~~~~~~~~~~~~~~
30-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
30+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
3131
a;
3232
b;
3333
foo.a;
@@ -36,10 +36,10 @@ tests/cases/conformance/importAssertion/3.ts(10,11): error TS1323: Dynamic impor
3636
==== tests/cases/conformance/importAssertion/2.ts (2 errors) ====
3737
import { a, b } from './0' assert {}
3838
~~~~~~~~~
39-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
39+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4040
import { a as c, b as d } from './0' assert { a: "a", b: "b", c: "c" }
4141
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42-
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext'.
42+
!!! error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.
4343
a;
4444
b;
4545
c;

0 commit comments

Comments
 (0)