diff --git a/src/ast/nodes/VariableDeclarator.js b/src/ast/nodes/VariableDeclarator.js index dc35bff..753d384 100644 --- a/src/ast/nodes/VariableDeclarator.js +++ b/src/ast/nodes/VariableDeclarator.js @@ -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 ) { diff --git a/test/function/vars-not-removed-in-if-block/_config.js b/test/function/vars-not-removed-in-if-block/_config.js new file mode 100644 index 0000000..c34bcea --- /dev/null +++ b/test/function/vars-not-removed-in-if-block/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'preserves var declarations in if blocks (#1113)' +}; diff --git a/test/function/vars-not-removed-in-if-block/main.js b/test/function/vars-not-removed-in-if-block/main.js new file mode 100644 index 0000000..f4367dd --- /dev/null +++ b/test/function/vars-not-removed-in-if-block/main.js @@ -0,0 +1,7 @@ +if ( Math.random() <= 1 ) { + var x = 1; +} else { + var x = 2; +} + +assert.equal( x, 1 );