mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 14:16:12 +03:00
fix(es/transforms/common_js): Allow reassignment to functions exported as default (#2705)
swc_ecma_transforms_module: - Mark a function exported using `default` as reassignable target.
This commit is contained in:
parent
4f70ee6d98
commit
691e5e581c
@ -372,6 +372,21 @@ where
|
||||
// init_export!("default");
|
||||
let ident = ident.unwrap_or_else(|| private_ident!("_default"));
|
||||
|
||||
// bind default exported fn into scope. Note this assigns
|
||||
// syntaxcontext
|
||||
// for the `default` ident, since default export is always named
|
||||
// as `export.default`
|
||||
// instead of actual ident of FnExpr even if it exists.
|
||||
{
|
||||
let mut scope = self.scope.borrow_mut();
|
||||
|
||||
scope
|
||||
.exported_bindings
|
||||
.entry((ident.sym.clone(), ident.span.ctxt()))
|
||||
.or_default()
|
||||
.push((js_word!("default"), DUMMY_SP.ctxt()));
|
||||
}
|
||||
|
||||
extra_stmts.push(ModuleItem::Stmt(Stmt::Decl(Decl::Fn(
|
||||
FnDecl {
|
||||
ident: ident.clone(),
|
||||
|
@ -0,0 +1,21 @@
|
||||
export default function someCall() {
|
||||
throw new Error('this should not be called');
|
||||
}
|
||||
|
||||
export function warn() {
|
||||
throw new Error('this should not be called');
|
||||
}
|
||||
|
||||
export const test = {};
|
||||
Object.defineProperty(test, 'someCall', {
|
||||
set: (v) => {
|
||||
someCall = v;
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperty(test, 'warn', {
|
||||
get: () => warn,
|
||||
set: (v) => {
|
||||
warn = v;
|
||||
},
|
||||
});
|
@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.default = someCall;
|
||||
exports.warn = warn;
|
||||
exports.test = void 0;
|
||||
function someCall() {
|
||||
throw new Error("this should not be called");
|
||||
}
|
||||
function warn() {
|
||||
throw new Error("this should not be called");
|
||||
}
|
||||
const test = {
|
||||
};
|
||||
exports.test = test;
|
||||
Object.defineProperty(test, "someCall", {
|
||||
set: (v)=>{
|
||||
exports.default = someCall = v;
|
||||
}
|
||||
});
|
||||
Object.defineProperty(test, "warn", {
|
||||
get: ()=>warn
|
||||
,
|
||||
set: (v)=>{
|
||||
exports.warn = warn = v;
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user