Skip to content

Commit

Permalink
fix(es/transforms/module): Fix for duplicate export (#1846)
Browse files Browse the repository at this point in the history
  • Loading branch information
IronLu233 authored Jun 29, 2021
1 parent c49e9b0 commit f8a3df8
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ecmascript/transforms/module/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_module"
repository = "https://github.com/swc-project/swc.git"
version = "0.24.0"
version = "0.24.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
22 changes: 22 additions & 0 deletions ecmascript/transforms/module/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl Scope {
/// ```js
/// Object.keys(_foo).forEach(function (key) {
/// if (key === "default" || key === "__esModule") return;
/// if (key in exports && exports[key] === _foo[key]) return;
/// Object.defineProperty(exports, key, {
/// enumerable: true,
/// get: function () {
Expand Down Expand Up @@ -217,6 +218,27 @@ impl Scope {
None
}
})
.chain({
Some(Stmt::If(IfStmt {
span: DUMMY_SP,
test: Box::new(
key_ident
.clone()
.make_bin(op!("in"), exports.clone())
.make_bin(
op!("&&"),
exports.clone().computed_member(key_ident.clone()).make_eq(
imported.clone().computed_member(key_ident.clone()),
),
),
),
cons: Box::new(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: None,
})),
alt: None,
}))
})
.chain(iter::once(
define_property(vec![
exports.as_arg(),
Expand Down
1 change: 1 addition & 0 deletions ecmascript/transforms/module/tests/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ define(["exports", "foo"], function (_exports, _foo) {
});
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in _exports && _exports[key] === _foo[key]) return;
Object.defineProperty(_exports, key, {
enumerable: true,
get: function () {
Expand Down
12 changes: 12 additions & 0 deletions ecmascript/transforms/module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,7 @@ var _foo = require("foo");
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -941,6 +942,7 @@ var _foo = require("./foo");
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -1257,6 +1259,7 @@ var _black = require("black");
Object.keys(_white).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _white[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand All @@ -1268,6 +1271,7 @@ Object.keys(_white).forEach(function (key) {
Object.keys(_black).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _black[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -2193,6 +2197,7 @@ var _foo = require("foo");
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -3803,6 +3808,7 @@ exports.default = _default;
Object.keys(_react).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _react[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
Expand Down Expand Up @@ -3987,6 +3993,7 @@ Object.defineProperty(exports, '__esModule', {
var _c = require('c');
Object.keys(_c).forEach(function(key) {
if (key === 'default' || key === '__esModule') return;
if (key in exports && exports[key] === _c[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down Expand Up @@ -4171,6 +4178,7 @@ Object.defineProperty(exports, "Scope", {
});
Object.keys(_http).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _http[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down Expand Up @@ -4214,6 +4222,7 @@ export * from './pipes';
Object.keys(_http).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _http[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -4224,6 +4233,7 @@ export * from './pipes';
Object.keys(_pipes).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _pipes[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down Expand Up @@ -4750,6 +4760,7 @@ test!(
Object.keys(_file1).forEach(function(key) {
if (key === 'default' || key === '__esModule') return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _file1[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -4760,6 +4771,7 @@ test!(
Object.keys(_file2).forEach(function(key) {
if (key === 'default' || key === '__esModule') return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _file2[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down
1 change: 1 addition & 0 deletions ecmascript/transforms/module/tests/umd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,7 @@ export * from "foo";
});
Object.keys(_foo).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in _exports && _exports[key] === _foo[key]) return;
Object.defineProperty(_exports, key, {
enumerable: true,
get: function () {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/issue-1859/case1-js/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function _interopRequireWildcard(obj) {
Object.keys(_appConfig).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _appConfig[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -42,6 +43,7 @@ Object.keys(_appConfig).forEach(function(key) {
Object.keys(_databaseConfig).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _databaseConfig[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/issue-1859/case2-ts/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function _interopRequireWildcard(obj) {
Object.keys(_appConfig).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _appConfig[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -42,6 +43,7 @@ Object.keys(_appConfig).forEach(function(key) {
Object.keys(_databaseConfig).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _databaseConfig[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/issue-1859/case3/output/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function _interopRequireWildcard(obj) {
Object.keys(_foo).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -42,6 +43,7 @@ Object.keys(_foo).forEach(function(key) {
Object.keys(_bar).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _bar[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down
2 changes: 2 additions & 0 deletions tests/fixture/issue-1859/case3/output/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function _interopRequireWildcard(obj) {
Object.keys(_foo).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _foo[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand All @@ -42,6 +43,7 @@ Object.keys(_foo).forEach(function(key) {
Object.keys(_bar).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _bar[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
Expand Down

0 comments on commit f8a3df8

Please sign in to comment.