Browse Source

Merge pull request #920 from rollup/gh-919

allow empty for loop heads
gh-953
Rich Harris 8 years ago
committed by GitHub
parent
commit
9f490cc773
  1. 2
      package.json
  2. 8
      src/ast/Node.js
  3. 4
      src/ast/nodes/ArrowFunctionExpression.js
  4. 15
      src/ast/nodes/ForInStatement.js
  5. 15
      src/ast/nodes/ForOfStatement.js
  6. 36
      src/ast/nodes/ForStatement.js
  7. 3
      test/form/body-less-for-loops/_config.js
  8. 16
      test/form/body-less-for-loops/_expected/amd.js
  9. 14
      test/form/body-less-for-loops/_expected/cjs.js
  10. 12
      test/form/body-less-for-loops/_expected/es.js
  11. 17
      test/form/body-less-for-loops/_expected/iife.js
  12. 20
      test/form/body-less-for-loops/_expected/umd.js
  13. 12
      test/form/body-less-for-loops/main.js
  14. 3
      test/form/for-loop-with-empty-head/_config.js
  15. 7
      test/form/for-loop-with-empty-head/_expected/amd.js
  16. 5
      test/form/for-loop-with-empty-head/_expected/cjs.js
  17. 3
      test/form/for-loop-with-empty-head/_expected/es.js
  18. 8
      test/form/for-loop-with-empty-head/_expected/iife.js
  19. 11
      test/form/for-loop-with-empty-head/_expected/umd.js
  20. 3
      test/form/for-loop-with-empty-head/main.js

2
package.json

@ -61,7 +61,7 @@
"mocha": "^3.0.0",
"remap-istanbul": "^0.6.4",
"require-relative": "^0.8.7",
"rollup": "^0.35.3",
"rollup": "^0.34.0",
"rollup-plugin-buble": "^0.12.1",
"rollup-plugin-commonjs": "^3.0.0",
"rollup-plugin-json": "^2.0.0",

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 );
}
}

15
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 ) {
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
super.initialise( this.body.scope );
assignTo( this.left, this.body.scope, STRING );
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 );
}
}

15
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 ) {
if ( this.body.type === 'BlockStatement' ) {
this.body.createScope( scope );
super.initialise( this.body.scope );
assignTo( this.left, this.body.scope, UNKNOWN );
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 );
}
}

36
src/ast/nodes/ForStatement.js

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

3
test/form/for-loop-with-empty-head/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'handles for loop with empty head'
};

7
test/form/for-loop-with-empty-head/_expected/amd.js

@ -0,0 +1,7 @@
define(function () { 'use strict';
for ( ; ; ) {
console.log( 42 );
}
});

5
test/form/for-loop-with-empty-head/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
for ( ; ; ) {
console.log( 42 );
}

3
test/form/for-loop-with-empty-head/_expected/es.js

@ -0,0 +1,3 @@
for ( ; ; ) {
console.log( 42 );
}

8
test/form/for-loop-with-empty-head/_expected/iife.js

@ -0,0 +1,8 @@
(function () {
'use strict';
for ( ; ; ) {
console.log( 42 );
}
}());

11
test/form/for-loop-with-empty-head/_expected/umd.js

@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
for ( ; ; ) {
console.log( 42 );
}
})));

3
test/form/for-loop-with-empty-head/main.js

@ -0,0 +1,3 @@
for ( ; ; ) {
console.log( 42 );
}
Loading…
Cancel
Save