Skip to content

Commit

Permalink
fix(es/minifier): Fix a bug about eval of name mangler (#7615)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jul 18, 2023
1 parent 3930f77 commit 6be1f70
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


export DIFF=0
export UPDATE=1
# export UPDATE=1
export SWC_CHECK=0
export SWC_RUN=0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"defaults": true,
"conditionals": false,
"collapse_vars": false,
"properties": false,
"join_vars": false,
"passes": 1
}
73 changes: 73 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([
[285], {
1973:
(function (module) {
eval(module.code)
}),

1985:
(function (__unused_webpack_module, exports, __webpack_require__) {
"use strict";

var t = 0,
r = e => "checkbox" === e.type;
var u = (e, t) => e.has((e => e.substring(0, e.search(/\.\d+(\.|$)/)) || e)(t)),
d = () => {
};

function H(e, t, r) {
}
var ee = e => "radio" === e.type;

function Me(e = {}) {
let t, n = {
...e
},
y = {},
h = 0,
p = {
},
_ = {
};
const E = 0, $ = async e => {
const r = e.target;
let s = r.name;
const a = 0;
if (a) {
let l, c;
const p = 0;
p ? (a._f.onBlur && a._f.onBlur(e), t && t(0)) : a._f.onChange && a._f.onChange(e);
const A = 0;

a._f.deps && E(s, c, l, A)
}
}, te = (e, t = {}) => {
let s = 0;
const a = 0;
return s ? a && H(h, e, t.disabled ? void 0 : 0) : 0, {
onChange: $,
onBlur: $,
ref: a => {
if (a) {
te(e, t), s = d(y, e);
const n = 0;
(e => ee(e) || r(e))(n),
s._f.refs;
} else s = d(y, e, {}), s._f && (s._f.mount = !1), (n.shouldUnregister || t.shouldUnregister) && (!u(_.array, e) || !p.action) && _.unMount.add(e)
}
}
};

}
exports.useForm = function (e = {}) {
const r = t.default.useRef();
r.current ? r.current.control._options = e : r.current = {
...Me(e),
};

};
}),


}
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
43 changes: 43 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([
[
285
],
{
1973: function(module) {
eval(module.code);
},
1985: function(o, f, i) {
"use strict";
var k = 0, v = (o)=>"checkbox" === o.type;
var b = (o, f)=>{
var i;
return o.has((i = f, i.substring(0, i.search(/\.\d+(\.|$)/)) || i));
}, g = ()=>{};
function C(o, f, i) {}
var m = (o)=>"radio" === o.type;
function w(o = {}) {
let f, i = {
...o
}, k = {}, v = 0, b = {}, g = {};
const C = 0, m = async (o)=>{
const i = o.target;
let k = i.name;
const v = 0;
if (v) {
let i, b;
const g = 0;
g ? (v._f.onBlur && v._f.onBlur(o), f && f(0)) : v._f.onChange && v._f.onChange(o);
const m = 0;
v._f.deps && C(k, b, i, m);
}
};
}
f.useForm = function(o = {}) {
const f = k.default.useRef();
f.current ? f.current.control._options = o : f.current = {
...w(o)
};
};
}
}
]);
39 changes: 28 additions & 11 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,34 @@ where

self.unresolved = self.get_unresolved(m, has_eval);

{
let map = self.get_map(m, false, true, has_eval);

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}

let map = self.get_map(m, false, true, has_eval);

// If we have eval, we cannot rename a whole program at once.
//
// Still, we can, and should rename some identifiers, if the containing scope
// (function-like nodes) does not have eval. This `eval` check includes
// `eval` in children.
//
// We calculate the top level map first, rename children, and then rename the
// top level.
//
//
// Order:
//
// 1. Top level map calculation
// 2. Per-unit map calculation
// 3. Per-unit renaming
// 4. Top level renaming
//
// This is because the the top level map may contain a mapping which conflicts
// with a map from one of the children.
//
// See https://github.com/swc-project/swc/pull/7615
if has_eval {
m.visit_mut_children_with(self);
}

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}

fn visit_mut_script(&mut self, m: &mut Script) {
Expand All @@ -261,15 +280,13 @@ where

self.unresolved = self.get_unresolved(m, has_eval);

{
let map = self.get_map(m, false, true, has_eval);

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
let map = self.get_map(m, false, true, has_eval);

if has_eval {
m.visit_mut_children_with(self);
}

m.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}

Expand Down

1 comment on commit 6be1f70

@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: 6be1f70 Previous: a2d0408 Ratio
es/full/bugs-1 289969 ns/iter (± 9838) 289772 ns/iter (± 7279) 1.00
es/full/minify/libraries/antd 1304036338 ns/iter (± 15109339) 1427195090 ns/iter (± 19347397) 0.91
es/full/minify/libraries/d3 276007898 ns/iter (± 2264141) 293890814 ns/iter (± 5210902) 0.94
es/full/minify/libraries/echarts 1052211908 ns/iter (± 11131022) 1127662261 ns/iter (± 16608581) 0.93
es/full/minify/libraries/jquery 83767153 ns/iter (± 181557) 87664418 ns/iter (± 3603741) 0.96
es/full/minify/libraries/lodash 98274818 ns/iter (± 352768) 101296454 ns/iter (± 1491577) 0.97
es/full/minify/libraries/moment 49355773 ns/iter (± 180144) 50758729 ns/iter (± 749969) 0.97
es/full/minify/libraries/react 17872540 ns/iter (± 53555) 18192445 ns/iter (± 112401) 0.98
es/full/minify/libraries/terser 217334673 ns/iter (± 2070634) 227940389 ns/iter (± 2864508) 0.95
es/full/minify/libraries/three 383465690 ns/iter (± 2953723) 403711030 ns/iter (± 8863632) 0.95
es/full/minify/libraries/typescript 2673688482 ns/iter (± 14047398) 2806644043 ns/iter (± 18361484) 0.95
es/full/minify/libraries/victory 563905149 ns/iter (± 9983710) 602809339 ns/iter (± 13917965) 0.94
es/full/minify/libraries/vue 119059524 ns/iter (± 1838031) 122932567 ns/iter (± 576430) 0.97
es/full/codegen/es3 35276 ns/iter (± 73) 35079 ns/iter (± 119) 1.01
es/full/codegen/es5 35507 ns/iter (± 87) 34976 ns/iter (± 146) 1.02
es/full/codegen/es2015 35440 ns/iter (± 49) 35155 ns/iter (± 139) 1.01
es/full/codegen/es2016 35499 ns/iter (± 96) 35110 ns/iter (± 121) 1.01
es/full/codegen/es2017 35471 ns/iter (± 108) 35070 ns/iter (± 153) 1.01
es/full/codegen/es2018 35520 ns/iter (± 162) 35038 ns/iter (± 135) 1.01
es/full/codegen/es2019 35466 ns/iter (± 119) 34948 ns/iter (± 167) 1.01
es/full/codegen/es2020 35438 ns/iter (± 112) 35087 ns/iter (± 150) 1.01
es/full/all/es3 172420391 ns/iter (± 711866) 174110729 ns/iter (± 1192190) 0.99
es/full/all/es5 164853198 ns/iter (± 732659) 164962083 ns/iter (± 1367343) 1.00
es/full/all/es2015 123978029 ns/iter (± 632558) 124439502 ns/iter (± 1328429) 1.00
es/full/all/es2016 123391653 ns/iter (± 468748) 123751174 ns/iter (± 575023) 1.00
es/full/all/es2017 122874601 ns/iter (± 1144855) 123076686 ns/iter (± 615926) 1.00
es/full/all/es2018 120620224 ns/iter (± 683775) 120741008 ns/iter (± 836438) 1.00
es/full/all/es2019 120391781 ns/iter (± 291343) 120656148 ns/iter (± 1056561) 1.00
es/full/all/es2020 116211056 ns/iter (± 675776) 115696908 ns/iter (± 615796) 1.00
es/full/parser 541855 ns/iter (± 5790) 539150 ns/iter (± 6464) 1.01
es/full/base/fixer 18015 ns/iter (± 78) 19686 ns/iter (± 139) 0.92
es/full/base/resolver_and_hygiene 79816 ns/iter (± 197) 80766 ns/iter (± 234) 0.99
serialization of serde 296 ns/iter (± 2) 314 ns/iter (± 1) 0.94
css/minify/libraries/bootstrap 29244489 ns/iter (± 96806) 30438022 ns/iter (± 183355) 0.96
css/visitor/compare/clone 1721249 ns/iter (± 15907) 1700985 ns/iter (± 11573) 1.01
css/visitor/compare/visit_mut_span 1773733 ns/iter (± 16909) 1805004 ns/iter (± 15945) 0.98
css/visitor/compare/visit_mut_span_panic 1842106 ns/iter (± 19463) 1909923 ns/iter (± 12949) 0.96
css/visitor/compare/fold_span 2558860 ns/iter (± 15613) 2572735 ns/iter (± 23471) 0.99
css/visitor/compare/fold_span_panic 2740979 ns/iter (± 21554) 2748804 ns/iter (± 14258) 1.00
css/lexer/bootstrap_5_1_3 4435014 ns/iter (± 4696) 4429748 ns/iter (± 4680) 1.00
css/lexer/foundation_6_7_4 3778904 ns/iter (± 3829) 3730857 ns/iter (± 2121) 1.01
css/lexer/tailwind_3_1_1 717609 ns/iter (± 869) 708760 ns/iter (± 1992) 1.01
css/parser/bootstrap_5_1_3 20210522 ns/iter (± 78698) 19465582 ns/iter (± 55295) 1.04
css/parser/foundation_6_7_4 16145955 ns/iter (± 59405) 15527992 ns/iter (± 27287) 1.04
css/parser/tailwind_3_1_1 3045956 ns/iter (± 4062) 3008952 ns/iter (± 11784) 1.01
es/codegen/colors 732903 ns/iter (± 401609) 732143 ns/iter (± 400532) 1.00
es/codegen/large 3127056 ns/iter (± 1673809) 3136802 ns/iter (± 1661513) 1.00
es/codegen/with-parser/colors 44367 ns/iter (± 556) 44008 ns/iter (± 622) 1.01
es/codegen/with-parser/large 481439 ns/iter (± 589) 483448 ns/iter (± 1112) 1.00
es/minify/libraries/antd 1164414145 ns/iter (± 20707759) 1252974482 ns/iter (± 23989848) 0.93
es/minify/libraries/d3 241387672 ns/iter (± 830556) 254584346 ns/iter (± 1997705) 0.95
es/minify/libraries/echarts 939356005 ns/iter (± 21854255) 998744566 ns/iter (± 17282105) 0.94
es/minify/libraries/jquery 73943968 ns/iter (± 392267) 75426373 ns/iter (± 270202) 0.98
es/minify/libraries/lodash 88179914 ns/iter (± 243949) 89575297 ns/iter (± 387307) 0.98
es/minify/libraries/moment 43351403 ns/iter (± 67003) 44343323 ns/iter (± 296778) 0.98
es/minify/libraries/react 15942140 ns/iter (± 39732) 16312154 ns/iter (± 98139) 0.98
es/minify/libraries/terser 188833816 ns/iter (± 1308249) 194964564 ns/iter (± 1907059) 0.97
es/minify/libraries/three 322540814 ns/iter (± 1545306) 342472861 ns/iter (± 3757310) 0.94
es/minify/libraries/typescript 2322619958 ns/iter (± 19205112) 2426047133 ns/iter (± 23814547) 0.96
es/minify/libraries/victory 477170121 ns/iter (± 5296910) 520217848 ns/iter (± 10242782) 0.92
es/minify/libraries/vue 106603724 ns/iter (± 187681) 109564189 ns/iter (± 538927) 0.97
es/visitor/compare/clone 1963115 ns/iter (± 6619) 1957099 ns/iter (± 13290) 1.00
es/visitor/compare/visit_mut_span 2296233 ns/iter (± 7691) 2282137 ns/iter (± 2421) 1.01
es/visitor/compare/visit_mut_span_panic 2336948 ns/iter (± 5690) 2336601 ns/iter (± 12385) 1.00
es/visitor/compare/fold_span 3408692 ns/iter (± 9225) 3370300 ns/iter (± 30996) 1.01
es/visitor/compare/fold_span_panic 3531585 ns/iter (± 7501) 3491401 ns/iter (± 12322) 1.01
es/lexer/colors 12753 ns/iter (± 9) 12843 ns/iter (± 16) 0.99
es/lexer/angular 6130795 ns/iter (± 4651) 6051190 ns/iter (± 3121) 1.01
es/lexer/backbone 802348 ns/iter (± 300) 787850 ns/iter (± 1609) 1.02
es/lexer/jquery 4481176 ns/iter (± 7020) 4403817 ns/iter (± 2790) 1.02
es/lexer/jquery mobile 6880827 ns/iter (± 11154) 6773772 ns/iter (± 12413) 1.02
es/lexer/mootools 3579966 ns/iter (± 7580) 3500888 ns/iter (± 1988) 1.02
es/lexer/underscore 663843 ns/iter (± 188) 655314 ns/iter (± 394) 1.01
es/lexer/three 21156134 ns/iter (± 52888) 20877798 ns/iter (± 38758) 1.01
es/lexer/yui 3854811 ns/iter (± 5115) 3816714 ns/iter (± 1815) 1.01
es/parser/colors 26624 ns/iter (± 65) 26740 ns/iter (± 45) 1.00
es/parser/angular 13479489 ns/iter (± 176043) 13724604 ns/iter (± 320615) 0.98
es/parser/backbone 1986523 ns/iter (± 11181) 2003133 ns/iter (± 9822) 0.99
es/parser/jquery 10849861 ns/iter (± 52221) 11184659 ns/iter (± 204499) 0.97
es/parser/jquery mobile 16733380 ns/iter (± 73745) 16953652 ns/iter (± 174092) 0.99
es/parser/mootools 8343666 ns/iter (± 13748) 8435915 ns/iter (± 22774) 0.99
es/parser/underscore 1706197 ns/iter (± 9654) 1727653 ns/iter (± 28759) 0.99
es/parser/three 47335276 ns/iter (± 581729) 49682504 ns/iter (± 647074) 0.95
es/parser/yui 8238663 ns/iter (± 42346) 8321583 ns/iter (± 121842) 0.99
es/preset-env/usage/builtin_type 136848 ns/iter (± 31955) 138570 ns/iter (± 33539) 0.99
es/preset-env/usage/property 16810 ns/iter (± 103) 16548 ns/iter (± 59) 1.02
es/resolver/typescript 91721099 ns/iter (± 1794596) 90806722 ns/iter (± 1034459) 1.01
es/fixer/typescript 65907863 ns/iter (± 700084) 66342065 ns/iter (± 1011265) 0.99
es/hygiene/typescript 134848588 ns/iter (± 1068359) 136892301 ns/iter (± 1655858) 0.99
es/resolver_with_hygiene/typescript 246598823 ns/iter (± 2748795) 253716136 ns/iter (± 2808018) 0.97
es/visitor/base-perf/module_clone 60191 ns/iter (± 317) 59576 ns/iter (± 330) 1.01
es/visitor/base-perf/fold_empty 63741 ns/iter (± 222) 63494 ns/iter (± 320) 1.00
es/visitor/base-perf/fold_noop_impl_all 64016 ns/iter (± 307) 63882 ns/iter (± 254) 1.00
es/visitor/base-perf/fold_noop_impl_vec 64191 ns/iter (± 160) 64516 ns/iter (± 253) 0.99
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 0) 57 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed_clone 39 ns/iter (± 0) 40 ns/iter (± 0) 0.97
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 0) 111 ns/iter (± 0) 0.98
es/visitor/base-perf/boxing_unboxed 77 ns/iter (± 0) 78 ns/iter (± 0) 0.99
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2512 ns/iter (± 14) 2497 ns/iter (± 26) 1.01
es/base/parallel/resolver/typescript 3823163795 ns/iter (± 302079470) 3895918900 ns/iter (± 268116074) 0.98
es/base/parallel/hygiene/typescript 1459577169 ns/iter (± 16035997) 1481648728 ns/iter (± 19895026) 0.99
misc/visitors/time-complexity/time 5 137 ns/iter (± 0) 145 ns/iter (± 2) 0.94
misc/visitors/time-complexity/time 10 390 ns/iter (± 2) 413 ns/iter (± 4) 0.94
misc/visitors/time-complexity/time 15 661 ns/iter (± 3) 662 ns/iter (± 3) 1.00
misc/visitors/time-complexity/time 20 1056 ns/iter (± 7) 1048 ns/iter (± 2) 1.01
misc/visitors/time-complexity/time 40 3497 ns/iter (± 45) 3483 ns/iter (± 4) 1.00
misc/visitors/time-complexity/time 60 7224 ns/iter (± 45) 7209 ns/iter (± 24) 1.00
es/full-target/es2016 237598 ns/iter (± 5622) 233950 ns/iter (± 601) 1.02
es/full-target/es2017 226391 ns/iter (± 559) 221452 ns/iter (± 557) 1.02
es/full-target/es2018 214236 ns/iter (± 441) 210042 ns/iter (± 655) 1.02
es2020_nullish_coalescing 71992 ns/iter (± 487) 70947 ns/iter (± 310) 1.01
es2020_optional_chaining 81982 ns/iter (± 336) 81595 ns/iter (± 307) 1.00
es2022_class_properties 118881 ns/iter (± 319) 118243 ns/iter (± 370) 1.01
es2018_object_rest_spread 74375 ns/iter (± 162) 75998 ns/iter (± 407) 0.98
es2019_optional_catch_binding 63922 ns/iter (± 172) 64212 ns/iter (± 350) 1.00
es2017_async_to_generator 63662 ns/iter (± 421) 64384 ns/iter (± 216) 0.99
es2016_exponentiation 69427 ns/iter (± 554) 69246 ns/iter (± 194) 1.00
es2015_arrow 71766 ns/iter (± 206) 71883 ns/iter (± 203) 1.00
es2015_block_scoped_fn 68531 ns/iter (± 182) 69291 ns/iter (± 244) 0.99
es2015_block_scoping 124311 ns/iter (± 609) 124249 ns/iter (± 499) 1.00

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

Please sign in to comment.