Skip to content

Commit

Permalink
fix corner case in evaluate (#4800)
Browse files Browse the repository at this point in the history
fixes #4799
  • Loading branch information
alexlamsl committed Mar 18, 2021
1 parent 48c46fa commit 2508481
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/compress.js
Expand Up @@ -1429,6 +1429,7 @@ merge(Compressor.prototype, {
}
}

var identifier_atom = makePredicate("Infinity NaN undefined");
function is_lhs_read_only(lhs, compressor) {
if (lhs instanceof AST_ObjectIdentity) return true;
if (lhs instanceof AST_PropAccess) {
Expand Down Expand Up @@ -1616,13 +1617,6 @@ merge(Compressor.prototype, {
|| compressor.option("unsafe") && global_names[this.name];
});

var identifier_atom = makePredicate("Infinity NaN undefined");
function is_identifier_atom(node) {
return node instanceof AST_Infinity
|| node instanceof AST_NaN
|| node instanceof AST_Undefined;
}

function declarations_only(node) {
return all(node.definitions, function(var_def) {
return !var_def.value;
Expand Down Expand Up @@ -9438,11 +9432,7 @@ merge(Compressor.prototype, {
OPT(AST_UnaryPrefix, function(self, compressor) {
var op = self.operator;
var exp = self.expression;
if (compressor.option("evaluate")
&& op == "delete"
&& !(exp instanceof AST_SymbolRef
|| exp instanceof AST_PropAccess
|| is_identifier_atom(exp))) {
if (compressor.option("evaluate") && op == "delete" && !may_not_delete(exp)) {
return make_sequence(self, [ exp, make_node(AST_True, self) ]).optimize(compressor);
}
if (compressor.option("sequences") && can_lift()) {
Expand Down Expand Up @@ -9496,10 +9486,19 @@ merge(Compressor.prototype, {
return op == "-" && (exp instanceof AST_Number || exp instanceof AST_Infinity)
? self : try_evaluate(compressor, self);

function may_not_delete(node) {
return node instanceof AST_Infinity
|| node instanceof AST_NaN
|| node instanceof AST_NewTarget
|| node instanceof AST_PropAccess
|| node instanceof AST_SymbolRef
|| node instanceof AST_Undefined;
}

function can_lift() {
switch (op) {
case "delete":
return !is_identifier_atom(exp.tail_node());
return !may_not_delete(exp.tail_node());
case "typeof":
return !is_undeclared_ref(exp.tail_node());
default:
Expand Down
18 changes: 18 additions & 0 deletions test/compress/functions.js
Expand Up @@ -5851,6 +5851,24 @@ new_target_collapse_vars: {
node_version: ">=6"
}

new_target_delete: {
options = {
evaluate: true,
}
input: {
new function() {
console.log(delete new.target);
}();
}
expect: {
new function() {
console.log(delete new.target);
}();
}
expect_stdout: true
node_version: ">=6"
}

new_target_reduce_vars: {
options = {
evaluate: true,
Expand Down

0 comments on commit 2508481

Please sign in to comment.