UNPKG

854 BJavaScriptView Raw
1var BlockStatement = require('./BlockStatement');
2var VariableDeclaration = require('./VariableDeclaration');
3var helpers = require('../helpers');
4
5var WhileStatement = module.exports = function(test, body) {
6 this.type = 'WhileStatement';
7 this.test = test;
8 this.body = body;
9};
10
11WhileStatement.prototype.normalize = function (place) {
12 if (this.body === null) {
13 this.body = new BlockStatement([]);
14 } else if (this.body.type !== 'BlockStatement') {
15 this.body = new BlockStatement([this.body]);
16 }
17
18 this.body.normalize();
19
20 //Move variable declarations outside
21 var declarations = [];
22 helpers.extractVariableDeclarations(this.body, declarations);
23 declarations = helpers.reduceDeclarations(declarations);
24 if (declarations.length > 0) {
25 place.push(new VariableDeclaration(declarations, 'var'));
26 }
27 place.push(this);
28};