fix(es/typescript): Correctly handle ESM context (#9490)

**Related issue:**

- https://github.com/nodejs/node/issues/54514
This commit is contained in:
magic-akari 2024-08-23 21:40:28 +08:00 committed by GitHub
parent 1e1b165181
commit fc0483ce1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
52 changed files with 108 additions and 225 deletions

View File

@ -0,0 +1,6 @@
---
swc_ecma_transforms_typescript: patch
swc_core: patch
---
fix(es/typescript): Correctly handle ESM context

View File

@ -37,13 +37,9 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var i;
var x = {};
var y = false;

View File

@ -26,10 +26,5 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
Object.defineProperty(exports, "__esModule", {
value: !0
});
});
"require"
], function(require) {});

View File

@ -9,4 +9,3 @@
//! `----
//// [/project/index.ts]
function handleError(err) {}
export { };

View File

@ -8,4 +8,3 @@
//! 3 |
//! `----
//// [/project/index.ts]
export { };

View File

@ -11,4 +11,3 @@ var M;
})(M || (M = {}));
var myA = M.A;
new myA;
export { };

View File

@ -32,9 +32,6 @@ var E1;
})(E1 || (E1 = {}));
//// [foo_1.ts]
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var i;
var x = {};
var y = false;

View File

@ -19,6 +19,3 @@ var E1, E11, _class_call_check = require("@swc/helpers/_/_class_call_check"), C1
};
C1.s1 = !0, (E1 = E11 || (E11 = {}))[E1.A = 0] = "A", E1[E1.B = 1] = "B", E1[E1.C = 2] = "C";
//// [foo_1.ts]
Object.defineProperty(exports, "__esModule", {
value: !0
});

View File

@ -7,13 +7,9 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var x;
x("test");
x(42);

View File

@ -6,11 +6,8 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
var x;
Object.defineProperty(exports, "__esModule", {
value: !0
}), x("test"), x(42), x.b, x.c, x.d;
x("test"), x(42), x.b, x.c, x.d;
});

View File

@ -41,4 +41,3 @@ function fundule() {
var p;
var p;
var p;
export { };

View File

@ -23,13 +23,9 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var answer = 42; // No exports
});
//// [foo_2.ts]

View File

@ -19,13 +19,8 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
Object.defineProperty(exports, "__esModule", {
value: !0
});
});
"require"
], function(require) {});
//// [foo_2.ts]
define([
"require",

View File

@ -54,9 +54,8 @@ export { };
//// [/i.ts]
var h = {};
console.log(h);
export { }; // Error
//// [/j.ts]
export { }; // noUnusedLocals error only
// noUnusedLocals error only
//// [/k.ts]
//! x Export assignment cannot be used when targeting ECMAScript modules. Consider using `export default` or another module format instead.
//! ,-[2:1]

View File

@ -38,9 +38,7 @@ export { };
//! `----
//// [/i.ts]
console.log({});
export { };
//// [/j.ts]
export { };
//// [/k.ts]
//! x Export assignment cannot be used when targeting ECMAScript modules. Consider using `export default` or another module format instead.
//! ,-[2:1]

View File

@ -11,4 +11,3 @@ var E;
E[E["Red"] = 0] = "Red";
E[E["Blue"] = 1] = "Blue";
})(E || (E = {}));
export { };

View File

@ -1,4 +1,4 @@
//// [moduleResolutionWithoutExtension2.ts]
//// [/src/buzz.mts]
// Extensionless relative path cjs import in an ES module
export { }; // should error, should not ask for extension
// should error, should not ask for extension

View File

@ -1,3 +1,2 @@
//// [moduleResolutionWithoutExtension2.ts]
//// [/src/buzz.mts]
export { };

View File

@ -1,4 +1,4 @@
//// [moduleResolutionWithoutExtension7.ts]
//// [/src/bar.cts]
// Extensionless relative path cjs import in a cjs module
export { }; // should error, should not ask for extension
// should error, should not ask for extension

View File

@ -1,3 +1,2 @@
//// [moduleResolutionWithoutExtension7.ts]
//// [/src/bar.cts]
export { };

View File

@ -1,2 +1 @@
//// [parserImportDeclaration1.ts]
export { };

View File

@ -1,2 +1 @@
//// [parserImportDeclaration1.ts]
export { };

View File

@ -1,2 +1 @@
//// [scannerImportDeclaration1.ts]
export { };

View File

@ -1,2 +1 @@
//// [scannerImportDeclaration1.ts]
export { };

View File

