UNPKG

1.17 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};
14
15ForStatement.prototype.normalize = function (place) {
16 if (this.body === null) {
17 this.body = new BlockStatement([]);
18 } else if (this.body.type !== 'BlockStatement') {
19 this.body = new BlockStatement([this.body]);
20 }
21
22 if (this.test === null) {
23 this.test = new Literal(true);
24 }
25
26 if (this.update !== null) {
27 this.body.body.push(new ExpressionStatement(this.update));
28 }
29
30 if (this.init !== null) {
31 if (this.init.type === 'AssignmentExpression') {
32 this.init = new ExpressionStatement(this.init);
33 }
34 place.push(this.init);
35 }
36
37 var whileStatement = new WhileStatement(this.test, this.body);
38 whileStatement.normalize(place);
39 this.transformedStatement = whileStatement;
40}