Skip to content

Commit

Permalink
fix(es/typescript): Correctly handle ESM context (#9490)
Browse files Browse the repository at this point in the history
**Related issue:**

- nodejs/node#54514
  • Loading branch information
magic-akari committed Aug 23, 2024
1 parent 1e1b165 commit fc0483c
Show file tree
Hide file tree
Showing 52 changed files with 108 additions and 225 deletions.
6 changes: 6 additions & 0 deletions .changeset/nasty-trees-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_transforms_typescript: patch
swc_core: patch
---

fix(es/typescript): Correctly handle ESM context
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,5 @@ define([
});
//// [foo_1.ts]
define([
"require",
"exports"
], function(require, exports) {
Object.defineProperty(exports, "__esModule", {
value: !0
});
});
"require"
], function(require) {});
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
//! `----
//// [/project/index.ts]
function handleError(err) {}
export { };
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
//! 3 |
//! `----
//// [/project/index.ts]
export { };
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ var M;
})(M || (M = {}));
var myA = M.A;
new myA;
export { };
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ function fundule() {
var p;
var p;
var p;
export { };
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ var E;
E[E["Red"] = 0] = "Red";
E[E["Blue"] = 1] = "Blue";
})(E || (E = {}));
export { };
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//// [moduleResolutionWithoutExtension2.ts]
//// [/src/buzz.mts]
export { };
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//// [moduleResolutionWithoutExtension7.ts]
//// [/src/bar.cts]
export { };
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [parserImportDeclaration1.ts]
export { };
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [parserImportDeclaration1.ts]
export { };
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [scannerImportDeclaration1.ts]
export { };
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
//// [scannerImportDeclaration1.ts]
export { };
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
///<amd-module name='NamedModule'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
///<amd-module name='FirstModuleName'/>
///<amd-module name='SecondModuleName'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
///<AmD-moDulE nAme='NamedModule'/>
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/*/<amd-module name='should-ignore'/> */ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
class Foo {
x;
constructor(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const foo = require("foo");
module.exports = foo;
Loading

0 comments on commit fc0483c

Please sign in to comment.