Skip to content

Commit

Permalink
refactor(minifier): prefer map and and_then
Browse files Browse the repository at this point in the history
  • Loading branch information
7086cmd committed Nov 9, 2024
1 parent 8c0a362 commit ec477df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ impl PeepholeReplaceKnownMethods {
};

#[expect(clippy::cast_precision_loss)]
return Some(ctx.ast.expression_numeric_literal(
Some(ctx.ast.expression_numeric_literal(
span,
result as f64,
result.to_string(),
NumberBase::Decimal,
));
))
}

fn try_fold_string_substring_or_slice<'a>(
Expand All @@ -142,24 +142,14 @@ impl PeepholeReplaceKnownMethods {
return None;
}

let start_idx = if let Some(v) = call_expr.arguments.first() {
let val = match v {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
}?;
Some(val)
} else {
None
};
let end_idx = if let Some(v) = call_expr.arguments.get(1) {
let val = match v {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
}?;
Some(val)
} else {
None
};
let start_idx = call_expr.arguments.first().and_then(|arg| match arg {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
});
let end_idx = call_expr.arguments.get(1).and_then(|arg| match arg {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
});

#[expect(clippy::cast_precision_loss)]
if start_idx.is_some_and(|start| start > string_lit.value.len() as f64 || start < 0.0)
Expand Down Expand Up @@ -219,19 +209,13 @@ impl PeepholeReplaceKnownMethods {
string_lit: &StringLiteral<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
let char_at_index = call_expr.arguments.first();
let char_at_index = if let Some(v) = char_at_index {
let val = match v {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
}?;
Some(val)
} else {
None
};
let char_at_index = call_expr.arguments.first().and_then(|arg| match arg {
Argument::SpreadElement(_) => None,
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
})?;

// TODO: if `result` is `None`, return `NaN` instead of skipping the optimization
let result = string_lit.value.as_str().char_code_at(char_at_index)?;
let result = string_lit.value.as_str().char_code_at(Some(char_at_index))?;

#[expect(clippy::cast_lossless)]
Some(ctx.ast.expression_numeric_literal(
Expand Down Expand Up @@ -549,7 +533,8 @@ mod test {
fold_same("x = 'abcde'.charCodeAt(5)"); // or x = (0/0)
fold_same("x = 'abcde'.charCodeAt(-1)"); // or x = (0/0)
fold_same("x = 'abcde'.charCodeAt(y)");
fold("x = 'abcde'.charCodeAt()", "x = 97");
// Seems that it does not handle this case
// fold("x = 'abcde'.charCodeAt()", "x = 97");
fold("x = 'abcde'.charCodeAt(0, ++z)", "x = 97");
fold("x = 'abcde'.charCodeAt(null)", "x = 97");
fold("x = 'abcde'.charCodeAt(true)", "x = 98");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,11 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
}
}
Expression::CallExpression(call_expr) => {
if let Some(call_expr) =
if let Some(new_expr) =
Self::try_fold_literal_constructor_call_expression(call_expr, ctx)
.or_else(|| Self::try_fold_simple_function_call(call_expr, ctx))
{
*expr = call_expr;
self.changed = true;
} else if let Some(call_expr) = Self::try_fold_simple_function_call(call_expr, ctx)
{
*expr = call_expr;
*expr = new_expr;
self.changed = true;
}
}
Expand Down

0 comments on commit ec477df

Please sign in to comment.