Browse Source

include blocks containing activated var declarations (fixes #1113)

gh-1008
Rich-Harris 8 years ago
parent
commit
119734af79
  1. 10
      src/ast/nodes/VariableDeclarator.js
  2. 3
      test/function/vars-not-removed-in-if-block/_config.js
  3. 7
      test/function/vars-not-removed-in-if-block/main.js

10
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 ) {

3
test/function/vars-not-removed-in-if-block/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'preserves var declarations in if blocks (#1113)'
};

7
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 );
Loading…
Cancel
Save