fix(es/transforms/module): Fix overriding of export * (#2883)

swc_ecma_transforms_module:
 - Emit correct value for `_exportNames`. (Closes #2101)
This commit is contained in:
Jeremy Grieshop 2021-11-28 04:08:02 -05:00 committed by GitHub
parent 875c421259
commit 6f05c101f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 105 additions and 16 deletions

View File

@ -2,6 +2,9 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
render: true
};
Object.defineProperty(exports, "render", {
enumerable: true,
get: function() {
@ -12,6 +15,7 @@ var _customRender = require("./customRender");
var _react = _interopRequireWildcard(require("@testing-library/react"));
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,

View File

@ -89,6 +89,31 @@ where
// Used only if export * exists
let mut exported_names: Option<Ident> = None;
// make a preliminary pass through to collect exported names ahead of time
for item in &items {
match item {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
ref specifiers,
..
})) => {
for ExportNamedSpecifier { orig, exported, .. } in
specifiers.into_iter().filter_map(|e| match e {
ExportSpecifier::Named(e) => Some(e),
_ => None,
})
{
if let Some(exported) = &exported {
exports.push(exported.sym.clone());
} else {
exports.push(orig.sym.clone());
}
}
}
_ => {}
}
}
for item in items {
self.in_top_level = true;
@ -158,21 +183,9 @@ where
ref specifiers,
..
})) => {
// handle: export {sym as alias1, alias2, ...} from "x"
for ExportNamedSpecifier { orig, exported, .. } in
specifiers.into_iter().filter_map(|e| match e {
ExportSpecifier::Named(e) => Some(e),
_ => None,
})
{
if let Some(exported) = &exported {
exports.push(exported.sym.clone());
} else {
exports.push(orig.sym.clone());
}
}
scope.import_to_export(&src, !specifiers.is_empty());
}
_ => {}
}
drop(scope);

View File

@ -4161,6 +4161,9 @@ test!(
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
Scope: true
};
Object.defineProperty(exports, "Scope", {
enumerable: true,
get: function() {
@ -4170,6 +4173,7 @@ Object.defineProperty(exports, "Scope", {
var _http = require("./http");
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,
@ -4200,15 +4204,15 @@ export * from './pipes';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
id: true
};
Object.defineProperty(exports, "id", {
enumerable: true,
get: function() {
return _interfaces.id;
}
});
var _exportNames = {
id: true
};
require("reflect-metadata");
var _http = require("./http");
Object.keys(_http).forEach(function(key) {

View File

@ -0,0 +1,7 @@
import { customRender } from './customRender';
// re-export everything
export * from '@testing-library/react';
// override render method
export { customRender as render };

View File

@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
render: true
};
Object.defineProperty(exports, "render", {
enumerable: true,
get: function() {
return _customRender.customRender;
}
});
var _customRender = require("./customRender");
var _react = require("@testing-library/react");
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() {
return _react[key];
}
});
});

View File

@ -0,0 +1,9 @@
import { customRender } from './customRender';
// override render method
export { customRender as render };
// re-ordering the export wildcard from case 1 should make no difference to output
// re-export everything
export * from '@testing-library/react';

View File

@ -0,0 +1,26 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "render", {
enumerable: true,
get: function() {
return _customRender.customRender;
}
});
var _exportNames = {
render: true
};
var _customRender = require("./customRender");
var _react = require("@testing-library/react");
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() {
return _react[key];
}
});
});