decaffeinate-parser
Version:
A better AST for CoffeeScript, inspired by CoffeeScriptRedux.
47 lines (46 loc) • 2.61 kB
JavaScript
;
exports.__esModule = true;
var nodes_1 = require("decaffeinate-coffeescript2/lib/coffeescript/nodes");
var nodes_2 = require("../nodes");
var getLocation_1 = require("../util/getLocation");
var isCommentOnlyNode_1 = require("../util/isCommentOnlyNode");
var UnsupportedNodeError_1 = require("../util/UnsupportedNodeError");
var mapAny_1 = require("./mapAny");
var mapValue_1 = require("./mapValue");
function mapObj(context, node) {
var _a = getLocation_1["default"](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_1["default"](property)) {
continue;
}
var _c = getLocation_1["default"](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 nodes_1.Value) {
// shorthand property
var value = mapValue_1["default"](context, property);
var isComputed = property.base instanceof nodes_1.ComputedPropertyName;
members.push(new nodes_2.ObjectInitialiserMember(line_1, column_1, start_1, end_1, raw_1, value, null, isComputed));
}
else if (property instanceof nodes_1.Assign && property.context === 'object') {
var key = mapAny_1["default"](context, property.variable);
var expression = mapAny_1["default"](context, property.value);
var isComputed = property.variable instanceof nodes_1.Value &&
property.variable.base instanceof nodes_1.ComputedPropertyName;
members.push(new nodes_2.ObjectInitialiserMember(line_1, column_1, start_1, end_1, raw_1, key, expression, isComputed));
}
else if (property instanceof nodes_1.Assign) {
var assignee = mapAny_1["default"](context, property.variable);
var expression = mapAny_1["default"](context, property.value);
members.push(new nodes_2.AssignOp(line_1, column_1, start_1, end_1, raw_1, assignee, expression));
}
else if (property instanceof nodes_1.Splat) {
members.push(new nodes_2.Spread(line_1, column_1, start_1, end_1, raw_1, mapAny_1["default"](context, property.name)));
}
else {
throw new UnsupportedNodeError_1["default"](property, 'Unexpected object member.');
}
}
return new nodes_2.ObjectInitialiser(line, column, start, end, raw, members);
}
exports["default"] = mapObj;