feat(minifier): simplify destructuring array assigment#16580
feat(minifier): simplify destructuring array assigment#16580armano2 wants to merge 56 commits intooxc-project:mainfrom
Conversation
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
…inline-statements
|
ok, all changes applied, and it seem to be working fine |
|
q: what do you think about not processing vars if there is more than one non literal value? // in this case only one variable will be created and we could create it only if its not first
decl.kind.is_var() &&
init_expr.elements.iter().filter(|elem| !elem.is_literal_value(false, ctx)).count() > 1or even do not do that if there is any non literal // in this case we never create variables
decl.kind.is_var() &&
init_expr.elements.iter().any(|elem| !elem.is_literal_value(false, ctx))that would let us handle all cases where we extensively create additional variables for vars this solves issues like: var [a, b] = [1, 2], [a, b] = [b, a] // in
var a = 2, b = 1 // new
var a = 1, _ = 2, _2 = a, a = _, b = _2 // oldvar [a, b] = [!d, !a] // in - out
var _ = !d, _2 = !a, a = _, b = _2 // oldvar [a, b] = [b, a], [a, b] = [b, a] // in - out
var _3 = b, _4 = a, a = _3, b = _4, _ = b, _2 = a, a = _, b = _2 // old |
| if decl.kind.is_var() && init_len > 1 { | ||
| let binding_identifiers = decl.id.get_binding_identifiers(); | ||
| if !binding_identifiers.is_empty() { | ||
| return !init_expr.elements.iter().any(|e| { | ||
| match e.as_expression() { | ||
| Some(Expression::NewExpression(_)) | ||
| | Some(Expression::CallExpression(_)) => true, | ||
| Some(Expression::Identifier(ident)) => { | ||
| let Some(ref_symbol) = | ||
| &ctx.scoping().get_reference(ident.reference_id()).symbol_id() | ||
| else { | ||
| // global reference | ||
| return false; | ||
| }; | ||
| // check whathever id is present in init [a] = [b] | ||
| binding_identifiers.iter().any(|id| id.symbol_id().eq(ref_symbol)) | ||
| } | ||
| None => e.is_spread(), // do not allow spread | ||
| _ => !e.is_literal_value(false, ctx), | ||
| } | ||
| }); | ||
| } | ||
| } |
There was a problem hiding this comment.
maybe we should extract this to something more generic
ideally we would want to resolve it based on nodeId by comparing sub-tree, but that's currently not supported
this produces better output than creating intermediate variables that would otherwise could never be in-lined
simplify destructuring array assignment pattern
use cases in a wild: