mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +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)]
|
#[expect(clippy::cast_precision_loss)]
|
||||||
return Some(ctx.ast.expression_numeric_literal(
|
Some(ctx.ast.expression_numeric_literal(
|
||||||
span,
|
span,
|
||||||
result as f64,
|
result as f64,
|
||||||
result.to_string(),
|
result.to_string(),
|
||||||
NumberBase::Decimal,
|
NumberBase::Decimal,
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_string_substring_or_slice<'a>(
|
fn try_fold_string_substring_or_slice<'a>(
|
||||||
|
|
@ -142,24 +142,14 @@ impl PeepholeReplaceKnownMethods {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_idx = if let Some(v) = call_expr.arguments.first() {
|
let start_idx = call_expr.arguments.first().and_then(|arg| match arg {
|
||||||
let val = match v {
|
|
||||||
Argument::SpreadElement(_) => None,
|
Argument::SpreadElement(_) => None,
|
||||||
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
|
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
|
||||||
}?;
|
});
|
||||||
Some(val)
|
let end_idx = call_expr.arguments.get(1).and_then(|arg| match arg {
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
let end_idx = if let Some(v) = call_expr.arguments.get(1) {
|
|
||||||
let val = match v {
|
|
||||||
Argument::SpreadElement(_) => None,
|
Argument::SpreadElement(_) => None,
|
||||||
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
|
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
|
||||||
}?;
|
});
|
||||||
Some(val)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
#[expect(clippy::cast_precision_loss)]
|
#[expect(clippy::cast_precision_loss)]
|
||||||
if start_idx.is_some_and(|start| start > string_lit.value.len() as f64 || start < 0.0)
|
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>,
|
string_lit: &StringLiteral<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
let char_at_index = call_expr.arguments.first();
|
let char_at_index = call_expr.arguments.first().and_then(|arg| match arg {
|
||||||
let char_at_index = if let Some(v) = char_at_index {
|
|
||||||
let val = match v {
|
|
||||||
Argument::SpreadElement(_) => None,
|
Argument::SpreadElement(_) => None,
|
||||||
_ => Ctx(ctx).get_side_free_number_value(v.to_expression()),
|
_ => Ctx(ctx).get_side_free_number_value(arg.to_expression()),
|
||||||
}?;
|
})?;
|
||||||
Some(val)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: if `result` is `None`, return `NaN` instead of skipping the optimization
|
// 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)]
|
#[expect(clippy::cast_lossless)]
|
||||||
Some(ctx.ast.expression_numeric_literal(
|
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(5)"); // or x = (0/0)
|
||||||
fold_same("x = 'abcde'.charCodeAt(-1)"); // or x = (0/0)
|
fold_same("x = 'abcde'.charCodeAt(-1)"); // or x = (0/0)
|
||||||
fold_same("x = 'abcde'.charCodeAt(y)");
|
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(0, ++z)", "x = 97");
|
||||||
fold("x = 'abcde'.charCodeAt(null)", "x = 97");
|
fold("x = 'abcde'.charCodeAt(null)", "x = 97");
|
||||||
fold("x = 'abcde'.charCodeAt(true)", "x = 98");
|
fold("x = 'abcde'.charCodeAt(true)", "x = 98");
|
||||||
|
|
|
||||||
|
|
@ -103,14 +103,11 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression::CallExpression(call_expr) => {
|
Expression::CallExpression(call_expr) => {
|
||||||
if let Some(call_expr) =
|
if let Some(new_expr) =
|
||||||
Self::try_fold_literal_constructor_call_expression(call_expr, ctx)
|
Self::try_fold_literal_constructor_call_expression(call_expr, ctx)
|
||||||
|
.or_else(|| Self::try_fold_simple_function_call(call_expr, ctx))
|
||||||
{
|
{
|
||||||
*expr = call_expr;
|
*expr = new_expr;
|
||||||
self.changed = true;
|
|
||||||
} else if let Some(call_expr) = Self::try_fold_simple_function_call(call_expr, ctx)
|
|
||||||
{
|
|
||||||
*expr = call_expr;
|
|
||||||
self.changed = true;
|
self.changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue