mirror of
https://github.com/swc-project/swc.git
synced 2024-12-28 16:13:34 +03:00
fix(es/modules/cjs): FIx detection of exported names (#4737)
This commit is contained in:
parent
f7e000383c
commit
d9bb59a8cb
12
crates/swc/tests/fixture/issues-4xxx/4700/1/.swcrc
Normal file
12
crates/swc/tests/fixture/issues-4xxx/4700/1/.swcrc
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"module": {
|
||||
"type": "commonjs",
|
||||
"noInterop": true
|
||||
},
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"tsx": true
|
||||
}
|
||||
}
|
||||
}
|
6
crates/swc/tests/fixture/issues-4xxx/4700/1/input.ts
Normal file
6
crates/swc/tests/fixture/issues-4xxx/4700/1/input.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export * from "another-module";
|
||||
|
||||
export function whatever(notExportName: string) {
|
||||
const shouldNotBeExportNameAsWell = 123;
|
||||
return shouldNotBeExportNameAsWell + notExportName;
|
||||
}
|
@ -207,8 +207,20 @@ impl Fold for CommonJs {
|
||||
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
|
||||
let mut found: Vec<Ident> = vec![];
|
||||
|
||||
let mut v = DestructuringFinder { found: &mut found };
|
||||
decl.visit_with(&mut v);
|
||||
match decl {
|
||||
Decl::Class(d) => {
|
||||
found.push(d.ident.clone());
|
||||
}
|
||||
Decl::Fn(d) => {
|
||||
found.push(d.ident.clone());
|
||||
}
|
||||
Decl::Var(v) => {
|
||||
found.extend(find_pat_ids(&v.decls));
|
||||
}
|
||||
_ => {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
for ident in found {
|
||||
exports.push(ident.sym.clone());
|
||||
|
@ -0,0 +1,6 @@
|
||||
export * from "another-module";
|
||||
|
||||
export function whatever(notExportName) {
|
||||
const shouldNotBeExportNameAsWell = 123;
|
||||
return shouldNotBeExportNameAsWell + notExportName;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
var _exportNames = {
|
||||
whatever: true
|
||||
};
|
||||
exports.whatever = whatever;
|
||||
var _anotherModule = require("another-module");
|
||||
Object.keys(_anotherModule).forEach(function(key) {
|
||||
if (key === "default" || key === "__esModule") return;
|
||||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
||||
if (key in exports && exports[key] === _anotherModule[key]) return;
|
||||
Object.defineProperty(exports, key, {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _anotherModule[key];
|
||||
}
|
||||
});
|
||||
});
|
||||
function whatever(notExportName) {
|
||||
const shouldNotBeExportNameAsWell = 123;
|
||||
return shouldNotBeExportNameAsWell + notExportName;
|
||||
}
|
Loading…
Reference in New Issue
Block a user