Skip to content

Commit

Permalink
include blocks containing activated var declarations (fixes #1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 21, 2016
1 parent b3b1098 commit 119734a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ast/nodes/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export default class VariableDeclarator extends Node {
this.activated = true;

this.run( this.findScope() );

// if declaration is inside a block, ensure that the block
// is marked for inclusion
if ( this.parent.kind === 'var' ) {
let node = this.parent.parent;
while ( /Statement/.test( node.type ) ) {
node.shouldInclude = true;
node = node.parent;
}
}
}

hasEffects ( scope ) {
Expand Down
3 changes: 3 additions & 0 deletions test/function/vars-not-removed-in-if-block/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'preserves var declarations in if blocks (#1113)'
};
7 changes: 7 additions & 0 deletions test/function/vars-not-removed-in-if-block/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if ( Math.random() <= 1 ) {
var x = 1;
} else {
var x = 2;
}

assert.equal( x, 1 );

0 comments on commit 119734a

Please sign in to comment.