Skip to content

Commit

Permalink
refactor(minifier): extract extract_id_or_assign_to_id method
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Feb 1, 2025
1 parent b00b8c8 commit 357135a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
16 changes: 16 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ impl<'a> Expression<'a> {
}
}

/// Returns the identifier or the assignment target's identifier of the given expression.
pub fn extract_id_or_assign_to_id(&self) -> Option<&IdentifierReference<'a>> {
match self {
Expression::Identifier(id) => Some(id),
Expression::AssignmentExpression(assign_expr) => {
if assign_expr.operator == AssignmentOperator::Assign {
if let AssignmentTarget::AssignmentTargetIdentifier(id) = &assign_expr.left {
return Some(id);
}
}
None
}
_ => None,
}
}

/// Returns `true` if this [`Expression`] is a function
/// (either [`Function`] or [`ArrowFunctionExpression`]).
pub fn is_function(&self) -> bool {
Expand Down
16 changes: 2 additions & 14 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,28 +1054,16 @@ impl<'a> PeepholeOptimizations {
None
}
};
let is_id_or_assign_to_id = |b: &Expression<'a>| match b {
Expression::Identifier(id) => Some(id.name),
Expression::AssignmentExpression(assign_expr) => {
if assign_expr.operator == AssignmentOperator::Assign {
if let AssignmentTarget::AssignmentTargetIdentifier(id) = &assign_expr.left {
return Some(id.name);
}
}
None
}
_ => None,
};
let (left_value, (left_non_value_expr, left_id_name)) = {
let left_value;
let left_non_value;
if let Some(v) = is_null_or_undefined(&left_binary_expr.left) {
left_value = v;
let left_non_value_id = is_id_or_assign_to_id(&left_binary_expr.right)?;
let left_non_value_id = left_binary_expr.right.extract_id_or_assign_to_id()?.name;
left_non_value = (&mut left_binary_expr.right, left_non_value_id);
} else {
left_value = is_null_or_undefined(&left_binary_expr.right)?;
let left_non_value_id = is_id_or_assign_to_id(&left_binary_expr.left)?;
let left_non_value_id = left_binary_expr.left.extract_id_or_assign_to_id()?.name;
left_non_value = (&mut left_binary_expr.left, left_non_value_id);
}
(left_value, left_non_value)
Expand Down

0 comments on commit 357135a

Please sign in to comment.