Skip to content

Commit

Permalink
fix(es/minifier): Only cast global Infinity/undefined/NaN (#7684)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #7683.
  • Loading branch information
magic-akari authored Jul 21, 2023
1 parent 549e138 commit 241c04a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
23 changes: 23 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7683/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
let Infinity = 3;
console.log(Infinity);
if (Infinity > 4) console.log("Infinity");
}

{
let NaN = 3;
console.log(NaN);
if (NaN < 4) console.log("NaN");
}

{
let undefined = 3;
console.log(undefined);
if (undefined < 4) console.log("undefined");
}

{
let x = 3;
console.log(x);
if (x < 4) console.log("undefined");
}
16 changes: 16 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/7683/1/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
let Infinity = 3;
console.log(Infinity);
if (Infinity > 4) console.log("Infinity");
}{
let NaN1 = 3;
console.log(NaN);
}{
let undefined = 3;
console.log(undefined);
if (undefined < 4) console.log("undefined");
}{
let x = 3;
console.log(x);
if (x < 4) console.log("undefined");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dead_code": true
}
15 changes: 9 additions & 6 deletions crates/swc_ecma_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,9 +902,9 @@ pub trait ExprExt {
Lit::Str(Str { value, .. }) => return (Pure, num_from_str(value)),
_ => return (Pure, Unknown),
},
Expr::Ident(Ident { sym, .. }) => match *sym {
js_word!("undefined") | js_word!("NaN") => NAN,
js_word!("Infinity") => INFINITY,
Expr::Ident(Ident { sym, span, .. }) => match *sym {
js_word!("undefined") | js_word!("NaN") if span.ctxt == ctx.unresolved_ctxt => NAN,
js_word!("Infinity") if span.ctxt == ctx.unresolved_ctxt => INFINITY,
_ => return (Pure, Unknown),
},
Expr::Unary(UnaryExpr {
Expand All @@ -915,8 +915,9 @@ pub trait ExprExt {
&**arg,
Expr::Ident(Ident {
sym: js_word!("Infinity"),
span,
..
})
}) if span.ctxt == ctx.unresolved_ctxt
) =>
{
-INFINITY
Expand Down Expand Up @@ -1005,8 +1006,10 @@ pub trait ExprExt {
// converted. unimplemented!("TplLit.
// as_string()")
}
Expr::Ident(Ident { ref sym, .. }) => match *sym {
js_word!("undefined") | js_word!("Infinity") | js_word!("NaN") => {
Expr::Ident(Ident { ref sym, span, .. }) => match *sym {
js_word!("undefined") | js_word!("Infinity") | js_word!("NaN")
if span.ctxt == ctx.unresolved_ctxt =>
{
Known(Cow::Borrowed(&**sym))
}
_ => Unknown,
Expand Down

1 comment on commit 241c04a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 241c04a Previous: 57398af Ratio
es/full/bugs-1 293194 ns/iter (± 6378) 292603 ns/iter (± 6315) 1.00
es/full/minify/libraries/antd 1321864611 ns/iter (± 18958849) 1327966908 ns/iter (± 12181376) 1.00
es/full/minify/libraries/d3 277917980 ns/iter (± 5712947) 279886813 ns/iter (± 3193588) 0.99
es/full/minify/libraries/echarts 1064009093 ns/iter (± 4245819) 1077454019 ns/iter (± 25836710) 0.99
es/full/minify/libraries/jquery 84729198 ns/iter (± 270018) 84513918 ns/iter (± 250907) 1.00
es/full/minify/libraries/lodash 98151219 ns/iter (± 161341) 98625075 ns/iter (± 341730) 1.00
es/full/minify/libraries/moment 49660844 ns/iter (± 60121) 49807443 ns/iter (± 126002) 1.00
es/full/minify/libraries/react 17962471 ns/iter (± 48853) 17973176 ns/iter (± 29848) 1.00
es/full/minify/libraries/terser 219869948 ns/iter (± 1124538) 220662642 ns/iter (± 1324478) 1.00
es/full/minify/libraries/three 386141614 ns/iter (± 2648781) 391015335 ns/iter (± 4206612) 0.99
es/full/minify/libraries/typescript 2667482556 ns/iter (± 13629789) 2734923648 ns/iter (± 34976325) 0.98
es/full/minify/libraries/victory 568135630 ns/iter (± 3140194) 614853844 ns/iter (± 18506502) 0.92
es/full/minify/libraries/vue 120854332 ns/iter (± 830287) 124652161 ns/iter (± 1182641) 0.97
es/full/codegen/es3 34316 ns/iter (± 82) 34637 ns/iter (± 110) 0.99
es/full/codegen/es5 34267 ns/iter (± 42) 34722 ns/iter (± 143) 0.99
es/full/codegen/es2015 34299 ns/iter (± 65) 34770 ns/iter (± 95) 0.99
es/full/codegen/es2016 34267 ns/iter (± 73) 34662 ns/iter (± 70) 0.99
es/full/codegen/es2017 34312 ns/iter (± 95) 34708 ns/iter (± 113) 0.99
es/full/codegen/es2018 34285 ns/iter (± 82) 34808 ns/iter (± 87) 0.98
es/full/codegen/es2019 34341 ns/iter (± 78) 34775 ns/iter (± 150) 0.99
es/full/codegen/es2020 34354 ns/iter (± 93) 34658 ns/iter (± 125) 0.99
es/full/all/es3 172475523 ns/iter (± 1168953) 174420963 ns/iter (± 1690737) 0.99
es/full/all/es5 165273391 ns/iter (± 1569545) 167039418 ns/iter (± 685326) 0.99
es/full/all/es2015 124461022 ns/iter (± 814370) 126364236 ns/iter (± 663237) 0.98
es/full/all/es2016 123286043 ns/iter (± 486242) 125298226 ns/iter (± 1047507) 0.98
es/full/all/es2017 122379610 ns/iter (± 386766) 124640474 ns/iter (± 593951) 0.98
es/full/all/es2018 120444805 ns/iter (± 431552) 122386808 ns/iter (± 1249745) 0.98
es/full/all/es2019 120191745 ns/iter (± 572433) 121735560 ns/iter (± 368142) 0.99
es/full/all/es2020 115859306 ns/iter (± 551974) 116909977 ns/iter (± 502282) 0.99
es/full/parser 539883 ns/iter (± 5226) 543184 ns/iter (± 6418) 0.99
es/full/base/fixer 17745 ns/iter (± 167) 17698 ns/iter (± 47) 1.00
es/full/base/resolver_and_hygiene 82647 ns/iter (± 206) 83514 ns/iter (± 644) 0.99
serialization of serde 294 ns/iter (± 1) 302 ns/iter (± 4) 0.97
css/minify/libraries/bootstrap 28254499 ns/iter (± 166533) 28861020 ns/iter (± 57949) 0.98
css/visitor/compare/clone 1637799 ns/iter (± 2667) 1648738 ns/iter (± 8520) 0.99
css/visitor/compare/visit_mut_span 1774096 ns/iter (± 6573) 1787254 ns/iter (± 19527) 0.99
css/visitor/compare/visit_mut_span_panic 1831275 ns/iter (± 5899) 1843006 ns/iter (± 6992) 0.99
css/visitor/compare/fold_span 2574277 ns/iter (± 12833) 2558829 ns/iter (± 14788) 1.01
css/visitor/compare/fold_span_panic 2764428 ns/iter (± 15263) 2769710 ns/iter (± 10412) 1.00
css/lexer/bootstrap_5_1_3 4487332 ns/iter (± 3861) 4500223 ns/iter (± 4160) 1.00
css/lexer/foundation_6_7_4 3766743 ns/iter (± 2325) 3794331 ns/iter (± 3673) 0.99
css/lexer/tailwind_3_1_1 720823 ns/iter (± 2985) 720596 ns/iter (± 519) 1.00
css/parser/bootstrap_5_1_3 19592842 ns/iter (± 76094) 19942652 ns/iter (± 29670) 0.98
css/parser/foundation_6_7_4 15529733 ns/iter (± 41698) 15747155 ns/iter (± 60183) 0.99
css/parser/tailwind_3_1_1 2981137 ns/iter (± 2870) 3025171 ns/iter (± 6102) 0.99
es/codegen/colors 734588 ns/iter (± 402652) 741769 ns/iter (± 404048) 0.99
es/codegen/large 3131931 ns/iter (± 1681480) 3153527 ns/iter (± 1675410) 0.99
es/codegen/with-parser/colors 44204 ns/iter (± 429) 43147 ns/iter (± 639) 1.02
es/codegen/with-parser/large 484572 ns/iter (± 574) 476629 ns/iter (± 820) 1.02
es/minify/libraries/antd 1148458195 ns/iter (± 14593694) 1161864391 ns/iter (± 8611441) 0.99
es/minify/libraries/d3 241812011 ns/iter (± 2676415) 245607554 ns/iter (± 1190801) 0.98
es/minify/libraries/echarts 919758931 ns/iter (± 13698541) 974600711 ns/iter (± 16773121) 0.94
es/minify/libraries/jquery 73636654 ns/iter (± 389240) 75299452 ns/iter (± 406476) 0.98
es/minify/libraries/lodash 88037321 ns/iter (± 137352) 90302813 ns/iter (± 523829) 0.97
es/minify/libraries/moment 43604865 ns/iter (± 112619) 44486359 ns/iter (± 191030) 0.98
es/minify/libraries/react 16014875 ns/iter (± 28353) 16165935 ns/iter (± 59168) 0.99
es/minify/libraries/terser 187765448 ns/iter (± 790676) 190955604 ns/iter (± 1810853) 0.98
es/minify/libraries/three 322992884 ns/iter (± 1740598) 327137055 ns/iter (± 4246184) 0.99
es/minify/libraries/typescript 2291881269 ns/iter (± 8994370) 2327760224 ns/iter (± 17599163) 0.98
es/minify/libraries/victory 472735163 ns/iter (± 14512767) 489827415 ns/iter (± 4452237) 0.97
es/minify/libraries/vue 107123358 ns/iter (± 248804) 108168796 ns/iter (± 307666) 0.99
es/visitor/compare/clone 1938516 ns/iter (± 4336) 1938032 ns/iter (± 3824) 1.00
es/visitor/compare/visit_mut_span 2287129 ns/iter (± 5028) 2289946 ns/iter (± 3081) 1.00
es/visitor/compare/visit_mut_span_panic 2324419 ns/iter (± 3129) 2331006 ns/iter (± 5171) 1.00
es/visitor/compare/fold_span 3315610 ns/iter (± 4195) 3327384 ns/iter (± 10529) 1.00
es/visitor/compare/fold_span_panic 3477431 ns/iter (± 4855) 3474297 ns/iter (± 9358) 1.00
es/lexer/colors 12609 ns/iter (± 13) 12639 ns/iter (± 18) 1.00
es/lexer/angular 6159853 ns/iter (± 18750) 6134978 ns/iter (± 15497) 1.00
es/lexer/backbone 800148 ns/iter (± 639) 809383 ns/iter (± 2244) 0.99
es/lexer/jquery 4502994 ns/iter (± 4983) 4487632 ns/iter (± 2956) 1.00
es/lexer/jquery mobile 6906329 ns/iter (± 13509) 6901984 ns/iter (± 5805) 1.00
es/lexer/mootools 3565140 ns/iter (± 6516) 3574012 ns/iter (± 6279) 1.00
es/lexer/underscore 673534 ns/iter (± 1093) 665234 ns/iter (± 570) 1.01
es/lexer/three 21197953 ns/iter (± 19689) 21039577 ns/iter (± 26438) 1.01
es/lexer/yui 3870332 ns/iter (± 1572) 3817851 ns/iter (± 1240) 1.01
es/parser/colors 26495 ns/iter (± 61) 26728 ns/iter (± 67) 0.99
es/parser/angular 13330895 ns/iter (± 54588) 13445116 ns/iter (± 152727) 0.99
es/parser/backbone 1969855 ns/iter (± 8759) 1991557 ns/iter (± 8721) 0.99
es/parser/jquery 10746776 ns/iter (± 43498) 10876853 ns/iter (± 71747) 0.99
es/parser/jquery mobile 16502279 ns/iter (± 37811) 16721197 ns/iter (± 79935) 0.99
es/parser/mootools 8259212 ns/iter (± 21767) 8334486 ns/iter (± 28432) 0.99
es/parser/underscore 1701210 ns/iter (± 9104) 1716190 ns/iter (± 10864) 0.99
es/parser/three 46023543 ns/iter (± 129240) 47113115 ns/iter (± 376993) 0.98
es/parser/yui 8215630 ns/iter (± 27648) 8295804 ns/iter (± 36453) 0.99
es/preset-env/usage/builtin_type 139635 ns/iter (± 32697) 139633 ns/iter (± 32756) 1.00
es/preset-env/usage/property 16828 ns/iter (± 58) 17211 ns/iter (± 59) 0.98
es/resolver/typescript 87890571 ns/iter (± 1028951) 89905825 ns/iter (± 1109238) 0.98
es/fixer/typescript 63452645 ns/iter (± 269791) 64055390 ns/iter (± 774294) 0.99
es/hygiene/typescript 133711435 ns/iter (± 573672) 135585584 ns/iter (± 1361227) 0.99
es/resolver_with_hygiene/typescript 244961364 ns/iter (± 1597556) 248408458 ns/iter (± 1690118) 0.99
es/visitor/base-perf/module_clone 61999 ns/iter (± 319) 59385 ns/iter (± 184) 1.04
es/visitor/base-perf/fold_empty 64061 ns/iter (± 342) 63954 ns/iter (± 325) 1.00
es/visitor/base-perf/fold_noop_impl_all 64403 ns/iter (± 882) 64544 ns/iter (± 315) 1.00
es/visitor/base-perf/fold_noop_impl_vec 64270 ns/iter (± 229) 64671 ns/iter (± 472) 0.99
es/visitor/base-perf/boxing_boxed_clone 58 ns/iter (± 0) 57 ns/iter (± 0) 1.02
es/visitor/base-perf/boxing_unboxed_clone 39 ns/iter (± 0) 40 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_boxed 110 ns/iter (± 0) 110 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 90 ns/iter (± 0) 0.86
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2498 ns/iter (± 7) 2514 ns/iter (± 15) 0.99
es/base/parallel/resolver/typescript 4498257590 ns/iter (± 283672280) 3774063086 ns/iter (± 272099948) 1.19
es/base/parallel/hygiene/typescript 1472152606 ns/iter (± 19301840) 1482008281 ns/iter (± 22362999) 0.99
misc/visitors/time-complexity/time 5 137 ns/iter (± 0) 146 ns/iter (± 2) 0.94
misc/visitors/time-complexity/time 10 396 ns/iter (± 3) 400 ns/iter (± 14) 0.99
misc/visitors/time-complexity/time 15 732 ns/iter (± 18) 681 ns/iter (± 7) 1.07
misc/visitors/time-complexity/time 20 1045 ns/iter (± 2) 1055 ns/iter (± 6) 0.99
misc/visitors/time-complexity/time 40 3487 ns/iter (± 4) 3483 ns/iter (± 17) 1.00
misc/visitors/time-complexity/time 60 8773 ns/iter (± 20) 8762 ns/iter (± 461) 1.00
es/full-target/es2016 235638 ns/iter (± 625) 238214 ns/iter (± 913) 0.99
es/full-target/es2017 225286 ns/iter (± 461) 225735 ns/iter (± 386) 1.00
es/full-target/es2018 212848 ns/iter (± 363) 213829 ns/iter (± 859) 1.00
es2020_nullish_coalescing 71685 ns/iter (± 357) 73988 ns/iter (± 539) 0.97
es2020_optional_chaining 78934 ns/iter (± 274) 79558 ns/iter (± 239) 0.99
es2022_class_properties 120924 ns/iter (± 224) 121455 ns/iter (± 452) 1.00
es2018_object_rest_spread 75738 ns/iter (± 361) 76307 ns/iter (± 227) 0.99
es2019_optional_catch_binding 64127 ns/iter (± 180) 64342 ns/iter (± 101) 1.00
es2017_async_to_generator 66062 ns/iter (± 252) 65222 ns/iter (± 233) 1.01
es2016_exponentiation 70353 ns/iter (± 202) 69218 ns/iter (± 241) 1.02
es2015_arrow 74826 ns/iter (± 317) 72965 ns/iter (± 159) 1.03
es2015_block_scoped_fn 70371 ns/iter (± 194) 69869 ns/iter (± 251) 1.01
es2015_block_scoping 126612 ns/iter (± 226) 125912 ns/iter (± 436) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.