-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathif.js
60 lines (48 loc) · 1.42 KB
/
if.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*jslint node: true, indent: 2 */
'use strict';
var doBody = require('./helper/body');
module.exports = function processIf(node, indent, inner) {
var codegen, str, that = this;
codegen = this.process.bind(this);
str = 'if' + this.ws + '(' + codegen(node.test, indent) + ')';
if (node.body) {
if (node.shortForm) {
str += ':' + this.nl;
} else {
str += this.ws + '{' + this.nl;
}
str += doBody.call(this, codegen, indent, node.body.children || [node.body]);
if (!node.shortForm) {
str += indent + '}';
}
} else if (!node.alternate) {
return str + ';';
}
if (node.alternate) {
str += (function () {
var out = '';
// is an "elseif"
if (node.alternate.kind === 'if') {
if (node.shortForm) {
return indent + 'else' + processIf.call(that, node.alternate, indent, true);
}
return that.ws + 'else' + processIf.call(that, node.alternate, indent, true);
}
// is an "else"
if (node.shortForm) {
out += indent + 'else:' + that.nl;
} else {
out += that.ws + 'else' + that.ws + '{' + that.nl;
}
out += doBody.call(that, codegen, indent, node.alternate.children || [node.alternate]);
if (!node.shortForm) {
out += indent + '}' + that.nl;
}
return out;
}());
}
if (node.shortForm && !inner) {
str += indent + 'endif;' + this.nl;
}
return str;
};