Skip to content

Commit 304db15

Browse files
authored
fix corner case in ie8 & rename (#3223)
1 parent 7cf72b8 commit 304db15

File tree

5 files changed

+131
-74
lines changed

5 files changed

+131
-74
lines changed

lib/ast.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ var AST_Node = DEFNODE("Node", "start end", {
118118
}
119119
}, null);
120120

121-
AST_Node.warn_function = null;
122121
AST_Node.warn = function(txt, props) {
123-
if (AST_Node.warn_function)
124-
AST_Node.warn_function(string_template(txt, props));
122+
if (AST_Node.warn_function) AST_Node.warn_function(string_template(txt, props));
125123
};
126124

127125
/* -----[ statements ]----- */
@@ -207,8 +205,7 @@ var AST_LabeledStatement = DEFNODE("LabeledStatement", "label", {
207205
var label = node.label;
208206
var def = this.label;
209207
node.walk(new TreeWalker(function(node) {
210-
if (node instanceof AST_LoopControl
211-
&& node.label && node.label.thedef === def) {
208+
if (node instanceof AST_LoopControl && node.label && node.label.thedef === def) {
212209
node.label.thedef = label;
213210
label.references.push(node);
214211
}

lib/propmangle.js

+37-55
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,41 @@ function find_builtins(reserved) {
5353
"-Infinity",
5454
"undefined",
5555
].forEach(add);
56-
[ Object, Array, Function, Number,
57-
String, Boolean, Error, Math,
58-
Date, RegExp
56+
[
57+
Array,
58+
Boolean,
59+
Date,
60+
Error,
61+
Function,
62+
Math,
63+
Number,
64+
Object,
65+
RegExp,
66+
String,
5967
].forEach(function(ctor) {
6068
Object.getOwnPropertyNames(ctor).map(add);
6169
if (ctor.prototype) {
6270
Object.getOwnPropertyNames(ctor.prototype).map(add);
6371
}
6472
});
73+
6574
function add(name) {
6675
push_uniq(reserved, name);
6776
}
6877
}
6978

7079
function reserve_quoted_keys(ast, reserved) {
71-
function add(name) {
72-
push_uniq(reserved, name);
73-
}
74-
7580
ast.walk(new TreeWalker(function(node) {
7681
if (node instanceof AST_ObjectKeyVal && node.quote) {
7782
add(node.key);
7883
} else if (node instanceof AST_Sub) {
7984
addStrings(node.property, add);
8085
}
8186
}));
87+
88+
function add(name) {
89+
push_uniq(reserved, name);
90+
}
8291
}
8392

8493
function addStrings(node, add) {
@@ -127,10 +136,8 @@ function mangle_properties(ast, options) {
127136
// note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
128137
// the same as passing an empty string.
129138
var debug = options.debug !== false;
130-
var debug_name_suffix;
131-
if (debug) {
132-
debug_name_suffix = (options.debug === true ? "" : options.debug);
133-
}
139+
var debug_suffix;
140+
if (debug) debug_suffix = options.debug === true ? "" : options.debug;
134141

135142
var names_to_mangle = [];
136143
var unmangleable = [];
@@ -139,18 +146,14 @@ function mangle_properties(ast, options) {
139146
ast.walk(new TreeWalker(function(node) {
140147
if (node instanceof AST_ObjectKeyVal) {
141148
add(node.key);
142-
}
143-
else if (node instanceof AST_ObjectProperty) {
149+
} else if (node instanceof AST_ObjectProperty) {
144150
// setter or getter, since KeyVal is handled above
145151
add(node.key.name);
146-
}
147-
else if (node instanceof AST_Dot) {
152+
} else if (node instanceof AST_Dot) {
148153
add(node.property);
149-
}
150-
else if (node instanceof AST_Sub) {
154+
} else if (node instanceof AST_Sub) {
151155
addStrings(node.property, add);
152-
}
153-
else if (node instanceof AST_Call
156+
} else if (node instanceof AST_Call
154157
&& node.expression.print_to_string() == "Object.defineProperty") {
155158
addStrings(node.args[1], add);
156159
}
@@ -160,18 +163,14 @@ function mangle_properties(ast, options) {
160163
return ast.transform(new TreeTransformer(function(node) {
161164
if (node instanceof AST_ObjectKeyVal) {
162165
node.key = mangle(node.key);
163-
}
164-
else if (node instanceof AST_ObjectProperty) {
166+
} else if (node instanceof AST_ObjectProperty) {
165167
// setter or getter
166168
node.key.name = mangle(node.key.name);
167-
}
168-
else if (node instanceof AST_Dot) {
169+
} else if (node instanceof AST_Dot) {
169170
node.property = mangle(node.property);
170-
}
171-
else if (!options.keep_quoted && node instanceof AST_Sub) {
171+
} else if (!options.keep_quoted && node instanceof AST_Sub) {
172172
node.property = mangleStrings(node.property);
173-
}
174-
else if (node instanceof AST_Call
173+
} else if (node instanceof AST_Call
175174
&& node.expression.print_to_string() == "Object.defineProperty") {
176175
node.args[1] = mangleStrings(node.args[1]);
177176
}
@@ -182,52 +181,37 @@ function mangle_properties(ast, options) {
182181
function can_mangle(name) {
183182
if (unmangleable.indexOf(name) >= 0) return false;
184183
if (reserved.indexOf(name) >= 0) return false;
185-
if (options.only_cache) {
186-
return cache.has(name);
187-
}
184+
if (options.only_cache) return cache.has(name);
188185
if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
189186
return true;
190187
}
191188

192189
function should_mangle(name) {
193190
if (regex && !regex.test(name)) return false;
194191
if (reserved.indexOf(name) >= 0) return false;
195-
return cache.has(name)
196-
|| names_to_mangle.indexOf(name) >= 0;
192+
return cache.has(name) || names_to_mangle.indexOf(name) >= 0;
197193
}
198194

199195
function add(name) {
200-
if (can_mangle(name))
201-
push_uniq(names_to_mangle, name);
202-
203-
if (!should_mangle(name)) {
204-
push_uniq(unmangleable, name);
205-
}
196+
if (can_mangle(name)) push_uniq(names_to_mangle, name);
197+
if (!should_mangle(name)) push_uniq(unmangleable, name);
206198
}
207199

208200
function mangle(name) {
209201
if (!should_mangle(name)) {
210202
return name;
211203
}
212-
213204
var mangled = cache.get(name);
214205
if (!mangled) {
215206
if (debug) {
216207
// debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
217-
var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_";
218-
219-
if (can_mangle(debug_mangled)) {
220-
mangled = debug_mangled;
221-
}
208+
var debug_mangled = "_$" + name + "$" + debug_suffix + "_";
209+
if (can_mangle(debug_mangled)) mangled = debug_mangled;
222210
}
223-
224211
// either debug mode is off, or it is on and we could not use the mangled name
225-
if (!mangled) {
226-
do {
227-
mangled = base54(++cname);
228-
} while (!can_mangle(mangled));
229-
}
230-
212+
if (!mangled) do {
213+
mangled = base54(++cname);
214+
} while (!can_mangle(mangled));
231215
cache.set(name, mangled);
232216
}
233217
return mangled;
@@ -238,11 +222,9 @@ function mangle_properties(ast, options) {
238222
if (node instanceof AST_Sequence) {
239223
var last = node.expressions.length - 1;
240224
node.expressions[last] = mangleStrings(node.expressions[last]);
241-
}
242-
else if (node instanceof AST_String) {
225+
} else if (node instanceof AST_String) {
243226
node.value = mangle(node.value);
244-
}
245-
else if (node instanceof AST_Conditional) {
227+
} else if (node instanceof AST_Conditional) {
246228
node.consequent = mangleStrings(node.consequent);
247229
node.alternative = mangleStrings(node.alternative);
248230
}

lib/scope.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
136136
// AST_Defun node before getting to its AST_Symbol.
137137
(node.scope = defun.parent_scope.resolve()).def_function(node, defun);
138138
} else if (node instanceof AST_SymbolLambda) {
139-
defun.def_function(node, node.name == "arguments" ? undefined : defun);
139+
var def = defun.def_function(node, node.name == "arguments" ? undefined : defun);
140+
if (options.ie8) def.defun = defun.parent_scope.resolve();
140141
} else if (node instanceof AST_SymbolVar) {
141142
defun.def_variable(node, node.TYPE == "SymbolVar" ? null : undefined);
142143
if (defun !== scope) {

test/compress/ie8.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ issue_2976_2: {
458458
}
459459
expect: {
460460
console.log(function f() {
461-
var o;
462-
return o === f ? "FAIL" : "PASS";
461+
var n;
462+
return n === f ? "FAIL" : "PASS";
463463
}());
464464
}
465465
expect_stdout: "PASS"
@@ -477,9 +477,9 @@ issue_2976_3: {
477477
}());
478478
}
479479
expect: {
480-
console.log(function r() {
481-
var o;
482-
return o === r ? "FAIL" : "PASS";
480+
console.log(function o() {
481+
var n;
482+
return n === o ? "FAIL" : "PASS";
483483
}());
484484
}
485485
expect_stdout: "PASS"
@@ -772,17 +772,17 @@ issue_3215_2: {
772772
}
773773
expect: {
774774
console.log(function foo() {
775-
var r = function r(o) {
775+
var o = function o(n) {
776776
return "PASS";
777777
};
778778
try {
779779
"moo";
780-
} catch (o) {
781-
r = function r(o) {
780+
} catch (n) {
781+
o = function o(n) {
782782
return "FAIL";
783783
};
784784
}
785-
return r;
785+
return o;
786786
}()());
787787
}
788788
expect_stdout: "PASS"
@@ -846,17 +846,17 @@ issue_3215_4: {
846846
}
847847
expect: {
848848
console.log(function foo() {
849-
var r = function r(o) {
849+
var o = function o(n) {
850850
return "FAIL";
851851
};
852852
try {
853853
moo;
854-
} catch (o) {
855-
r = function r(o) {
854+
} catch (n) {
855+
o = function o(n) {
856856
return "PASS";
857857
};
858858
}
859-
return r;
859+
return o;
860860
}()());
861861
}
862862
expect_stdout: "PASS"

test/compress/rename.js

+77
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,80 @@ function_catch_catch_ie8: {
534534
"undefined",
535535
]
536536
}
537+
538+
function_do_catch_ie8: {
539+
rename = true
540+
options = {
541+
ie8: true,
542+
side_effects: true,
543+
unused: true,
544+
}
545+
mangle = {
546+
ie8: true,
547+
toplevel: true,
548+
}
549+
input: {
550+
var a = 1, b = 1, c = 0;
551+
function d(e) {
552+
var f, g, h, i;
553+
do {
554+
try {
555+
try {
556+
var j = function q(){}();
557+
} catch (r) {
558+
--a && w("ddddddddeeeeeeegggggggggiiiiilllllllnnnnntuuuuuuuuyyyyyyy");
559+
var k, l, m, n, o;
560+
--m;
561+
--n;
562+
--o;
563+
}
564+
try {
565+
i[1];
566+
} catch (s) {
567+
var p;
568+
switch (function t() {
569+
c++;
570+
}()) {
571+
case j + --p:
572+
}
573+
}
574+
} catch (u) {}
575+
} while (--i);
576+
b--;
577+
}
578+
d();
579+
console.log(b, c);
580+
}
581+
expect: {
582+
var t = 1, u = 1, y = 0;
583+
function c(c) {
584+
var d;
585+
do {
586+
try {
587+
try {
588+
var e = void 0;
589+
} catch (i) {
590+
--t && w("ddddddddeeeeeeegggggggggiiiiilllllllnnnnntuuuuuuuuyyyyyyy");
591+
0;
592+
0;
593+
0;
594+
}
595+
try {
596+
d[1];
597+
} catch (l) {
598+
var g;
599+
switch(function x() {
600+
y++;
601+
}()) {
602+
case e + --g:
603+
}
604+
}
605+
} catch (n) {}
606+
} while (--d);
607+
u--;
608+
}
609+
c();
610+
console.log(u, y);
611+
}
612+
expect_stdout: "0 1"
613+
}

0 commit comments

Comments
 (0)