Skip to content

Commit

Permalink
fix(es/minifier): Fix detection of direct eval (#6215)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Oct 21, 2022
1 parent dc615b1 commit 780b5de
Show file tree
Hide file tree
Showing 15 changed files with 3,483 additions and 1,024 deletions.
12 changes: 6 additions & 6 deletions crates/swc_ecma_minifier/tests/benches-full/terser.js
Expand Up @@ -3,7 +3,7 @@
'exports',
'source-map'
], factory) : factory((global1 = 'undefined' != typeof globalThis ? globalThis : global1 || self).Terser = {}, global1.sourceMap);
}(this, function(exports, MOZ_SourceMap) {
}(this, function(exports1, MOZ_SourceMap) {
'use strict';
let mangle_options;
var def_is_string, def_find_defs, MOZ_SourceMap__default = MOZ_SourceMap && 'object' == typeof MOZ_SourceMap && 'default' in MOZ_SourceMap ? MOZ_SourceMap : {
Expand Down Expand Up @@ -4085,14 +4085,14 @@
AST_Node.from_mozilla_ast = function(node) {
var save_stack = FROM_MOZ_STACK;
FROM_MOZ_STACK = [];
var ast1 = from_moz(node);
return FROM_MOZ_STACK = save_stack, ast1;
var ast = from_moz(node);
return FROM_MOZ_STACK = save_stack, ast;
};
var TO_MOZ_STACK = null;
function to_moz(node) {
null === TO_MOZ_STACK && (TO_MOZ_STACK = []), TO_MOZ_STACK.push(node);
var ast1 = null != node ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
return TO_MOZ_STACK.pop(), 0 === TO_MOZ_STACK.length && (TO_MOZ_STACK = null), ast1;
var ast = null != node ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
return TO_MOZ_STACK.pop(), 0 === TO_MOZ_STACK.length && (TO_MOZ_STACK = null), ast;
}
function to_moz_in_destructuring() {
for(var i = TO_MOZ_STACK.length; i--;)if (TO_MOZ_STACK[i] instanceof AST_Destructuring) return !0;
Expand Down Expand Up @@ -18048,5 +18048,5 @@
return error.defs;
}
}
exports._default_options = _default_options, exports._run_cli = run_cli, exports.minify = minify;
exports1._default_options = _default_options, exports1._run_cli = run_cli, exports1.minify = minify;
});

Large diffs are not rendered by default.

@@ -0,0 +1 @@
{}

Large diffs are not rendered by default.

Expand Up @@ -908,7 +908,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down Expand Up @@ -1209,8 +1209,8 @@
if ("%" === t && "%" !== e) throw new n("invalid intrinsic syntax, expected closing `%`");
if ("%" === e && "%" !== t) throw new n("invalid intrinsic syntax, expected opening `%`");
var o = [];
return P(r, O, function(r, t, e, n1) {
o[o.length] = e ? P(n1, w, "$1") : t || r;
return P(r, O, function(r, t, e, n) {
o[o.length] = e ? P(n, w, "$1") : t || r;
}), o;
}, j = function(r, t) {
var o, e = r;
Expand Down Expand Up @@ -1293,7 +1293,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down Expand Up @@ -1607,8 +1607,8 @@
if ("%" === t && "%" !== e) throw new n("invalid intrinsic syntax, expected closing `%`");
if ("%" === e && "%" !== t) throw new n("invalid intrinsic syntax, expected opening `%`");
var o = [];
return m(r, h, function(r, t, e, n1) {
o[o.length] = e ? m(n1, O, "$1") : t || r;
return m(r, h, function(r, t, e, n) {
o[o.length] = e ? m(n, O, "$1") : t || r;
}), o;
}, E = function(r, t) {
var o, e = r;
Expand Down Expand Up @@ -2252,7 +2252,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down
1,972 changes: 986 additions & 986 deletions crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js

Large diffs are not rendered by default.

@@ -1,6 +1,6 @@
var c = a;
c();
var d = a.b;
d();
var e = eval;
e();
var v = a;
v();
var r = a.b;
r();
var b = eval;
b();
@@ -1,9 +1,9 @@
(function (foo) {
(function(foo) {
foo();
(0, foo.bar)();
(0, eval)("console.log(foo);");
})();
(function (foo) {
(function(foo) {
var eval = console;
foo();
(0, foo.bar)();
Expand Down
@@ -1,11 +1,11 @@
(function(foo) {
(0, foo)();
(0, foo.bar)();
(function(o) {
(0, o)();
(0, o.bar)();
(0, eval)("console.log(foo);");
})();
(function(foo) {
(function(o) {
var eval = console;
(0, foo)();
(0, foo.bar)();
(0, o)();
(0, o.bar)();
(0, eval)("console.log(foo);");
})();
Expand Up @@ -9,8 +9,8 @@ function f1() {
eval(x, y, z, e);
}
function p1() {
var a = foo(), b = bar(), eval = baz();
return a + b + eval;
var a = foo(), o = bar(), eval = baz();
return a + o + eval;
}
function p2() {
var a = foo(), b = bar(), eval = baz;
Expand Down
@@ -1,5 +1,5 @@
function f1(a, eval, c, d, e) {
return a("c") + eval;
function f1(n, eval, c, r, t) {
return n("c") + eval;
}
function f2(a, b, c, d, e) {
return a + eval("c");
Expand Down
@@ -1,5 +1,5 @@
function f1(a, eval, c, d, e) {
return a("c") + eval;
function f1(n, eval, c, r, t) {
return n("c") + eval;
}
function f2(a, b, c, d, e) {
return a + eval("c");
Expand Down
8 changes: 6 additions & 2 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs
Expand Up @@ -137,7 +137,7 @@ impl Scope {

for id in queue {
let fid = fast_id(id.clone());
if to.get(&fid).is_some() || previous.get(&fid).is_some() {
if to.get(&fid).is_some() || previous.get(&fid).is_some() || id.0 == js_word!("eval") {
continue;
}

Expand Down Expand Up @@ -273,7 +273,11 @@ impl Scope {

for id in queue {
let fid = fast_id(id.clone());
if preserved.contains(&id) || to.get(&fid).is_some() || previous.get(&fid).is_some() {
if preserved.contains(&id)
|| to.get(&fid).is_some()
|| previous.get(&fid).is_some()
|| id.0 == js_word!("eval")
{
continue;
}

Expand Down
14 changes: 11 additions & 3 deletions crates/swc_ecma_transforms_base/src/rename/eval.rs
Expand Up @@ -31,9 +31,17 @@ impl Visit for EvalFinder {

fn visit_export_namespace_specifier(&mut self, _: &ExportNamespaceSpecifier) {}

fn visit_ident(&mut self, i: &Ident) {
if i.sym == js_word!("eval") {
self.found = true;
fn visit_callee(&mut self, c: &Callee) {
c.visit_children_with(self);

if let Callee::Expr(e) = c {
if let Expr::Ident(Ident {
sym: js_word!("eval"),
..
}) = &**e
{
self.found = true
}
}
}

Expand Down

1 comment on commit 780b5de

@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: 780b5de Previous: 055bdcd Ratio
es/full/bugs-1 362156 ns/iter (± 56792) 331388 ns/iter (± 18290) 1.09
es/full/minify/libraries/antd 1905278662 ns/iter (± 45290223) 1757086065 ns/iter (± 33848513) 1.08
es/full/minify/libraries/d3 431306742 ns/iter (± 27361648) 383448670 ns/iter (± 13722779) 1.12
es/full/minify/libraries/echarts 1646595475 ns/iter (± 40406738) 1509419592 ns/iter (± 20301409) 1.09
es/full/minify/libraries/jquery 113632814 ns/iter (± 3929219) 96148711 ns/iter (± 3055092) 1.18
es/full/minify/libraries/lodash 132863189 ns/iter (± 5172453) 113435565 ns/iter (± 2669397) 1.17
es/full/minify/libraries/moment 64289220 ns/iter (± 1760647) 56748501 ns/iter (± 1288480) 1.13
es/full/minify/libraries/react 21836183 ns/iter (± 1078776) 19299921 ns/iter (± 235824) 1.13
es/full/minify/libraries/terser 338215637 ns/iter (± 5250413) 291880484 ns/iter (± 10210176) 1.16
es/full/minify/libraries/three 590452716 ns/iter (± 8472894) 536764954 ns/iter (± 30908826) 1.10
es/full/minify/libraries/typescript 3598037442 ns/iter (± 100117870) 3284782543 ns/iter (± 17877936) 1.10
es/full/minify/libraries/victory 849814878 ns/iter (± 21110988) 786072433 ns/iter (± 16538391) 1.08
es/full/minify/libraries/vue 170845023 ns/iter (± 5337223) 145574692 ns/iter (± 3116369) 1.17
es/full/codegen/es3 33130 ns/iter (± 1244) 32422 ns/iter (± 855) 1.02
es/full/codegen/es5 34552 ns/iter (± 8896) 32416 ns/iter (± 726) 1.07
es/full/codegen/es2015 33290 ns/iter (± 694) 32399 ns/iter (± 907) 1.03
es/full/codegen/es2016 33271 ns/iter (± 684) 32408 ns/iter (± 839) 1.03
es/full/codegen/es2017 33251 ns/iter (± 933) 32398 ns/iter (± 1095) 1.03
es/full/codegen/es2018 33219 ns/iter (± 2801) 32545 ns/iter (± 790) 1.02
es/full/codegen/es2019 33554 ns/iter (± 3449) 32449 ns/iter (± 867) 1.03
es/full/codegen/es2020 33112 ns/iter (± 469) 32456 ns/iter (± 901) 1.02
es/full/all/es3 197220625 ns/iter (± 9392496) 184123511 ns/iter (± 4985787) 1.07
es/full/all/es5 186152444 ns/iter (± 9740796) 174949875 ns/iter (± 4302546) 1.06
es/full/all/es2015 145834505 ns/iter (± 6294959) 139885585 ns/iter (± 5485638) 1.04
es/full/all/es2016 145550651 ns/iter (± 6081475) 139403394 ns/iter (± 3804846) 1.04
es/full/all/es2017 146951872 ns/iter (± 10047126) 139107902 ns/iter (± 4751990) 1.06
es/full/all/es2018 142365476 ns/iter (± 9353307) 137613286 ns/iter (± 4111949) 1.03
es/full/all/es2019 142297320 ns/iter (± 6528360) 136916444 ns/iter (± 3078756) 1.04
es/full/all/es2020 137525955 ns/iter (± 9942661) 131477295 ns/iter (± 4437850) 1.05
es/full/parser 707316 ns/iter (± 41920) 681184 ns/iter (± 24786) 1.04
es/full/base/fixer 26533 ns/iter (± 1815) 25390 ns/iter (± 1829) 1.05
es/full/base/resolver_and_hygiene 92376 ns/iter (± 5287) 89863 ns/iter (± 3205) 1.03
serialization of ast node 213 ns/iter (± 5) 211 ns/iter (± 8) 1.01
serialization of serde 233 ns/iter (± 6) 216 ns/iter (± 7) 1.08

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

Please sign in to comment.