From 55acf310f8e070b006ea5a8d6360fdc1be181e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Thu, 15 Mar 2018 18:31:13 +0000 Subject: [PATCH] close #2946 by ensuring let and const aren't put out of blocks --- lib/compress.js | 9 ++++++++- test/compress/blocks.js | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index 886a4b5b..5aa4c517 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3115,11 +3115,18 @@ merge(Compressor.prototype, { return self; }); + function can_be_extracted_from_if_block(node) { + return !( + node instanceof AST_Const || + node instanceof AST_Let + ); + } + OPT(AST_BlockStatement, function(self, compressor){ tighten_body(self.body, compressor); switch (self.body.length) { case 1: - if (!compressor.has_directive("use strict") && compressor.parent() instanceof AST_If + if (!compressor.has_directive("use strict") && compressor.parent() instanceof AST_If && can_be_extracted_from_if_block(self.body[0]) || can_be_evicted_from_block(self.body[0])) { return self.body[0]; } diff --git a/test/compress/blocks.js b/test/compress/blocks.js index 46533e9d..7f506285 100644 --- a/test/compress/blocks.js +++ b/test/compress/blocks.js @@ -187,3 +187,30 @@ issue_1672_if_strict: { expect_stdout: true node_version: ">=6" } + +issue_2946_else_const: { + input: { + if (1) { + const x = 6; + } else { + const y = 12; + } + if (2) { + let z = 24; + } else { + let w = 48; + } + } + expect: { + if (1) { + const x = 6; + } else { + const y = 12; + } + if (2) { + let z = 24; + } else { + let w = 48; + } + } +}