From c2e471e3ad37a5c02f91179781c85d0e5177dfff Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 29 May 2017 18:08:08 +0800 Subject: [PATCH] fix `if_return` on block-scoped variables (#2021) fixes #1317 --- lib/compress.js | 4 ++- test/compress/if_return.js | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 2582550bf79..de6a476a38d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1068,7 +1068,9 @@ merge(Compressor.prototype, { } function can_merge_flow(ab) { - if (!ab) return false; + if (!ab || !all(ret, function(stat) { + return !(stat instanceof AST_Const || stat instanceof AST_Let); + })) return false; var lct = ab instanceof AST_LoopControl ? compressor.loopcontrol_target(ab) : null; return ab instanceof AST_Return && in_lambda && is_return_void(ab.value) || ab instanceof AST_Continue && self === loop_body(lct) diff --git a/test/compress/if_return.js b/test/compress/if_return.js index 72b69e709f9..bc958e68721 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -326,3 +326,61 @@ issue_512: { } } } + +issue_1317: { + options = { + if_return: true, + } + input: { + !function(a) { + if (a) return; + let b = 1; + function g() { + return b; + } + console.log(g()); + }(); + } + expect: { + !function(a) { + if (a) return; + let b = 1; + function g() { + return b; + } + console.log(g()); + }(); + } + expect_stdout: "1" + node_version: ">=6" +} + +issue_1317_strict: { + options = { + if_return: true, + } + input: { + "use strict"; + !function(a) { + if (a) return; + let b = 1; + function g() { + return b; + } + console.log(g()); + }(); + } + expect: { + "use strict"; + !function(a) { + if (a) return; + let b = 1; + function g() { + return b; + } + console.log(g()); + }(); + } + expect_stdout: "1" + node_version: ">=4" +}