@@ -180,7 +180,7 @@ class ConstPlugin {
180
180
? statement . alternate
181
181
: statement . consequent ;
182
182
if ( branchToRemove ) {
183
- this . eliminateUnusedStatement ( parser , branchToRemove ) ;
183
+ this . eliminateUnusedStatement ( parser , branchToRemove , true ) ;
184
184
}
185
185
return bool ;
186
186
}
@@ -193,7 +193,7 @@ class ConstPlugin {
193
193
) {
194
194
return ;
195
195
}
196
- this . eliminateUnusedStatement ( parser , statement ) ;
196
+ this . eliminateUnusedStatement ( parser , statement , false ) ;
197
197
return true ;
198
198
} ) ;
199
199
parser . hooks . expressionConditionalOperator . tap (
@@ -509,9 +509,10 @@ class ConstPlugin {
509
509
* Eliminate an unused statement.
510
510
* @param {JavascriptParser } parser the parser
511
511
* @param {Statement } statement the statement to remove
512
+ * @param {boolean } alwaysInBlock whether to always generate curly brackets
512
513
* @returns {void }
513
514
*/
514
- eliminateUnusedStatement ( parser , statement ) {
515
+ eliminateUnusedStatement ( parser , statement , alwaysInBlock ) {
515
516
// Before removing the unused branch, the hoisted declarations
516
517
// must be collected.
517
518
//
@@ -545,8 +546,14 @@ class ConstPlugin {
545
546
const declarations = parser . scope . isStrict
546
547
? getHoistedDeclarations ( statement , false )
547
548
: getHoistedDeclarations ( statement , true ) ;
548
- const replacement =
549
- declarations . length > 0 ? `{ var ${ declarations . join ( ", " ) } ; }` : "{}" ;
549
+
550
+ const inBlock = alwaysInBlock || statement . type === "BlockStatement" ;
551
+
552
+ let replacement = inBlock ? "{" : "" ;
553
+ replacement +=
554
+ declarations . length > 0 ? ` var ${ declarations . join ( ", " ) } ; ` : "" ;
555
+ replacement += inBlock ? "}" : "" ;
556
+
550
557
const dep = new ConstDependency (
551
558
`// removed by dead control flow\n${ replacement } ` ,
552
559
/** @type {Range } */ ( statement . range )
0 commit comments