UNPKG

1.23 kBJavaScriptView Raw
1var Literal = require('./Literal');
2var BlockStatement = require('./BlockStatement');
3var WhileStatement = require('./WhileStatement');
4var ExpressionStatement = require('./ExpressionStatement');
5var VariableDeclaration = require('./VariableDeclaration');
6
7var ForStatement = module.exports = function(init, test, update, body) {
8 this.type = 'ForStatement';
9 this.init = init;
10 this.test = test;
11 this.update = update;
12 this.body = body;
13 this.async = false;
14};
15
16ForStatement.prototype.normalize = function (place) {
17 if (this.body === null) {
18 this.body = new BlockStatement([]);
19 } else if (this.body.type !== 'BlockStatement') {
20 this.body = new BlockStatement([this.body]);
21 }
22
23 if (this.test === null) {
24 this.test = new Literal(true);
25 }
26
27 if (this.update !== null) {
28 this.body.body.push(new ExpressionStatement(this.update));
29 }
30
31 if (this.init !== null) {
32 if (this.init.type !== 'VariableDeclaration') {
33 this.init = new ExpressionStatement(this.init);
34 }
35 place.push(this.init);
36 }
37
38 var whileStatement = new WhileStatement(this.test, this.body);
39 whileStatement.normalize(place);
40 whileStatement.update = this.update;
41 this.transformedStatement = whileStatement;
42}