Skip to content

Commit 5e52b28

Browse files
authored
Support require(esm) in --module nodenext (#60761)
1 parent efe07a0 commit 5e52b28

File tree

209 files changed

+3602
-580
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

209 files changed

+3602
-580
lines changed

src/compiler/checker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4715,7 +4715,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47154715
if (errorNode && resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
47164716
errorOnImplicitAnyModule(/*isError*/ false, errorNode, currentSourceFile, mode, resolvedModule, moduleReference);
47174717
}
4718-
if (errorNode && (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext)) {
4718+
if (errorNode && (moduleKind === ModuleKind.Node16 || moduleKind === ModuleKind.Node18)) {
47194719
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
47204720
const overrideHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l) || isJSDocImportTag(l));
47214721
// An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
@@ -48082,6 +48082,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4808248082
);
4808348083
}
4808448084

48085+
if (moduleKind === ModuleKind.NodeNext && !isImportAttributes) {
48086+
return grammarErrorOnFirstToken(node, Diagnostics.Import_assertions_have_been_replaced_by_import_attributes_Use_with_instead_of_asserts);
48087+
}
48088+
4808548089
if (declaration.moduleSpecifier && getEmitSyntaxForModuleSpecifierExpression(declaration.moduleSpecifier) === ModuleKind.CommonJS) {
4808648090
return grammarErrorOnNode(
4808748091
node,

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,10 @@
39803980
"category": "Error",
39813981
"code": 2879
39823982
},
3983+
"Import assertions have been replaced by import attributes. Use 'with' instead of 'asserts'.": {
3984+
"category": "Error",
3985+
"code": 2880
3986+
},
39833987

39843988
"Import declaration '{0}' is using private name '{1}'.": {
39853989
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
2+
error TS5109: Option 'moduleResolution' must be set to 'Node16' (or left unspecified) when option 'module' is set to 'Node18'.
3+
4+
5+
!!! error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
6+
!!! error TS5109: Option 'moduleResolution' must be set to 'Node16' (or left unspecified) when option 'module' is set to 'Node18'.
7+
==== /app/test.ts (0 errors) ====
8+
import { test } from '../lib';
9+
10+
==== /lib/package.json (0 errors) ====
11+
{
12+
"main": "./cjs/index.js"
13+
}
14+
15+
==== /lib/cjs/index.js (0 errors) ====
16+
export function test() {}
17+
18+
==== /lib/cjs/index.d.ts (0 errors) ====
19+
export function test(): void;
20+

tests/baselines/reference/bundlerDirectoryModule(module=node18,moduleresolution=bundler).trace.json

+827
Large diffs are not rendered by default.

tests/baselines/reference/bundlerDirectoryModule(module=node18,moduleresolution=nodenext).trace.json

+957
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
2+
3+
4+
==== index.ts (1 errors) ====
5+
import foo = require("./foo.ts"); // Error
6+
~~~~~~~~~~
7+
!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
8+
import type _foo = require("./foo.ts"); // Ok
9+
10+
==== foo.ts/index.ts (0 errors) ====
11+
export = {};
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/conformance/externalModules/rewriteRelativeImportExtensions/cjsErrors.ts] ////
2+
3+
//// [index.ts]
4+
export = {};
5+
6+
//// [index.ts]
7+
import foo = require("./foo.ts"); // Error
8+
import type _foo = require("./foo.ts"); // Ok
9+
10+
11+
//// [index.js]
12+
"use strict";
13+
module.exports = {};
14+
//// [index.js]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
const foo = require("./foo.js"); // Error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
main.ts(1,18): error TS2307: Cannot find module './file.js' or its corresponding type declarations.
2+
main.ts(2,18): error TS2307: Cannot find module './file.jsx' or its corresponding type declarations.
3+
main.ts(3,18): error TS2307: Cannot find module './file.ts' or its corresponding type declarations.
4+
main.ts(4,18): error TS2307: Cannot find module './file.tsx' or its corresponding type declarations.
5+
main.ts(5,18): error TS2307: Cannot find module './file.mjs' or its corresponding type declarations.
6+
main.ts(6,18): error TS2307: Cannot find module './file.cjs' or its corresponding type declarations.
7+
main.ts(7,18): error TS2307: Cannot find module './file.mts' or its corresponding type declarations.
8+
main.ts(8,18): error TS2307: Cannot find module './file.cts' or its corresponding type declarations.
9+
main.ts(9,18): error TS2307: Cannot find module './file.d.ts' or its corresponding type declarations.
10+
main.ts(10,19): error TS2307: Cannot find module './file.d.cts' or its corresponding type declarations.
11+
main.ts(11,19): error TS2307: Cannot find module './file.d.mts' or its corresponding type declarations.
12+
main.ts(12,19): error TS2307: Cannot find module './file.d.json.ts' or its corresponding type declarations.
13+
14+
15+
==== package.json (0 errors) ====
16+
{"type": "module"}
17+
==== main.ts (12 errors) ====
18+
import def1 from "./file.js";
19+
~~~~~~~~~~~
20+
!!! error TS2307: Cannot find module './file.js' or its corresponding type declarations.
21+
import def2 from "./file.jsx";
22+
~~~~~~~~~~~~
23+
!!! error TS2307: Cannot find module './file.jsx' or its corresponding type declarations.
24+
import def3 from "./file.ts";
25+
~~~~~~~~~~~
26+
!!! error TS2307: Cannot find module './file.ts' or its corresponding type declarations.
27+
import def4 from "./file.tsx";
28+
~~~~~~~~~~~~
29+
!!! error TS2307: Cannot find module './file.tsx' or its corresponding type declarations.
30+
import def5 from "./file.mjs";
31+
~~~~~~~~~~~~
32+
!!! error TS2307: Cannot find module './file.mjs' or its corresponding type declarations.
33+
import def6 from "./file.cjs";
34+
~~~~~~~~~~~~
35+
!!! error TS2307: Cannot find module './file.cjs' or its corresponding type declarations.
36+
import def7 from "./file.mts";
37+
~~~~~~~~~~~~
38+
!!! error TS2307: Cannot find module './file.mts' or its corresponding type declarations.
39+
import def8 from "./file.cts";
40+
~~~~~~~~~~~~
41+
!!! error TS2307: Cannot find module './file.cts' or its corresponding type declarations.
42+
import def9 from "./file.d.ts";
43+
~~~~~~~~~~~~~
44+
!!! error TS2307: Cannot find module './file.d.ts' or its corresponding type declarations.
45+
import def10 from "./file.d.cts";
46+
~~~~~~~~~~~~~~
47+
!!! error TS2307: Cannot find module './file.d.cts' or its corresponding type declarations.
48+
import def11 from "./file.d.mts";
49+
~~~~~~~~~~~~~~
50+
!!! error TS2307: Cannot find module './file.d.mts' or its corresponding type declarations.
51+
import def12 from "./file.d.json.ts";
52+
~~~~~~~~~~~~~~~~~~
53+
!!! error TS2307: Cannot find module './file.d.json.ts' or its corresponding type declarations.
54+
==== file.d.js.ts (0 errors) ====
55+
declare var bad: "bad1";
56+
export default bad;
57+
==== file.d.jsx.ts (0 errors) ====
58+
declare var bad: "bad2";
59+
export default bad;
60+
==== file.d.ts.ts (0 errors) ====
61+
declare var bad: "bad3";
62+
export default bad;
63+
==== file.d.tsx.ts (0 errors) ====
64+
declare var bad: "bad4";
65+
export default bad;
66+
==== file.d.mjs.ts (0 errors) ====
67+
declare var bad: "bad5";
68+
export default bad;
69+
==== file.d.cjs.ts (0 errors) ====
70+
declare var bad: "bad6";
71+
export default bad;
72+
==== file.d.mts.ts (0 errors) ====
73+
declare var bad: "bad7";
74+
export default bad;
75+
==== file.d.cts.ts (0 errors) ====
76+
declare var bad: "bad8";
77+
export default bad;
78+
==== file.d.d.ts.ts (0 errors) ====
79+
declare var bad: "bad9";
80+
export default bad;
81+
==== file.d.d.mts.ts (0 errors) ====
82+
declare var bad: "bad10";
83+
export default bad;
84+
==== file.d.d.cts.ts (0 errors) ====
85+
declare var bad: "bad11";
86+
export default bad;
87+
==== file.d.d.json.ts (0 errors) ====
88+
declare var bad: "bad12";
89+
export default bad;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [tests/cases/conformance/nonjsExtensions/declarationFileForTsJsImport.ts] ////
2+
3+
//// [package.json]
4+
{"type": "module"}
5+
//// [main.ts]
6+
import def1 from "./file.js";
7+
import def2 from "./file.jsx";
8+
import def3 from "./file.ts";
9+
import def4 from "./file.tsx";
10+
import def5 from "./file.mjs";
11+
import def6 from "./file.cjs";
12+
import def7 from "./file.mts";
13+
import def8 from "./file.cts";
14+
import def9 from "./file.d.ts";
15+
import def10 from "./file.d.cts";
16+
import def11 from "./file.d.mts";
17+
import def12 from "./file.d.json.ts";
18+
//// [file.d.js.ts]
19+
declare var bad: "bad1";
20+
export default bad;
21+
//// [file.d.jsx.ts]
22+
declare var bad: "bad2";
23+
export default bad;
24+
//// [file.d.ts.ts]
25+
declare var bad: "bad3";
26+
export default bad;
27+
//// [file.d.tsx.ts]
28+
declare var bad: "bad4";
29+
export default bad;
30+
//// [file.d.mjs.ts]
31+
declare var bad: "bad5";
32+
export default bad;
33+
//// [file.d.cjs.ts]
34+
declare var bad: "bad6";
35+
export default bad;
36+
//// [file.d.mts.ts]
37+
declare var bad: "bad7";
38+
export default bad;
39+
//// [file.d.cts.ts]
40+
declare var bad: "bad8";
41+
export default bad;
42+
//// [file.d.d.ts.ts]
43+
declare var bad: "bad9";
44+
export default bad;
45+
//// [file.d.d.mts.ts]
46+
declare var bad: "bad10";
47+
export default bad;
48+
//// [file.d.d.cts.ts]
49+
declare var bad: "bad11";
50+
export default bad;
51+
//// [file.d.d.json.ts]
52+
declare var bad: "bad12";
53+
export default bad;
54+
55+
//// [main.js]
56+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//// [tests/cases/conformance/nonjsExtensions/declarationFileForTsJsImport.ts] ////
2+
3+
=== main.ts ===
4+
import def1 from "./file.js";
5+
>def1 : Symbol(def1, Decl(main.ts, 0, 6))
6+
7+
import def2 from "./file.jsx";
8+
>def2 : Symbol(def2, Decl(main.ts, 1, 6))
9+
10+
import def3 from "./file.ts";
11+
>def3 : Symbol(def3, Decl(main.ts, 2, 6))
12+
13+
import def4 from "./file.tsx";
14+
>def4 : Symbol(def4, Decl(main.ts, 3, 6))
15+
16+
import def5 from "./file.mjs";
17+
>def5 : Symbol(def5, Decl(main.ts, 4, 6))
18+
19+
import def6 from "./file.cjs";
20+
>def6 : Symbol(def6, Decl(main.ts, 5, 6))
21+
22+
import def7 from "./file.mts";
23+
>def7 : Symbol(def7, Decl(main.ts, 6, 6))
24+
25+
import def8 from "./file.cts";
26+
>def8 : Symbol(def8, Decl(main.ts, 7, 6))
27+
28+
import def9 from "./file.d.ts";
29+
>def9 : Symbol(def9, Decl(main.ts, 8, 6))
30+
31+
import def10 from "./file.d.cts";
32+
>def10 : Symbol(def10, Decl(main.ts, 9, 6))
33+
34+
import def11 from "./file.d.mts";
35+
>def11 : Symbol(def11, Decl(main.ts, 10, 6))
36+
37+
import def12 from "./file.d.json.ts";
38+
>def12 : Symbol(def12, Decl(main.ts, 11, 6))
39+
40+
=== file.d.js.ts ===
41+
declare var bad: "bad1";
42+
>bad : Symbol(bad, Decl(file.d.js.ts, 0, 11))
43+
44+
export default bad;
45+
>bad : Symbol(bad, Decl(file.d.js.ts, 0, 11))
46+
47+
=== file.d.jsx.ts ===
48+
declare var bad: "bad2";
49+
>bad : Symbol(bad, Decl(file.d.jsx.ts, 0, 11))
50+
51+
export default bad;
52+
>bad : Symbol(bad, Decl(file.d.jsx.ts, 0, 11))
53+
54+
=== file.d.ts.ts ===
55+
declare var bad: "bad3";
56+
>bad : Symbol(bad, Decl(file.d.ts.ts, 0, 11))
57+
58+
export default bad;
59+
>bad : Symbol(bad, Decl(file.d.ts.ts, 0, 11))
60+
61+
=== file.d.tsx.ts ===
62+
declare var bad: "bad4";
63+
>bad : Symbol(bad, Decl(file.d.tsx.ts, 0, 11))
64+
65+
export default bad;
66+
>bad : Symbol(bad, Decl(file.d.tsx.ts, 0, 11))
67+
68+
=== file.d.mjs.ts ===
69+
declare var bad: "bad5";
70+
>bad : Symbol(bad, Decl(file.d.mjs.ts, 0, 11))
71+
72+
export default bad;
73+
>bad : Symbol(bad, Decl(file.d.mjs.ts, 0, 11))
74+
75+
=== file.d.cjs.ts ===
76+
declare var bad: "bad6";
77+
>bad : Symbol(bad, Decl(file.d.cjs.ts, 0, 11))
78+
79+
export default bad;
80+
>bad : Symbol(bad, Decl(file.d.cjs.ts, 0, 11))
81+
82+
=== file.d.mts.ts ===
83+
declare var bad: "bad7";
84+
>bad : Symbol(bad, Decl(file.d.mts.ts, 0, 11))
85+
86+
export default bad;
87+
>bad : Symbol(bad, Decl(file.d.mts.ts, 0, 11))
88+
89+
=== file.d.cts.ts ===
90+
declare var bad: "bad8";
91+
>bad : Symbol(bad, Decl(file.d.cts.ts, 0, 11))
92+
93+
export default bad;
94+
>bad : Symbol(bad, Decl(file.d.cts.ts, 0, 11))
95+
96+
=== file.d.d.ts.ts ===
97+
declare var bad: "bad9";
98+
>bad : Symbol(bad, Decl(file.d.d.ts.ts, 0, 11))
99+
100+
export default bad;
101+
>bad : Symbol(bad, Decl(file.d.d.ts.ts, 0, 11))
102+
103+
=== file.d.d.mts.ts ===
104+
declare var bad: "bad10";
105+
>bad : Symbol(bad, Decl(file.d.d.mts.ts, 0, 11))
106+
107+
export default bad;
108+
>bad : Symbol(bad, Decl(file.d.d.mts.ts, 0, 11))
109+
110+
=== file.d.d.cts.ts ===
111+
declare var bad: "bad11";
112+
>bad : Symbol(bad, Decl(file.d.d.cts.ts, 0, 11))
113+
114+
export default bad;
115+
>bad : Symbol(bad, Decl(file.d.d.cts.ts, 0, 11))
116+
117+
=== file.d.d.json.ts ===
118+
declare var bad: "bad12";
119+
>bad : Symbol(bad, Decl(file.d.d.json.ts, 0, 11))
120+
121+
export default bad;
122+
>bad : Symbol(bad, Decl(file.d.d.json.ts, 0, 11))
123+

0 commit comments

Comments
 (0)