close #2946 by ensuring let and const aren't put out of blocks

This commit is contained in:
Fábio Santos 2018-03-15 18:31:13 +00:00
parent 569757d14d
commit 55acf310f8
2 changed files with 35 additions and 1 deletions

View File

@ -3115,11 +3115,18 @@ merge(Compressor.prototype, {
return self; 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){ OPT(AST_BlockStatement, function(self, compressor){
tighten_body(self.body, compressor); tighten_body(self.body, compressor);
switch (self.body.length) { switch (self.body.length) {
case 1: 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])) { || can_be_evicted_from_block(self.body[0])) {
return self.body[0]; return self.body[0];
} }

View File

@ -187,3 +187,30 @@ issue_1672_if_strict: {
expect_stdout: true expect_stdout: true
node_version: ">=6" 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;
}
}
}