mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(minifier): use map and and_then instead of let else (#7178)
For the test case, Closure Compiler doesn't handle this at all in the REPL! If it's necessary, I will turn it back. This PR uses builtin `and_then` and `map` method, which is better instead of a lot of `if let Some`.
This commit is contained in:
parent
d27e14f065
commit
a2977655c8
2 changed files with 20 additions and 38 deletions
|
|
@ -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>(
|
||||
|
|
@ -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)
|
||||
|
|
@ -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(
|
||||
|
|
@ -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");
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue