Skip to content

Commit 1f917f4

Browse files
authored
refactor(minifier): remove unecessary unfolds, for parentized and ts exprs (#27297)
Minifier expects that input code is a JS with TS already striped away, in many places we directly assert if we encounter TS nodes - `get_inner_expression` - unecessary unfolds ts and parentized expressions https://github.com/oxc-project/oxc/blob/7f65b757df7c3225320d8f7e051e64873f80f988/crates/oxc_ast/src/ast_impl/js.rs#L236-L250 - `without_parentheses` - unfolds parentized expression, witch should not be preset after normalization phase as we remove all of them https://github.com/oxc-project/oxc/blob/7f65b757df7c3225320d8f7e051e64873f80f988/crates/oxc_minifier/src/peephole/normalize.rs#L134-L140 https://github.com/oxc-project/oxc/blob/7f65b757df7c3225320d8f7e051e64873f80f988/crates/oxc_ast/src/ast_impl/js.rs#L161-L167 this is a follow up on #26544 (comment)
1 parent 4dce2c2 commit 1f917f4

5 files changed

Lines changed: 11 additions & 12 deletions

File tree

‎crates/oxc_minifier/src/peephole/fold_constants.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a> PeepholeOptimizations {
261261
/// filters anyway.
262262
fn is_cheap_to_number_operand(e: &Expression<'a>) -> bool {
263263
matches!(
264-
e.get_inner_expression(),
264+
e,
265265
Expression::NumericLiteral(_)
266266
| Expression::StringLiteral(_)
267267
| Expression::BooleanLiteral(_)
@@ -411,7 +411,7 @@ impl<'a> PeepholeOptimizations {
411411
/// Lower bound for the minified size of a numeric expression. Parentheses are deliberately
412412
/// omitted, so accepting a fold based on this count cannot make the output longer.
413413
fn numeric_expression_size_lower_bound(expr: &Expression<'a>) -> Option<usize> {
414-
match expr.get_inner_expression() {
414+
match expr {
415415
Expression::NumericLiteral(lit) => Self::number_literal_source_len(lit.value),
416416
Expression::UnaryExpression(e)
417417
if matches!(
@@ -484,7 +484,7 @@ impl<'a> PeepholeOptimizations {
484484
expr: &Expression<'a>,
485485
ctx: &TraverseCtx<'a>,
486486
) -> Option<(usize, bool)> {
487-
let result = match expr.get_inner_expression() {
487+
let result = match expr {
488488
Expression::StringLiteral(lit) => (lit.value.len() + 2, false),
489489
Expression::NumericLiteral(lit) => (Self::number_literal_source_len(lit.value)?, false),
490490
Expression::BooleanLiteral(_) => (2, false),
@@ -1063,7 +1063,7 @@ fn try_fold_chain_at_element<'a>(
10631063
}
10641064

10651065
fn try_fold_chain_at_expr<'a>(expr: &mut Expression<'a>, ctx: &TraverseCtx<'a>) -> ChainFold<'a> {
1066-
match expr.get_inner_expression_mut() {
1066+
match expr {
10671067
Expression::CallExpression(c) => try_fold_call_expression(c, ctx),
10681068
match_member_expression!(Expression) => {
10691069
try_fold_member_expression(expr.to_member_expression_mut(), ctx)
@@ -1161,7 +1161,7 @@ fn try_fold_at_optional<'a>(
11611161
/// [`cjs-module-lexer`]: https://github.com/nodejs/cjs-module-lexer
11621162
/// [esbuild]: https://github.com/evanw/esbuild/blob/v0.28.0/internal/linker/linker.go#L5127-L5138
11631163
pub(super) fn is_cjs_module_exports_hint(expr: &Expression<'_>) -> bool {
1164-
let Expression::AssignmentExpression(assign) = expr.get_inner_expression() else {
1164+
let Expression::AssignmentExpression(assign) = expr else {
11651165
return false;
11661166
};
11671167
assign.operator == AssignmentOperator::Assign

‎crates/oxc_minifier/src/peephole/minimize_logical_expression.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'a> PeepholeOptimizations {
320320
AssignmentTarget::PrivateFieldExpression(e) => &e.object,
321321
_ => return,
322322
};
323-
if let Expression::Identifier(ident) = object.get_inner_expression()
323+
if let Expression::Identifier(ident) = object
324324
&& let Some(symbol_id) = ctx.scoping().get_reference(ident.reference_id()).symbol_id()
325325
{
326326
ctx.state.symbols.record_member_write_effect(symbol_id, MemberWriteEffect::Hazard);

‎crates/oxc_minifier/src/peephole/remove_unused_declaration.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a> PeepholeOptimizations {
7777
if !ctx.is_expression_whose_name_needs_to_be_kept(init) {
7878
return false;
7979
}
80-
let Expression::ClassExpression(class) = init.without_parentheses() else {
80+
let Expression::ClassExpression(class) = init else {
8181
return false;
8282
};
8383
matches!(Self::classify_class_removability(class, ctx), ClassRemovability::Keep)

‎crates/oxc_minifier/src/peephole/remove_unused_expression.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,10 +1097,9 @@ impl<'a> PeepholeOptimizations {
10971097
// Unwrap parens and sequence tails — `(0, x)` — so the
10981098
// classification does not change when a later fold surfaces
10991099
// the inner expression.
1100-
let mut e = super_class.get_inner_expression();
1101-
while let Expression::SequenceExpression(seq) = e {
1102-
let Some(last) = seq.expressions.last() else { break };
1103-
e = last.get_inner_expression();
1100+
let mut e = super_class;
1101+
if let Expression::SequenceExpression(seq) = e {
1102+
e = Self::last_expression_in_sequence(seq).unwrap_or(e);
11041103
}
11051104
// Keep the existing statically-provable TypeError for literal
11061105
// arrow heritage: `class C extends (() => {}) {}`.

‎crates/oxc_minifier/src/traverse_context/ecma_context.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'a> TraverseCtx<'a, MinifierState<'a>> {
429429
if !expr.is_anonymous_function_definition() {
430430
return false;
431431
}
432-
let is_class = matches!(expr.without_parentheses(), Expression::ClassExpression(_));
432+
let is_class = matches!(expr, Expression::ClassExpression(_));
433433
(options.class && is_class) || (options.function && !is_class)
434434
}
435435

0 commit comments

Comments
 (0)