This commit is contained in:
Fábio Santos 2018-03-15 20:11:35 +00:00 committed by GitHub
commit 3dfa427d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -3115,11 +3115,21 @@ merge(Compressor.prototype, {
return self; return self;
}); });
function can_be_extracted_from_if_block(node) {
return !(
node instanceof AST_Const ||
node instanceof AST_Let ||
node instanceof AST_Class
);
}
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,40 @@ 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;
}
if (3) {
class X {}
} else {
class Y {}
}
}
expect: {
if (1) {
const x = 6;
} else {
const y = 12;
}
if (2) {
let z = 24;
} else {
let w = 48;
}
if (3) {
class X {}
} else {
class Y {}
}
}
}