decaffeinate-parser
Version:
A better AST for CoffeeScript, inspired by CoffeeScriptRedux.
44 lines (43 loc) • 2.4 kB
JavaScript
import { Assign, ComputedPropertyName, Splat, Value } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes';
import { AssignOp, ObjectInitialiser, ObjectInitialiserMember, Spread } from '../nodes';
import getLocation from '../util/getLocation';
import isCommentOnlyNode from '../util/isCommentOnlyNode';
import UnsupportedNodeError from '../util/UnsupportedNodeError';
import mapAny from './mapAny';
import mapValue from './mapValue';
export default function mapObj(context, node) {
var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw;
var members = [];
for (var _i = 0, _b = node.properties; _i < _b.length; _i++) {
var property = _b[_i];
if (isCommentOnlyNode(property)) {
continue;
}
var _c = getLocation(context, property), line_1 = _c.line, column_1 = _c.column, start_1 = _c.start, end_1 = _c.end, raw_1 = _c.raw;
if (property instanceof Value) {
// shorthand property
var value = mapValue(context, property);
var isComputed = property.base instanceof ComputedPropertyName;
members.push(new ObjectInitialiserMember(line_1, column_1, start_1, end_1, raw_1, value, null, isComputed));
}
else if (property instanceof Assign && property.context === 'object') {
var key = mapAny(context, property.variable);
var expression = mapAny(context, property.value);
var isComputed = property.variable instanceof Value &&
property.variable.base instanceof ComputedPropertyName;
members.push(new ObjectInitialiserMember(line_1, column_1, start_1, end_1, raw_1, key, expression, isComputed));
}
else if (property instanceof Assign) {
var assignee = mapAny(context, property.variable);
var expression = mapAny(context, property.value);
members.push(new AssignOp(line_1, column_1, start_1, end_1, raw_1, assignee, expression));
}
else if (property instanceof Splat) {
members.push(new Spread(line_1, column_1, start_1, end_1, raw_1, mapAny(context, property.name)));
}
else {
throw new UnsupportedNodeError(property, 'Unexpected object member.');
}
}
return new ObjectInitialiser(line, column, start, end, raw, members);
}