mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): improve minimizing conditional expressions (#8251)
This commit is contained in:
parent
91b42defb5
commit
d1224f95ce
1 changed files with 14 additions and 1 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use oxc_allocator::Vec;
|
||||
use oxc_ast::ast::*;
|
||||
use oxc_ecmascript::constant_evaluation::ValueType;
|
||||
use oxc_span::{GetSpan, SPAN};
|
||||
use oxc_span::{cmp::ContentEq, GetSpan, SPAN};
|
||||
use oxc_traverse::{traverse_mut_with_ctx, Ancestor, ReusableTraverseCtx, Traverse, TraverseCtx};
|
||||
|
||||
use crate::CompressorPass;
|
||||
|
|
@ -462,6 +462,15 @@ impl<'a> PeepholeMinimizeConditions {
|
|||
));
|
||||
}
|
||||
|
||||
// `foo() ? bar : bar` -> `foo(), bar`
|
||||
if expr.alternate.content_eq(&expr.consequent) {
|
||||
let expressions = ctx.ast.vec_from_array([
|
||||
ctx.ast.move_expression(&mut expr.test),
|
||||
ctx.ast.move_expression(&mut expr.consequent),
|
||||
]);
|
||||
return Some(ctx.ast.expression_sequence(expr.span, expressions));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
|
@ -851,6 +860,10 @@ mod test {
|
|||
fold("function x () { return a ? true : b }", "function x() { return !!a || b }");
|
||||
// can't be minified e.g. `a = ''` would return `''`
|
||||
fold("function x() { return a && true }", "function x() { return a && true }");
|
||||
|
||||
fold("foo ? bar : bar", "foo, bar");
|
||||
fold_same("foo ? bar : baz");
|
||||
fold("foo() ? bar : bar", "foo(), bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue