From ea2359381b737a60b7879d60b756a575271252b7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 25 Feb 2018 15:39:00 +0800 Subject: [PATCH] fix `collapse_vars` on nested exception (#2955) fixes #2954 --- lib/compress.js | 9 +++++---- test/compress/collapse_vars.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 78aee565..2d0a03ae 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -914,7 +914,7 @@ merge(Compressor.prototype, { function tighten_body(statements, compressor) { var scope = compressor.find_parent(AST_Scope); - var in_loop = is_in_loop(); + var in_loop = is_in_node(AST_IterationStatement); var CHANGED, max_iter = 10; do { CHANGED = false; @@ -937,9 +937,10 @@ merge(Compressor.prototype, { } } while (CHANGED && max_iter-- > 0); - function is_in_loop() { + function is_in_node(type) { + if (compressor.self() instanceof type) return true; for (var node, level = 0; node = compressor.parent(level); level++) { - if (node instanceof AST_IterationStatement) return true; + if (node instanceof type) return true; if (node instanceof AST_Scope) break; } return false; @@ -957,7 +958,7 @@ merge(Compressor.prototype, { if (scope.uses_eval || scope.uses_with) return statements; var args; var candidates = []; - var in_try = compressor.self() instanceof AST_Try; + var in_try = is_in_node(AST_Try); var stat_index = statements.length; var scanner = new TreeTransformer(function(node, descend) { if (abort) return node; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 688f54c4..ecf64241 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -4665,3 +4665,38 @@ issue_2931: { } expect_stdout: "undefined" } + +issue_2954: { + options = { + collapse_vars: true, + } + input: { + var a = "PASS", b; + try { + do { + b = function() { + throw 0; + }(); + a = "FAIL"; + b && b.c; + } while (0); + } catch (e) { + } + console.log(a); + } + expect: { + var a = "PASS", b; + try { + do { + b = function() { + throw 0; + }(); + a = "FAIL"; + b && b.c; + } while (0); + } catch (e) { + } + console.log(a); + } + expect_stdout: "PASS" +} -- 2.34.1