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:
OJ Kwon 2021-11-10 00:33:44 -08:00 committed by GitHub
parent 4f70ee6d98
commit 691e5e581c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -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(),

View File

@ -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;
},
});

View File

@ -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;
}
});