decaffeinate-parser
Version:
A better AST for CoffeeScript, inspired by CoffeeScriptRedux.
32 lines (31 loc) • 1.41 kB
JavaScript
import { ForFrom, ForIn, ForOf } from '../nodes';
import getLocation from '../util/getLocation';
import mapAny from './mapAny';
import mapPossiblyEmptyBlock from './mapPossiblyEmptyBlock';
export default function mapFor(context, node) {
var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw;
var keyAssignee = node.index ? mapAny(context, node.index) : null;
var valAssignee = node.name ? mapAny(context, node.name) : null;
var body = mapPossiblyEmptyBlock(context, node.body);
var target = mapAny(context, node.source);
var filter = node.guard ? mapAny(context, node.guard) : null;
if (body && body.start < target.start) {
body = body.withInline(true);
}
if (node.object) {
var isOwn = node.own;
return new ForOf(line, column, start, end, raw, keyAssignee, valAssignee, target, filter, body, isOwn);
}
else {
if (node.from) {
if (keyAssignee) {
throw new Error('Unexpected key assignee in for...from.');
}
return new ForFrom(line, column, start, end, raw, valAssignee, target, filter, body);
}
else {
var step = node.step ? mapAny(context, node.step) : null;
return new ForIn(line, column, start, end, raw, keyAssignee, valAssignee, target, filter, body, step);
}
}
}