Skip to content

Commit

Permalink
feat(minifier): normalize Infinity into f64::Infinity
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 27, 2024
1 parent 74572de commit 18dbaa1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 16 deletions.
23 changes: 22 additions & 1 deletion crates/oxc_minifier/src/ast_passes/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use oxc_ast::ast::*;
use oxc_syntax::scope::ScopeFlags;
use oxc_traverse::{traverse_mut_with_ctx, ReusableTraverseCtx, Traverse, TraverseCtx};

use crate::CompressorPass;
use crate::{node_util::Ctx, CompressorPass};

/// Normalize AST
///
Expand All @@ -25,6 +25,12 @@ impl<'a> Traverse<'a> for Normalize {
Self::convert_while_to_for(stmt, ctx);
}
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::Identifier(_) = expr {
Self::convert_infinity_into_number(expr, ctx);
}
}
}

impl<'a> Normalize {
Expand All @@ -45,6 +51,21 @@ impl<'a> Normalize {
);
*stmt = Statement::ForStatement(for_stmt);
}

fn convert_infinity_into_number(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let ctx = Ctx(ctx);
match expr {
Expression::Identifier(ident) if ctx.is_identifier_infinity(ident) => {
*expr = ctx.ast.expression_numeric_literal(
ident.span,
f64::INFINITY,
None,
NumberBase::Decimal,
);
}
_ => {}
}
}
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,8 +1276,11 @@ mod test {
#[test]
fn test_coercion_substitution_while() {
// enableTypeCheck();
test_same("var x = {}; while (x != null) throw 'a';");
test_same("var x = 1; while (x != 0) throw 'a';");
test(
"var x = {}; while (x != null) throw 'a';",
"var x = {}; for (;x != null;) throw 'a';",
);
test("var x = 1; while (x != 0) throw 'a';", "var x = 1; for (;x != 0;) throw 'a';");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ mod test {

// Cases to test for empty block.
// fold("while(x()){x}", "while(x());");
fold("while(x()){x()}", "while(x())x()");
fold("while(x()){x()}", "for(;x();)x()");
// fold("for(x=0;x<100;x++){x}", "for(x=0;x<100;x++);");
// fold("for(x in y){x}", "for(x in y);");
// fold("for (x of y) {x}", "for(x of y);");
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_minifier/src/ast_passes/statement_fusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ mod test {

#[test]
fn fuse_into_label() {
// fuse("a;b;c;label:for(x in y){}", "label:for(x in a,b,c,y){}");
// fuse("a;b;c;label:for(;g;){}", "label:for(a,b,c;g;){}");
// fuse("a;b;c;l1:l2:l3:for(;g;){}", "l1:l2:l3:for(a,b,c;g;){}");
fuse_same("a;b;c;label:while(true){}");
fuse("a;b;c;label:for(x in y){}", "label:for(x in a,b,c,y){}");
fuse("a;b;c;label:for(;g;){}", "label:for(a,b,c;g;){}");
fuse("a;b;c;l1:l2:l3:for(;g;){}", "l1:l2:l3:for(a,b,c;g;){}");
fuse("a;b;c;label:while(true){}", "label:for(a,b,c;true;){}");
}

#[test]
Expand All @@ -304,7 +304,7 @@ mod test {

#[test]
fn no_fuse_into_while() {
fuse_same("a;b;c;while(x){}");
fuse("a;b;c;while(x){}", "for(a,b,c;x;){}");
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ impl<'a> Ctx<'a, '_> {
}
false
}

pub fn is_identifier_infinity(self, ident: &IdentifierReference) -> bool {
if ident.name == "Infinity" && ident.is_global_reference(self.symbols()) {
return true;
}
false
}
}
3 changes: 2 additions & 1 deletion crates/oxc_minifier/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use oxc_span::SourceType;
use oxc_traverse::ReusableTraverseCtx;

use crate::{
ast_passes::{CompressorPass, RemoveSyntax},
ast_passes::{CompressorPass, Normalize, RemoveSyntax},
CompressOptions,
};

Expand Down Expand Up @@ -45,6 +45,7 @@ fn run<'a, P: CompressorPass<'a>>(
SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree();
let mut ctx = ReusableTraverseCtx::new(scopes, symbols, allocator);
RemoveSyntax::new(CompressOptions::all_false()).build(&mut program, &mut ctx);
Normalize::new().build(&mut program, &mut ctx);
pass.build(&mut program, &mut ctx);
}

Expand Down
12 changes: 6 additions & 6 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ Original | minified | minified | gzip | gzip | Fixture

342.15 kB | 118.77 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js

544.10 kB | 72.53 kB | 72.48 kB | 26.23 kB | 26.20 kB | lodash.js
544.10 kB | 72.52 kB | 72.48 kB | 26.22 kB | 26.20 kB | lodash.js

555.77 kB | 274.26 kB | 270.13 kB | 91.26 kB | 90.80 kB | d3.js
555.77 kB | 274.12 kB | 270.13 kB | 91.25 kB | 90.80 kB | d3.js

1.01 MB | 461.13 kB | 458.89 kB | 126.91 kB | 126.71 kB | bundle.min.js

1.25 MB | 657.23 kB | 646.76 kB | 164.23 kB | 163.73 kB | three.js
1.25 MB | 657.19 kB | 646.76 kB | 164.21 kB | 163.73 kB | three.js

2.14 MB | 735.67 kB | 724.14 kB | 181.09 kB | 181.07 kB | victory.js
2.14 MB | 735.56 kB | 724.14 kB | 181.07 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.35 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.26 kB | 331.56 kB | echarts.js

6.69 MB | 2.38 MB | 2.31 MB | 495.33 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.94 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.93 kB | 915.50 kB | typescript.js

0 comments on commit 18dbaa1

Please sign in to comment.