Skip to content

Commit b70591b

Browse files
authoredMar 5, 2017
handle variable declaration within catch blocks (#1546)
accounts for IE8- scoping
1 parent b33e7f8 commit b70591b

File tree

5 files changed

+69
-6
lines changed

5 files changed

+69
-6
lines changed
 

‎lib/ast.js

-3
Original file line numberDiff line numberDiff line change
@@ -812,9 +812,6 @@ var AST_SymbolAccessor = DEFNODE("SymbolAccessor", null, {
812812

813813
var AST_SymbolDeclaration = DEFNODE("SymbolDeclaration", "init", {
814814
$documentation: "A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",
815-
$propdoc: {
816-
init: "[AST_Node*/S] array of initializers for this declaration."
817-
}
818815
}, AST_Symbol);
819816

820817
var AST_SymbolVar = DEFNODE("SymbolVar", null, {

‎lib/compress.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ merge(Compressor.prototype, {
223223

224224
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){
225225
var reduce_vars = rescan && compressor.option("reduce_vars");
226+
var ie8 = !compressor.option("screw_ie8");
226227
var safe_ids = [];
227228
push();
228229
var suppressor = new TreeWalker(function(node) {
@@ -232,7 +233,7 @@ merge(Compressor.prototype, {
232233
d.fixed = false;
233234
}
234235
});
235-
var tw = new TreeWalker(function(node){
236+
var tw = new TreeWalker(function(node, descend){
236237
if (!(node instanceof AST_Directive || node instanceof AST_Constant)) {
237238
node._squeezed = false;
238239
node._optimized = false;
@@ -247,6 +248,9 @@ merge(Compressor.prototype, {
247248
d.fixed = false;
248249
}
249250
}
251+
if (ie8 && node instanceof AST_SymbolCatch) {
252+
node.definition().fixed = false;
253+
}
250254
if (node instanceof AST_VarDef) {
251255
var d = node.name.definition();
252256
if (d.fixed === undefined) {
@@ -301,6 +305,12 @@ merge(Compressor.prototype, {
301305
pop();
302306
return true;
303307
}
308+
if (node instanceof AST_Catch) {
309+
push();
310+
descend();
311+
pop();
312+
return true;
313+
}
304314
}
305315
});
306316
this.walk(tw);

‎lib/scope.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
154154
}
155155
else if (node instanceof AST_SymbolVar
156156
|| node instanceof AST_SymbolConst) {
157-
var def = defun.def_variable(node);
158-
def.init = tw.parent().value;
157+
defun.def_variable(node);
159158
}
160159
else if (node instanceof AST_SymbolCatch) {
161160
scope.def_variable(node);

‎test/compress/reduce_vars.js

+23
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,29 @@ inner_var_for_in_2: {
605605
}
606606
}
607607

608+
inner_var_catch: {
609+
options = {
610+
evaluate: true,
611+
reduce_vars: true,
612+
}
613+
input: {
614+
try {
615+
a();
616+
} catch (e) {
617+
var b = 1;
618+
}
619+
console.log(b);
620+
}
621+
expect: {
622+
try {
623+
a();
624+
} catch (e) {
625+
var b = 1;
626+
}
627+
console.log(b);
628+
}
629+
}
630+
608631
issue_1533_1: {
609632
options = {
610633
collapse_vars: true,

‎test/compress/screw-ie8.js

+34
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,37 @@ dont_screw_try_catch_undefined: {
148148
}
149149
}
150150
}
151+
152+
reduce_vars: {
153+
options = {
154+
evaluate: true,
155+
reduce_vars: true,
156+
screw_ie8: false,
157+
unused: true,
158+
}
159+
mangle = {
160+
screw_ie8: false,
161+
}
162+
input: {
163+
function f() {
164+
var a;
165+
try {
166+
x();
167+
} catch (a) {
168+
y();
169+
}
170+
alert(a);
171+
}
172+
}
173+
expect: {
174+
function f() {
175+
var t;
176+
try {
177+
x();
178+
} catch (t) {
179+
y();
180+
}
181+
alert(t);
182+
}
183+
}
184+
}

0 commit comments

Comments
 (0)
Please sign in to comment.