aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compress.js38
-rw-r--r--test/compress/drop-unused.js150
2 files changed, 113 insertions, 75 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 7f1b46e3..ce0fbdd5 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2487,6 +2487,20 @@ merge(Compressor.prototype, {
// pass 3: we should drop declarations not in_use
var tt = new TreeTransformer(
function before(node, descend, in_list) {
+ var parent = tt.parent();
+ if (drop_vars) {
+ var sym = assign_as_unused(node);
+ if (sym instanceof AST_SymbolRef
+ && !(sym.definition().id in in_use_ids)) {
+ if (node instanceof AST_Assign) {
+ return maintain_this_binding(parent, node, node.right.transform(tt));
+ }
+ return make_node(AST_Number, node, {
+ value: 0
+ });
+ }
+ }
+ if (scope !== self) return;
if (node instanceof AST_Function
&& node.name
&& !compressor.option("keep_fnames")) {
@@ -2520,9 +2534,7 @@ merge(Compressor.prototype, {
def.eliminated++;
return make_node(AST_EmptyStatement, node);
}
- return node;
}
- var parent = tt.parent();
if (node instanceof AST_Definitions && !(parent instanceof AST_ForIn && parent.init === node)) {
// place uninitialized names at the start
var body = [], head = [], tail = [];
@@ -2602,18 +2614,6 @@ merge(Compressor.prototype, {
});
}
}
- if (drop_vars) {
- var sym = assign_as_unused(node);
- if (sym instanceof AST_SymbolRef
- && !(sym.definition().id in in_use_ids)) {
- if (node instanceof AST_Assign) {
- return maintain_this_binding(parent, node, node.right.transform(tt));
- }
- return make_node(AST_Number, node, {
- value: 0
- });
- }
- }
// certain combination of unused name + side effect leads to:
// https://github.com/mishoo/UglifyJS2/issues/44
// https://github.com/mishoo/UglifyJS2/issues/1830
@@ -2645,8 +2645,13 @@ merge(Compressor.prototype, {
}
return node;
}
- if (node instanceof AST_Scope && node !== self)
+ if (node instanceof AST_Scope) {
+ var save_scope = scope;
+ scope = node;
+ descend(node, this);
+ scope = save_scope;
return node;
+ }
function template(sym) {
return {
@@ -2662,8 +2667,7 @@ merge(Compressor.prototype, {
function scan_ref_scoped(node, descend) {
var sym;
- if (scope === self
- && (sym = assign_as_unused(node)) instanceof AST_SymbolRef
+ if ((sym = assign_as_unused(node)) instanceof AST_SymbolRef
&& self.variables.get(sym.name) === sym.definition()) {
if (node instanceof AST_Assign) node.right.walk(tw);
return true;
diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js
index 189a0008..f9ef7877 100644
--- a/test/compress/drop-unused.js
+++ b/test/compress/drop-unused.js
@@ -179,7 +179,9 @@ keep_fnames: {
}
drop_assign: {
- options = { unused: true };
+ options = {
+ unused: true,
+ }
input: {
function f1() {
var a;
@@ -200,7 +202,7 @@ drop_assign: {
var a;
return function() {
a = 1;
- }
+ };
}
}
expect: {
@@ -217,16 +219,17 @@ drop_assign: {
return 1;
}
function f5() {
- var a;
return function() {
- a = 1;
- }
+ 1;
+ };
}
}
}
keep_assign: {
- options = { unused: "keep_assign" };
+ options = {
+ unused: "keep_assign",
+ }
input: {
function f1() {
var a;
@@ -247,7 +250,7 @@ keep_assign: {
var a;
return function() {
a = 1;
- }
+ };
}
}
expect: {
@@ -270,19 +273,22 @@ keep_assign: {
var a;
return function() {
a = 1;
- }
+ };
}
}
}
drop_toplevel_funcs: {
- options = { toplevel: "funcs", unused: true };
+ options = {
+ toplevel: "funcs",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -298,13 +304,16 @@ drop_toplevel_funcs: {
}
drop_toplevel_vars: {
- options = { toplevel: "vars", unused: true };
+ options = {
+ toplevel: "vars",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -312,11 +321,10 @@ drop_toplevel_vars: {
console.log(b = 3);
}
expect: {
- var c = g;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
2;
function g() {}
@@ -326,13 +334,17 @@ drop_toplevel_vars: {
}
drop_toplevel_vars_fargs: {
- options = { keep_fargs: false, toplevel: "vars", unused: true };
+ options = {
+ keep_fargs: false,
+ toplevel: "vars",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -340,11 +352,10 @@ drop_toplevel_vars_fargs: {
console.log(b = 3);
}
expect: {
- var c = g;
function f() {
return function() {
- c = 2;
- }
+ 2;
+ };
}
2;
function g() {}
@@ -354,13 +365,16 @@ drop_toplevel_vars_fargs: {
}
drop_toplevel_all: {
- options = { toplevel: true, unused: true };
+ options = {
+ toplevel: true,
+ unused: true
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -374,13 +388,16 @@ drop_toplevel_all: {
}
drop_toplevel_retain: {
- options = { top_retain: "f,a,o", unused: true };
+ options = {
+ top_retain: "f,a,o",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -388,26 +405,28 @@ drop_toplevel_retain: {
console.log(b = 3);
}
expect: {
- var a, c = g;
+ var a;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
a = 2;
- function g() {}
console.log(3);
}
}
drop_toplevel_retain_array: {
- options = { top_retain: [ "f", "a", "o" ], unused: true };
+ options = {
+ top_retain: [ "f", "a", "o" ],
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -415,26 +434,28 @@ drop_toplevel_retain_array: {
console.log(b = 3);
}
expect: {
- var a, c = g;
+ var a;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
a = 2;
- function g() {}
console.log(3);
}
}
drop_toplevel_retain_regex: {
- options = { top_retain: /^[fao]$/, unused: true };
+ options = {
+ top_retain: /^[fao]$/,
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -442,26 +463,29 @@ drop_toplevel_retain_regex: {
console.log(b = 3);
}
expect: {
- var a, c = g;
+ var a;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
a = 2;
- function g() {}
console.log(3);
}
}
drop_toplevel_all_retain: {
- options = { toplevel: true, top_retain: "f,a,o", unused: true };
+ options = {
+ toplevel: true,
+ top_retain: "f,a,o",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -469,26 +493,29 @@ drop_toplevel_all_retain: {
console.log(b = 3);
}
expect: {
- var a, c = g;
+ var a;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
a = 2;
- function g() {}
console.log(3);
}
}
drop_toplevel_funcs_retain: {
- options = { toplevel: "funcs", top_retain: "f,a,o", unused: true };
+ options = {
+ toplevel: "funcs",
+ top_retain: "f,a,o",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -500,7 +527,7 @@ drop_toplevel_funcs_retain: {
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -509,13 +536,17 @@ drop_toplevel_funcs_retain: {
}
drop_toplevel_vars_retain: {
- options = { toplevel: "vars", top_retain: "f,a,o", unused: true };
+ options = {
+ toplevel: "vars",
+ top_retain: "f,a,o",
+ unused: true,
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -523,11 +554,11 @@ drop_toplevel_vars_retain: {
console.log(b = 3);
}
expect: {
- var a, c = g;
+ var a;
function f(d) {
return function() {
- c = 2;
- }
+ 2;
+ };
}
a = 2;
function g() {}
@@ -537,13 +568,16 @@ drop_toplevel_vars_retain: {
}
drop_toplevel_keep_assign: {
- options = { toplevel: true, unused: "keep_assign" };
+ options = {
+ toplevel: true,
+ unused: "keep_assign",
+ }
input: {
var a, b = 1, c = g;
function f(d) {
return function() {
c = 2;
- }
+ };
}
a = 2;
function g() {}
@@ -753,11 +787,11 @@ issue_1583: {
expect: {
function m(t) {
(function(e) {
- t = function() {
+ (function() {
return (function(a) {
return function(a) {};
})();
- }();
+ })();
})();
}
}