Skip to content

Commit

Permalink
enhance unsafe side_effects (#4862)
Browse files Browse the repository at this point in the history
closes #4861
  • Loading branch information
alexlamsl committed Apr 22, 2021
1 parent bddb5a0 commit f46209b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/compress.js
Expand Up @@ -1638,7 +1638,7 @@ merge(Compressor.prototype, {
return node instanceof AST_SymbolRef && node.definition().undeclared;
}

var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Math Number parseFloat parseInt RangeError ReferenceError RegExp Object setInterval setTimeout String SyntaxError TypeError unescape URIError");
var global_names = makePredicate("Array Boolean clearInterval clearTimeout console Date decodeURI decodeURIComponent encodeURI encodeURIComponent Error escape eval EvalError Function isFinite isNaN JSON Map Math Number parseFloat parseInt RangeError ReferenceError RegExp Object Set setInterval setTimeout String SyntaxError TypeError unescape URIError WeakMap WeakSet");
AST_SymbolRef.DEFMETHOD("is_declared", function(compressor) {
return this.defined
|| !this.definition().undeclared
Expand Down Expand Up @@ -4800,10 +4800,14 @@ merge(Compressor.prototype, {
});

var global_pure_fns = makePredicate("Boolean decodeURI decodeURIComponent Date encodeURI encodeURIComponent Error escape EvalError isFinite isNaN Number Object parseFloat parseInt RangeError ReferenceError String SyntaxError TypeError unescape URIError");
var global_pure_constructors = makePredicate("Map Set WeakMap WeakSet");
AST_Call.DEFMETHOD("is_expr_pure", function(compressor) {
if (compressor.option("unsafe")) {
var expr = this.expression;
if (is_undeclared_ref(expr) && global_pure_fns[expr.name]) return true;
if (is_undeclared_ref(expr)) {
if (global_pure_fns[expr.name]) return true;
if (this instanceof AST_New && global_pure_constructors[expr.name]) return true;
}
if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression)) {
var static_fn = static_fns[expr.expression.name];
return static_fn && (static_fn[expr.property]
Expand Down
30 changes: 30 additions & 0 deletions test/compress/side_effects.js
Expand Up @@ -198,6 +198,36 @@ global_fns: {
]
}

global_constructors: {
options = {
side_effects: true,
unsafe: true,
}
input: {
Map;
new Map(console.log("foo"));
Set;
new Set(console.log("bar"));
WeakMap;
new WeakMap(console.log("baz"));
WeakSet;
new WeakSet(console.log("moo"));
}
expect: {
console.log("foo");
console.log("bar");
console.log("baz");
console.log("moo");
}
expect_stdout: [
"foo",
"bar",
"baz",
"moo",
]
node_version: ">=0.12"
}

unsafe_builtin_1: {
options = {
side_effects: true,
Expand Down

0 comments on commit f46209b

Please sign in to comment.