@ -1,12 +1,8 @@
///<amd-module name='NamedModule'/>
define("NamedModule", [
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,8 +1,5 @@
///<amd-module name='NamedModule'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,15 +1,10 @@
///<amd-module name='NamedModule'/>
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,13 +1,9 @@
///<amd-module name='FirstModuleName'/>
///<amd-module name='SecondModuleName'/>
define("SecondModuleName", [
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,9 +1,6 @@
///<amd-module name='FirstModuleName'/>
///<amd-module name='SecondModuleName'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,16 +1,11 @@
///<amd-module name='FirstModuleName'/>
///<amd-module name='SecondModuleName'/>
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,12 +1,8 @@
///<AmD-moDulE nAme='NamedModule'/>
define("NamedModule", [
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,8 +1,5 @@
///<AmD-moDulE nAme='NamedModule'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,15 +1,10 @@
///<AmD-moDulE nAme='NamedModule'/>
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,11 +1,7 @@
/*/<amd-module name='should-ignore'/> */ define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,7 +1,4 @@
/*/<amd-module name='should-ignore'/> */ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,14 +1,9 @@
/*/<amd-module name='should-ignore'/> */ (function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){

View File

@ -1,11 +1,7 @@
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
module.exports = foo;
});

View File

@ -1,6 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
module.exports = foo;

View File

@ -1,14 +1,9 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
module.exports = foo;
});

View File

@ -1,10 +1,6 @@
define([
"require",
"exports"
], function(require, exports1) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports1, "__esModule", {
value: true
});
const foo = exports.foo = require("foo");
});

View File

@ -1,5 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = exports.foo = require("foo");

View File

@ -1,13 +1,8 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports1) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports1, "__esModule", {
value: true
});
const foo = exports.foo = require("foo");
});

View File

@ -1,11 +1,7 @@
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
console.log(1);
});
// print b

View File

@ -1,6 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
console.log(1);
// print b

View File

@ -1,14 +1,9 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
console.log(1);
});
// print b

View File

@ -1,11 +1,7 @@
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const assert = require("assert");
assert(true);
});

View File

@ -1,6 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const assert = require("assert");
assert(true);

View File

@ -1,14 +1,9 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const assert = require("assert");
assert(true);
});

View File

@ -1,11 +1,7 @@
define([
"require",
"exports"
], function(require, exports) {
"require"
], function(require) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
foo.bar = 1;
foo.bar = 2;

View File

@ -1,7 +1,4 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
foo.bar = 1;
foo.bar = 2;

View File

@ -1,14 +1,9 @@
(function(global, factory) {
if (typeof module === "object" && typeof module.exports === "object") factory(exports);
else if (typeof define === "function" && define.amd) define([
"exports"
], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {});
})(this, function(exports) {
if (typeof module === "object" && typeof module.exports === "object") factory();
else if (typeof define === "function" && define.amd) define([], factory);
else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory();
})(this, function() {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
foo.bar = 1;
foo.bar = 2;

View File

@ -90,12 +90,12 @@ impl TypeScript {
n.body
.iter()
.rev()
.find(|m| m.is_module_decl())
.find(|m| m.is_es_module_decl())
.map(Spanned::span)
}
fn restore_esm_ctx(n: &mut Module, span: Span) {
if n.body.iter().any(ModuleItem::is_module_decl) {
if n.body.iter().any(ModuleItem::is_es_module_decl) {
return;
}
@ -109,6 +109,36 @@ impl TypeScript {
}
}
trait EsModuleDecl {
fn is_es_module_decl(&self) -> bool;
}
impl EsModuleDecl for ModuleDecl {
fn is_es_module_decl(&self) -> bool {
// Do not use `matches!`
// We should cover all cases explicitly.
match self {
ModuleDecl::Import(..)
| ModuleDecl::ExportDecl(..)
| ModuleDecl::ExportNamed(..)
| ModuleDecl::ExportDefaultDecl(..)
| ModuleDecl::ExportDefaultExpr(..)
| ModuleDecl::ExportAll(..) => true,
ModuleDecl::TsImportEquals(..)
| ModuleDecl::TsExportAssignment(..)
| ModuleDecl::TsNamespaceExport(..) => false,
}
}
}
impl EsModuleDecl for ModuleItem {
fn is_es_module_decl(&self) -> bool {
self.as_module_decl()
.map_or(false, ModuleDecl::is_es_module_decl)
}
}
pub fn tsx<C>(
cm: Lrc<SourceMap>,
config: Config,