Browse Source

handle body-less for loops etc

gh-953
Rich-Harris 8 years ago
parent
commit
7afadff268
  1. 8
      src/ast/Node.js
  2. 4
      src/ast/nodes/ArrowFunctionExpression.js
  3. 17
      src/ast/nodes/ForInStatement.js
  4. 17
      src/ast/nodes/ForOfStatement.js
  5. 29
      src/ast/nodes/ForStatement.js
  6. 3
      test/form/body-less-for-loops/_config.js
  7. 16
      test/form/body-less-for-loops/_expected/amd.js
  8. 14
      test/form/body-less-for-loops/_expected/cjs.js
  9. 12
      test/form/body-less-for-loops/_expected/es.js
  10. 17
      test/form/body-less-for-loops/_expected/iife.js
  11. 20
      test/form/body-less-for-loops/_expected/umd.js
  12. 12
      test/form/body-less-for-loops/main.js

8
src/ast/Node.js

@ -3,7 +3,7 @@ import getLocation from '../utils/getLocation.js';
export default class Node {
bind ( scope ) {
this.eachChild( child => child.bind( scope ) );
this.eachChild( child => child.bind( this.scope || scope ) );
}
eachChild ( callback ) {
@ -43,6 +43,8 @@ export default class Node {
}
hasEffects ( scope ) {
if ( this.scope ) scope = this.scope;
for ( const key of this.keys ) {
const value = this[ key ];
@ -61,7 +63,7 @@ export default class Node {
}
initialise ( scope ) {
this.eachChild( child => child.initialise( scope ) );
this.eachChild( child => child.initialise( this.scope || scope ) );
}
locate () {
@ -82,7 +84,7 @@ export default class Node {
this.ran = true;
this.eachChild( child => {
child.run( scope );
child.run( this.scope || scope );
});
}

4
src/ast/nodes/ArrowFunctionExpression.js

@ -18,6 +18,7 @@ export default class ArrowFunctionExpression extends Node {
initialise ( scope ) {
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
this.scope = this.body.scope;
} else {
this.scope = new Scope({
parent: scope,
@ -32,7 +33,6 @@ export default class ArrowFunctionExpression extends Node {
}
}
scope = this.scope || this.body.scope;
super.initialise( scope );
super.initialise( this.scope );
}
}

17
src/ast/nodes/ForInStatement.js

@ -1,11 +1,22 @@
import Statement from './shared/Statement.js';
import assignTo from './shared/assignTo.js';
import Scope from '../scopes/Scope.js';
import { STRING } from '../values.js';
export default class ForInStatement extends Statement {
initialise ( scope ) {
this.body.createScope( scope );
super.initialise( this.body.scope );
assignTo( this.left, this.body.scope, STRING );
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
this.scope = this.body.scope;
} else {
this.scope = new Scope({
parent: scope,
isBlockScope: true,
isLexicalBoundary: false
});
}
super.initialise( this.scope );
assignTo( this.left, this.scope, STRING );
}
}

17
src/ast/nodes/ForOfStatement.js

@ -1,11 +1,22 @@
import Statement from './shared/Statement.js';
import assignTo from './shared/assignTo.js';
import Scope from '../scopes/Scope.js';
import { UNKNOWN } from '../values.js';
export default class ForOfStatement extends Statement {
initialise ( scope ) {
this.body.createScope( scope );
super.initialise( this.body.scope );
assignTo( this.left, this.body.scope, UNKNOWN );
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
this.scope = this.body.scope;
} else {
this.scope = new Scope({
parent: scope,
isBlockScope: true,
isLexicalBoundary: false
});
}
super.initialise( this.scope );
assignTo( this.left, this.scope, UNKNOWN );
}
}

29
src/ast/nodes/ForStatement.js

@ -1,22 +1,23 @@
import Statement from './shared/Statement.js';
import Scope from '../scopes/Scope.js';
export default class ForStatement extends Statement {
hasEffects () {
return super.hasEffects( this.body.scope );
}
initialise ( scope ) {
this.body.createScope( scope );
scope = this.body.scope;
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
this.scope = this.body.scope;
} else {
this.scope = new Scope({
parent: scope,
isBlockScope: true,
isLexicalBoundary: false
});
}
// can't use super, because we need to control the order
if ( this.init ) this.init.initialise( scope );
if ( this.test ) this.test.initialise( scope );
if ( this.update ) this.update.initialise( scope );
this.body.initialise( scope );
}
run ( scope ) {
super.run( scope );
if ( this.init ) this.init.initialise( this.scope );
if ( this.test ) this.test.initialise( this.scope );
if ( this.update ) this.update.initialise( this.scope );
this.body.initialise( this.scope );
}
}

3
test/form/body-less-for-loops/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'supports body-less for loops'
};

16
test/form/body-less-for-loops/_expected/amd.js

@ -0,0 +1,16 @@
define(function () { 'use strict';
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );
});

14
test/form/body-less-for-loops/_expected/cjs.js

@ -0,0 +1,14 @@
'use strict';
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );

12
test/form/body-less-for-loops/_expected/es.js

@ -0,0 +1,12 @@
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );

17
test/form/body-less-for-loops/_expected/iife.js

@ -0,0 +1,17 @@
(function () {
'use strict';
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );
}());

20
test/form/body-less-for-loops/_expected/umd.js

@ -0,0 +1,20 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );
})));

12
test/form/body-less-for-loops/main.js

@ -0,0 +1,12 @@
for ( let i = 0; i < 10; i += 1 ) console.log( i );
for ( const letter of array ) console.log( letter );
for ( const index in array ) console.log( index );
let i;
for ( i = 0; i < 10; i += 1 ) console.log( i );
let letter;
for ( letter of array ) console.log( letter );
let index;
for ( index in array ) console.log( index );
Loading…
Cancel
Save