Skip to content

Commit 7cc51be

Browse files
authored
fix(es/transforms/cjs): Allow reassignment to exported functions (#2569)
swc_ecma_transforms_module: - `common_js`: Reassign exports for the functions. (#2549)
1 parent a6af0ab commit 7cc51be

File tree

6 files changed

+149
-33
lines changed

6 files changed

+149
-33
lines changed

ecmascript/transforms/module/src/amd.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ where
220220

221221
for ident in found.drain(..) {
222222
scope
223-
.exported_vars
223+
.exported_bindings
224224
.entry((ident.sym.clone(), ident.span.ctxt()))
225225
.or_default()
226226
.push((ident.sym.clone(), ident.span.ctxt()));
@@ -361,14 +361,18 @@ where
361361

362362
let key = (orig.sym.clone(), orig.span.ctxt());
363363
if scope.declared_vars.contains(&key) {
364-
scope.exported_vars.entry(key.clone()).or_default().push(
365-
exported
366-
.clone()
367-
.map(|i| (i.sym.clone(), i.span.ctxt()))
368-
.unwrap_or_else(|| {
369-
(orig.sym.clone(), orig.span.ctxt())
370-
}),
371-
);
364+
scope
365+
.exported_bindings
366+
.entry(key.clone())
367+
.or_default()
368+
.push(
369+
exported
370+
.clone()
371+
.map(|i| (i.sym.clone(), i.span.ctxt()))
372+
.unwrap_or_else(|| {
373+
(orig.sym.clone(), orig.span.ctxt())
374+
}),
375+
);
372376
}
373377

374378
if let Some(ref src) = export.src {

ecmascript/transforms/module/src/common_js.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,19 @@ where
184184
_ => unreachable!(),
185185
};
186186

187-
//
188187
extra_stmts.push(ModuleItem::Stmt(Stmt::Decl(decl.fold_with(self))));
189188

189+
if !is_class {
190+
let mut scope = self.scope.borrow_mut();
191+
scope
192+
.exported_bindings
193+
.entry((ident.sym.clone(), ident.span.ctxt()))
194+
.or_default()
195+
.push((ident.sym.clone(), ident.span.ctxt()));
196+
197+
drop(scope);
198+
}
199+
190200
let append_to: &mut Vec<_> = if is_class {
191201
&mut extra_stmts
192202
} else {
@@ -230,7 +240,7 @@ where
230240

231241
for ident in found.drain(..) {
232242
scope
233-
.exported_vars
243+
.exported_bindings
234244
.entry((ident.sym.clone(), ident.span.ctxt()))
235245
.or_default()
236246
.push((ident.sym.clone(), ident.span.ctxt()));
@@ -379,14 +389,18 @@ where
379389

380390
let key = orig.to_id();
381391
if scope.declared_vars.contains(&key) {
382-
scope.exported_vars.entry(key.clone()).or_default().push(
383-
exported
384-
.clone()
385-
.map(|i| (i.sym.clone(), i.span.ctxt()))
386-
.unwrap_or_else(|| {
387-
(orig.sym.clone(), orig.span.ctxt())
388-
}),
389-
);
392+
scope
393+
.exported_bindings
394+
.entry(key.clone())
395+
.or_default()
396+
.push(
397+
exported
398+
.clone()
399+
.map(|i| (i.sym.clone(), i.span.ctxt()))
400+
.unwrap_or_else(|| {
401+
(orig.sym.clone(), orig.span.ctxt())
402+
}),
403+
);
390404
}
391405

392406
if let Some(ref src) = export.src {

ecmascript/transforms/module/src/umd.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ where
230230

231231
for ident in found.drain(..) {
232232
scope
233-
.exported_vars
233+
.exported_bindings
234234
.entry((ident.sym.clone(), ident.span.ctxt()))
235235
.or_default()
236236
.push((ident.sym.clone(), ident.span.ctxt()));
@@ -365,14 +365,18 @@ where
365365
let mut scope_ref_mut = self.scope.borrow_mut();
366366
let scope = &mut *scope_ref_mut;
367367
if scope.declared_vars.contains(&key) {
368-
scope.exported_vars.entry(key.clone()).or_default().push(
369-
exported
370-
.clone()
371-
.map(|i| (i.sym.clone(), i.span.ctxt()))
372-
.unwrap_or_else(|| {
373-
(orig.sym.clone(), orig.span.ctxt())
374-
}),
375-
);
368+
scope
369+
.exported_bindings
370+
.entry(key.clone())
371+
.or_default()
372+
.push(
373+
exported
374+
.clone()
375+
.map(|i| (i.sym.clone(), i.span.ctxt()))
376+
.unwrap_or_else(|| {
377+
(orig.sym.clone(), orig.span.ctxt())
378+
}),
379+
);
376380
}
377381

378382
if let Some(ref src) = export.src {

ecmascript/transforms/module/src/util.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub struct Scope {
121121
/// Declared variables except const.
122122
pub(crate) declared_vars: Vec<Id>,
123123

124-
/// Maps of exported variables.
124+
/// Maps of exported bindings.
125125
///
126126
///
127127
/// e.g.
@@ -130,7 +130,7 @@ pub struct Scope {
130130
///
131131
/// - `export { a as b }`
132132
/// -> `{ a: [b] }`
133-
pub(crate) exported_vars: AHashMap<Id, Vec<Id>>,
133+
pub(crate) exported_bindings: AHashMap<Id, Vec<Id>>,
134134

135135
/// This is required to handle
136136
/// `export * from 'foo';`
@@ -604,7 +604,7 @@ impl Scope {
604604
let arg = arg.ident().unwrap();
605605
let mut scope = folder.scope_mut();
606606
let entry = scope
607-
.exported_vars
607+
.exported_bindings
608608
.entry((arg.sym.clone(), arg.span.ctxt()));
609609

610610
match entry {
@@ -744,7 +744,7 @@ impl Scope {
744744
let i = pat.ident().unwrap();
745745
let mut scope = folder.scope_mut();
746746
let entry = scope
747-
.exported_vars
747+
.exported_bindings
748748
.entry((i.id.sym.clone(), i.id.span.ctxt()));
749749

750750
match entry {
@@ -772,7 +772,7 @@ impl Scope {
772772
.filter_map(|i| {
773773
let mut scope = folder.scope_mut();
774774
let entry = match scope
775-
.exported_vars
775+
.exported_bindings
776776
.entry((i.sym.clone(), i.span.ctxt()))
777777
{
778778
Entry::Occupied(entry) => entry,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function log() {
2+
console.log('unexported');
3+
}
4+
5+
export function noassign() {
6+
console.log('stub');
7+
}
8+
9+
export function warn() {
10+
throw new Error('this should not be called');
11+
}
12+
13+
export const errors = {
14+
a: 1
15+
};
16+
17+
export const addOne = (x) => `${x + 1}`;
18+
export const someFunc = (x) => `The answer is : ${addOne(x)}`;
19+
20+
export const test = {};
21+
22+
Object.defineProperty(test, 'log', {
23+
get: function get() {
24+
return log;
25+
},
26+
set: function set(v) {
27+
log = v;
28+
}
29+
});
30+
31+
Object.defineProperty(test, 'warn', {
32+
get: () => warn,
33+
set: (v) => {
34+
warn = v;
35+
},
36+
});
37+
38+
Object.defineProperty(test, 'errors', {
39+
get: () => errors,
40+
set: (v) => {
41+
errors = v;
42+
},
43+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", {
3+
value: true
4+
});
5+
exports.noassign = noassign;
6+
exports.warn = warn;
7+
exports.test = exports.someFunc = exports.addOne = exports.errors = void 0;
8+
function log() {
9+
console.log("unexported");
10+
}
11+
function noassign() {
12+
console.log("stub");
13+
}
14+
function warn() {
15+
throw new Error("this should not be called");
16+
}
17+
const errors = {
18+
a: 1
19+
};
20+
exports.errors = errors;
21+
const addOne = (x)=>`${x + 1}`
22+
;
23+
exports.addOne = addOne;
24+
const someFunc = (x)=>`The answer is : ${addOne(x)}`
25+
;
26+
exports.someFunc = someFunc;
27+
const test = {
28+
};
29+
exports.test = test;
30+
Object.defineProperty(test, "log", {
31+
get: function get() {
32+
return log;
33+
},
34+
set: function set(v) {
35+
log = v;
36+
}
37+
});
38+
Object.defineProperty(test, "warn", {
39+
get: ()=>warn
40+
,
41+
set: (v)=>{
42+
exports.warn = warn = v;
43+
}
44+
});
45+
Object.defineProperty(test, "errors", {
46+
get: ()=>errors
47+
,
48+
set: (v)=>{
49+
exports.errors = errors = v;
50+
}
51+
});

0 commit comments

Comments
 (0)