Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48526,6 +48526,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
moduleKind === ModuleKind.Preserve &&
node.kind !== SyntaxKind.ImportEqualsDeclaration &&
node.kind !== SyntaxKind.VariableDeclaration &&
// `const { x } = require(...)` is CommonJS alias syntax (BindingElement), not ESM.
node.kind !== SyntaxKind.BindingElement &&
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
) {
// In `--module preserve`, ESM input syntax emits ESM output syntax, but there will be times
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/file.cjs(11,10): error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.


==== /file.cjs (1 errors) ====
// Destructured and whole-module require are valid CommonJS and must not error under
// --module preserve + --verbatimModuleSyntax (https://github.com/microsoft/TypeScript/issues/63696).
const { x } = require("./mod");
const { y: renamed } = require("./mod");
const whole = require("./mod");
x;
renamed;
whole.y;

// True ESM syntax in a .cjs file remains an error.
import { x as x2 } from "./mod";
~~~~~~~
!!! error TS1293: ECMAScript module syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.

==== /mod.js (0 errors) ====
exports.x = 1;
exports.y = 2;

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/modulePreserveDestructuredRequire.ts] ////

=== /file.cjs ===
// Destructured and whole-module require are valid CommonJS and must not error under
// --module preserve + --verbatimModuleSyntax (https://github.com/microsoft/TypeScript/issues/63696).
const { x } = require("./mod");
>x : Symbol(x, Decl(file.cjs, 2, 7))
>require : Symbol(require)
>"./mod" : Symbol(whole, Decl(mod.js, 0, 0))

const { y: renamed } = require("./mod");
>y : Symbol(renamed, Decl(mod.js, 0, 14))
>renamed : Symbol(renamed, Decl(file.cjs, 3, 7))
>require : Symbol(require)
>"./mod" : Symbol(whole, Decl(mod.js, 0, 0))

const whole = require("./mod");
>whole : Symbol(whole, Decl(file.cjs, 4, 5))
>require : Symbol(require)
>"./mod" : Symbol(whole, Decl(mod.js, 0, 0))

x;
>x : Symbol(x, Decl(file.cjs, 2, 7))

renamed;
>renamed : Symbol(renamed, Decl(file.cjs, 3, 7))

whole.y;
>whole.y : Symbol(renamed, Decl(mod.js, 0, 14))
>whole : Symbol(whole, Decl(file.cjs, 4, 5))
>y : Symbol(renamed, Decl(mod.js, 0, 14))

// True ESM syntax in a .cjs file remains an error.
import { x as x2 } from "./mod";
>x : Symbol(x, Decl(mod.js, 0, 0))
>x2 : Symbol(x2, Decl(file.cjs, 10, 8))

=== /mod.js ===
exports.x = 1;
>exports.x : Symbol(x, Decl(mod.js, 0, 0))
>exports : Symbol(x, Decl(mod.js, 0, 0))
>x : Symbol(x, Decl(mod.js, 0, 0))

exports.y = 2;
>exports.y : Symbol(y, Decl(mod.js, 0, 14))
>exports : Symbol(y, Decl(mod.js, 0, 14))
>y : Symbol(y, Decl(mod.js, 0, 14))

85 changes: 85 additions & 0 deletions tests/baselines/reference/modulePreserveDestructuredRequire.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//// [tests/cases/compiler/modulePreserveDestructuredRequire.ts] ////

=== /file.cjs ===
// Destructured and whole-module require are valid CommonJS and must not error under
// --module preserve + --verbatimModuleSyntax (https://github.com/microsoft/TypeScript/issues/63696).
const { x } = require("./mod");
>x : 1
> : ^
>require("./mod") : typeof whole
> : ^^^^^^^^^^^^
>require : any
> : ^^^
>"./mod" : "./mod"
> : ^^^^^^^

const { y: renamed } = require("./mod");
>y : any
> : ^^^
>renamed : 2
> : ^
>require("./mod") : typeof whole
> : ^^^^^^^^^^^^
>require : any
> : ^^^
>"./mod" : "./mod"
> : ^^^^^^^

const whole = require("./mod");
>whole : typeof whole
> : ^^^^^^^^^^^^
>require("./mod") : typeof whole
> : ^^^^^^^^^^^^
>require : any
> : ^^^
>"./mod" : "./mod"
> : ^^^^^^^

x;
>x : 1
> : ^

renamed;
>renamed : 2
> : ^

whole.y;
>whole.y : 2
> : ^
>whole : typeof whole
> : ^^^^^^^^^^^^
>y : 2
> : ^

// True ESM syntax in a .cjs file remains an error.
import { x as x2 } from "./mod";
>x : 1
> : ^
>x2 : 1
> : ^

=== /mod.js ===
exports.x = 1;
>exports.x = 1 : 1
> : ^
>exports.x : 1
> : ^
>exports : typeof import("./mod")
> : ^^^^^^^^^^^^^^^^^^^^^^
>x : 1
> : ^
>1 : 1
> : ^

exports.y = 2;
>exports.y = 2 : 2
> : ^
>exports.y : 2
> : ^
>exports : typeof import("./mod")
> : ^^^^^^^^^^^^^^^^^^^^^^
>y : 2
> : ^
>2 : 2
> : ^

23 changes: 23 additions & 0 deletions tests/cases/compiler/modulePreserveDestructuredRequire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @module: preserve
// @target: esnext
// @verbatimModuleSyntax: true
// @checkJs: true
// @noEmit: true
// @strict: true

// @Filename: /mod.js
exports.x = 1;
exports.y = 2;

// @Filename: /file.cjs
// Destructured and whole-module require are valid CommonJS and must not error under
// --module preserve + --verbatimModuleSyntax (https://github.com/microsoft/TypeScript/issues/63696).
const { x } = require("./mod");
const { y: renamed } = require("./mod");
const whole = require("./mod");
x;
renamed;
whole.y;

// True ESM syntax in a .cjs file remains an error.
import { x as x2 } from "./mod";