fix(es/minifier): Fix handling of optional chaining when hoist_props is enabled (#7246)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/7228.
This commit is contained in:
Donny/강동윤 2023-04-13 12:23:29 +09:00 committed by GitHub
parent dd56100585
commit a44fea1ec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,64 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": false,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": false,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": false,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": false,
"typeofs": false,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": false,
"const_to_let": false,
"pristine_globals": false
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}

View File

@ -0,0 +1,4 @@
function f() {
const foos = something.getFoos();
return foos?.[0];
}

View File

@ -0,0 +1,3 @@
function f() {
return something.getFoos()?.[0];
}

View File

@ -195,6 +195,10 @@ where
pub(super) fn replace_props(&mut self, e: &mut Expr) {
let member = match e {
Expr::Member(m) => m,
Expr::OptChain(m) => match &mut *m.base {
OptChainBase::Member(m) => m,
_ => return,
},
_ => return,
};
if let Expr::Ident(obj) = &*member.obj {

View File

@ -0,0 +1,47 @@
{
"arguments": false,
"arrows": false,
"booleans": false,
"booleans_as_integers": false,
"collapse_vars": false,
"comparisons": false,
"computed_props": false,
"conditionals": false,
"dead_code": false,
"directives": false,
"drop_console": false,
"drop_debugger": false,
"evaluate": false,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": false,
"join_vars": false,
"keep_classnames": false,
"keep_fargs": false,
"keep_fnames": false,
"keep_infinity": false,
"loops": false,
"negate_iife": false,
"properties": false,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": false,
"switches": false,
"typeofs": false,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": false,
"const_to_let": false,
"pristine_globals": false,
"defaults": true
}

View File

@ -0,0 +1,4 @@
export function f() {
const foos = something.getFoos();
return foos?.[0];
}

View File

@ -0,0 +1,4 @@
export function f() {
const foos = something.getFoos();
return foos?.[0];
}