Skip to content

Commit 194a2ae

Browse files
authored
Key validity of import=require in module: node off of module and not target (#49222)
1 parent 7c6521e commit 194a2ae

14 files changed

+202
-1
lines changed

src/compiler/transformers/module/esnextAnd2015.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace ts {
7272
// Though an error in es2020 modules, in node-flavor es2020 modules, we can helpfully transform this to a synthetic `require` call
7373
// To give easy access to a synchronous `require` in node-flavor esm. We do the transform even in scenarios where we error, but `import.meta.url`
7474
// is available, just because the output is reasonable for a node-like runtime.
75-
return getEmitScriptTarget(compilerOptions) >= ModuleKind.ES2020 ? visitImportEqualsDeclaration(node as ImportEqualsDeclaration) : undefined;
75+
return getEmitModuleKind(compilerOptions) >= ModuleKind.Node16 ? visitImportEqualsDeclaration(node as ImportEqualsDeclaration) : undefined;
7676
case SyntaxKind.ExportAssignment:
7777
return visitExportAssignment(node as ExportAssignment);
7878
case SyntaxKind.ExportDeclaration:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"type": "module"
6+
}
7+
//// [mod.d.ts]
8+
declare module "foo";
9+
//// [index.ts]
10+
/// <reference path="./mod.d.ts" />
11+
// This should emit a call to createRequire(import.meta.url)
12+
import foo = require("foo");
13+
foo;
14+
15+
//// [index.js]
16+
import { createRequire as _createRequire } from "module";
17+
const __require = _createRequire(import.meta.url);
18+
/// <reference path="./mod.d.ts" />
19+
// This should emit a call to createRequire(import.meta.url)
20+
const foo = __require("foo");
21+
foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
6+
7+
foo;
8+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : any
6+
7+
foo;
8+
>foo : any
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : any
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"type": "module"
6+
}
7+
//// [mod.d.ts]
8+
declare module "foo";
9+
//// [index.ts]
10+
/// <reference path="./mod.d.ts" />
11+
// This should emit a call to createRequire(import.meta.url)
12+
import foo = require("foo");
13+
foo;
14+
15+
//// [index.js]
16+
import { createRequire as _createRequire } from "module";
17+
const __require = _createRequire(import.meta.url);
18+
/// <reference path="./mod.d.ts" />
19+
// This should emit a call to createRequire(import.meta.url)
20+
const foo = __require("foo");
21+
foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
6+
7+
foo;
8+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : any
6+
7+
foo;
8+
>foo : any
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : any
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"type": "module"
6+
}
7+
//// [mod.d.ts]
8+
declare module "foo";
9+
//// [index.ts]
10+
/// <reference path="./mod.d.ts" />
11+
// This should emit a call to createRequire(import.meta.url)
12+
import foo = require("foo");
13+
foo;
14+
15+
//// [index.js]
16+
import { createRequire as _createRequire } from "module";
17+
var __require = _createRequire(import.meta.url);
18+
/// <reference path="./mod.d.ts" />
19+
// This should emit a call to createRequire(import.meta.url)
20+
var foo = __require("foo");
21+
foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
6+
7+
foo;
8+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : any
6+
7+
foo;
8+
>foo : any
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : any
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"type": "module"
6+
}
7+
//// [mod.d.ts]
8+
declare module "foo";
9+
//// [index.ts]
10+
/// <reference path="./mod.d.ts" />
11+
// This should emit a call to createRequire(import.meta.url)
12+
import foo = require("foo");
13+
foo;
14+
15+
//// [index.js]
16+
import { createRequire as _createRequire } from "module";
17+
const __require = _createRequire(import.meta.url);
18+
/// <reference path="./mod.d.ts" />
19+
// This should emit a call to createRequire(import.meta.url)
20+
const foo = __require("foo");
21+
foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
6+
7+
foo;
8+
>foo : Symbol(foo, Decl(index.ts, 0, 0))
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/index.ts ===
2+
/// <reference path="./mod.d.ts" />
3+
// This should emit a call to createRequire(import.meta.url)
4+
import foo = require("foo");
5+
>foo : any
6+
7+
foo;
8+
>foo : any
9+
10+
=== tests/cases/compiler/mod.d.ts ===
11+
declare module "foo";
12+
>"foo" : any
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @target: es5,es2016,es2020,esnext
2+
// @module: nodenext
3+
// @filename: package.json
4+
{
5+
"type": "module"
6+
}
7+
// @filename: mod.d.ts
8+
declare module "foo";
9+
// @filename: index.ts
10+
/// <reference path="./mod.d.ts" />
11+
// This should emit a call to createRequire(import.meta.url)
12+
import foo = require("foo");
13+
foo;

0 commit comments

Comments
 (0)