-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathswitch.js
35 lines (32 loc) · 922 Bytes
/
switch.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
/*jslint node: true, indent: 2 */
'use strict';
var doBody = require('./helper/body');
module.exports = function (node, indent) {
var codegen, str, that = this, cases;
codegen = this.process.bind(this);
str = 'switch' + this.ws + '(' + codegen(node.test, indent) + ')';
if (node.shortForm) {
str += ':' + this.nl;
} else {
str += this.ws + '{' + this.nl;
}
cases = node.body.children.map(function (item) {
var head;
if (item.test) {
head = indent + that.indent + 'case ' + codegen(item.test, indent) + ':' + that.nl;
} else {
head = indent + that.indent + 'default:' + that.nl;
}
if (item.body) {
head += doBody.call(that, codegen, indent + that.indent, item.body.children || [item.body]);
}
return head;
});
str += cases.join('');
if (node.shortForm) {
str += indent + 'endswitch;';
} else {
str += indent + '}';
}
return str;
};