-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(minifier): add
MinimizeConditions
pass
- Loading branch information
Showing
12 changed files
with
231 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use oxc_ast::{ast::*, AstBuilder}; | ||
use oxc_traverse::{Traverse, TraverseCtx}; | ||
|
||
use crate::{node_util::NodeUtil, tri::Tri, CompressorPass}; | ||
|
||
/// Minimize Conditions | ||
/// | ||
/// A peephole optimization that minimizes conditional expressions according to De Morgan's laws. | ||
/// Also rewrites conditional statements as expressions by replacing them | ||
/// with `? :` and short-circuit binary operators. | ||
/// | ||
/// <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java> | ||
pub struct MinimizeConditions<'a> { | ||
ast: AstBuilder<'a>, | ||
} | ||
|
||
impl<'a> CompressorPass<'a> for MinimizeConditions<'a> {} | ||
|
||
impl<'a> Traverse<'a> for MinimizeConditions<'a> { | ||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { | ||
self.fold_expression(expr, ctx); | ||
} | ||
} | ||
|
||
impl<'a> MinimizeConditions<'a> { | ||
pub fn new(ast: AstBuilder<'a>) -> Self { | ||
Self { ast } | ||
} | ||
|
||
fn fold_expression(&self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { | ||
if let Some(folded_expr) = match expr { | ||
Expression::ConditionalExpression(e) => self.try_fold_conditional_expression(e, ctx), | ||
Expression::UnaryExpression(e) if e.operator.is_not() => self.try_minimize_not(e), | ||
_ => None, | ||
} { | ||
*expr = folded_expr; | ||
}; | ||
} | ||
|
||
fn try_fold_conditional_expression( | ||
&self, | ||
expr: &mut ConditionalExpression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Option<Expression<'a>> { | ||
match ctx.get_boolean_value(&expr.test) { | ||
Tri::True => { | ||
// Bail `let o = { f() { assert.ok(this !== o); } }; (true ? o.f : false)(); (true ? o.f : false)``;` | ||
let parent = ctx.ancestry.parent(); | ||
if parent.is_tagged_template_expression() || parent.is_call_expression() { | ||
return None; | ||
} | ||
Some(self.ast.move_expression(&mut expr.consequent)) | ||
} | ||
Tri::False => Some(self.ast.move_expression(&mut expr.alternate)), | ||
Tri::Unknown => None, | ||
} | ||
} | ||
|
||
/// Try to minimize NOT nodes such as `!(x==y)`. | ||
fn try_minimize_not(&self, expr: &mut UnaryExpression<'a>) -> Option<Expression<'a>> { | ||
debug_assert!(expr.operator.is_not()); | ||
if let Expression::BinaryExpression(binary_expr) = &mut expr.argument { | ||
if let Some(new_op) = binary_expr.operator.equality_inverse_operator() { | ||
binary_expr.operator = new_op; | ||
return Some(self.ast.move_expression(&mut expr.argument)); | ||
} | ||
} | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.