UNPKG

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