cream-and-sugar
Version:
A deliciously functional syntax for JavaScript with native support for JSX
28 lines (26 loc) • 846 B
JavaScript
import { compile, nodes, compileBody } from '../utils';
/*
* Convert caseof into switch.
*/
compile(nodes.CaseofNode, function () {
let needsDefault = false;
let compiled = this.conditions.map((condition, index) => {
const {test, body} = condition;
const compiledTest = test === 'default' ? test : test.compile(true);
const casePrefix = compiledTest === 'default' ? compiledTest : `case ${compiledTest}`;
if (index === this.conditions.length - 1 && compiledTest !== 'default') {
needsDefault = true;
}
return `${casePrefix}:
${compileBody(body)};
`;
}).join('');
if (needsDefault) {
compiled += `default: throw new Error('No match found for "caseof" statement.');`;
}
return `(function () {
switch (${this.comparator.compile(true)}) {
${compiled}
}
}.bind(this)())`;
});