Skip to content

Commit

Permalink
fix(minifier): fix crash with []['concat'](1) (#8750)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jan 27, 2025
1 parent 0fcff20 commit a3b078a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_minifier/src/peephole/replace_known_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,12 @@ impl<'a> PeepholeOptimizations {
}
}

let Expression::StaticMemberExpression(member) = callee else { unreachable!() };
let Expression::ArrayExpression(array_expr) = &mut member.object else { return None };
let object = match callee {
Expression::StaticMemberExpression(member) => &mut member.object,
Expression::ComputedMemberExpression(member) => &mut member.object,
_ => unreachable!(),
};
let Expression::ArrayExpression(array_expr) = object else { return None };

let can_merge_until = args
.iter()
Expand Down Expand Up @@ -574,7 +578,7 @@ impl<'a> PeepholeOptimizations {
}

if args.is_empty() {
Some(ctx.ast.move_expression(&mut member.object))
Some(ctx.ast.move_expression(object))
} else if can_merge_until.is_some() {
Some(ctx.ast.expression_call(
span,
Expand Down Expand Up @@ -1438,6 +1442,7 @@ mod test {
test("var x; ''.concat(x.a).concat(x)", "var x; ''.concat(x.a, x)"); // x.a might have a getter that updates x, but that side effect is preserved correctly

// other
test("x = []['concat'](1)", "x = [1]");
test_same("obj.concat([1,2]).concat(1)");
}

Expand Down

0 comments on commit a3b078a

Please sign in to comment.