[Pkg-javascript-commits] [uglifyjs] 154/228: fix `delete` related issues in `collapse_vars` and `reduce_vars` (#1689)
Jonas Smedegaard
dr at jones.dk
Sat Apr 15 14:25:26 UTC 2017
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository uglifyjs.
commit 861a79ac9fdb2cdbb54054306eb896e2c134af73
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date: Sun Mar 26 19:14:30 2017 +0800
fix `delete` related issues in `collapse_vars` and `reduce_vars` (#1689)
---
lib/compress.js | 44 ++++++++++++++++--------------------------
test/compress/collapse_vars.js | 3 ++-
test/compress/reduce_vars.js | 27 ++++++++++++++++++++++++++
3 files changed, 46 insertions(+), 28 deletions(-)
diff --git a/lib/compress.js b/lib/compress.js
index a617adb..83486b6 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1207,8 +1207,10 @@ merge(Compressor.prototype, {
node.DEFMETHOD("is_string", func);
});
+ var unary_side_effects = makePredicate("delete ++ --");
+
function isLHS(node, parent) {
- return parent instanceof AST_Unary && (parent.operator == "++" || parent.operator == "--")
+ return parent instanceof AST_Unary && unary_side_effects(parent.operator)
|| parent instanceof AST_Assign && parent.left === node;
}
@@ -1643,9 +1645,7 @@ merge(Compressor.prototype, {
|| this.alternative.has_side_effects(compressor);
});
def(AST_Unary, function(compressor){
- return this.operator == "delete"
- || this.operator == "++"
- || this.operator == "--"
+ return unary_side_effects(this.operator)
|| this.expression.has_side_effects(compressor);
});
def(AST_SymbolRef, function(compressor){
@@ -2196,26 +2196,19 @@ merge(Compressor.prototype, {
return node;
});
def(AST_Unary, function(compressor, first_in_statement){
- switch (this.operator) {
- case "delete":
- case "++":
- case "--":
- return this;
- case "typeof":
- if (this.expression instanceof AST_SymbolRef) return null;
- default:
- var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
- if (first_in_statement
- && this instanceof AST_UnaryPrefix
- && is_iife_call(expression)) {
- if (expression === this.expression && this.operator.length === 1) return this;
- return make_node(AST_UnaryPrefix, this, {
- operator: this.operator.length === 1 ? this.operator : "!",
- expression: expression
- });
- }
- return expression;
+ if (unary_side_effects(this.operator)) return this;
+ if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
+ var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
+ if (first_in_statement
+ && this instanceof AST_UnaryPrefix
+ && is_iife_call(expression)) {
+ if (expression === this.expression && this.operator.length === 1) return this;
+ return make_node(AST_UnaryPrefix, this, {
+ operator: this.operator.length === 1 ? this.operator : "!",
+ expression: expression
+ });
}
+ return expression;
});
def(AST_SymbolRef, function() {
return this.undeclared() ? this : null;
@@ -2945,10 +2938,7 @@ merge(Compressor.prototype, {
field = "left";
}
} else if (cdr instanceof AST_Call
- || cdr instanceof AST_Unary
- && cdr.operator != "delete"
- && cdr.operator != "++"
- && cdr.operator != "--") {
+ || cdr instanceof AST_Unary && !unary_side_effects(cdr.operator)) {
field = "expression";
} else break;
parent = cdr;
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js
index 2437ca5..4107707 100644
--- a/test/compress/collapse_vars.js
+++ b/test/compress/collapse_vars.js
@@ -894,7 +894,8 @@ collapse_vars_unary: {
}
expect: {
function f0(o, p) {
- delete o[p];
+ var x = o[p];
+ delete x;
}
function f1(n) {
return n > +!!n
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 943dd29..f4dd68d 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -1544,3 +1544,30 @@ issue_1670_6: {
}
expect_stdout: "1"
}
+
+unary_delete: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ unused: true,
+ }
+ input: {
+ var b = 10;
+ function f() {
+ var a;
+ if (delete a) b--;
+ }
+ f();
+ console.log(b);
+ }
+ expect: {
+ var b = 10;
+ function f() {
+ var a;
+ if (delete a) b--;
+ }
+ f();
+ console.log(b);
+ }
+ expect_stdout: true
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git
More information about the Pkg-javascript-commits
mailing list