UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

74 lines (73 loc) 3.41 kB
"use strict"; exports.__esModule = true; var nodes_1 = require("decaffeinate-coffeescript2/lib/coffeescript/nodes"); var nodes_2 = require("../nodes"); var getLocation_1 = require("../util/getLocation"); var getTemplateLiteralComponents_1 = require("../util/getTemplateLiteralComponents"); var mapAny_1 = require("./mapAny"); function mapCSX(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 _b = node.args, properties = _b[0], children = _b[1]; var mappedProperties = mapCSXProperties(context, properties); var mappedChildren = mapCSXChildren(context, children); return new nodes_2.CSXElement(line, column, start, end, raw, mappedProperties, mappedChildren); } exports["default"] = mapCSX; function mapCSXProperties(context, properties) { if (!(properties instanceof nodes_1.Value) || !(properties.base instanceof nodes_1.Arr)) { throw new Error('Expected a value for the CSX properties arg.'); } var values = properties.base.objects; var resultProperties = []; for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { var value = values_1[_i]; if (!(value instanceof nodes_1.Value)) { throw new Error('Expected value for CSX property.'); } if (value.base instanceof nodes_1.Obj) { for (var _a = 0, _b = value.base.objects; _a < _b.length; _a++) { var propertyAssignment = _b[_a]; if (propertyAssignment instanceof nodes_1.Splat) { resultProperties.push(mapAny_1["default"](context, propertyAssignment)); } else if (propertyAssignment instanceof nodes_1.Assign) { if (!(propertyAssignment.value instanceof nodes_1.Value)) { throw new Error('Unexpected property assignment value.'); } if (!(propertyAssignment.value.base instanceof nodes_1.StringLiteral)) { resultProperties.push(mapAny_1["default"](context, propertyAssignment.value)); } } else { throw new Error('Unexpected property assignment object field in CSX.'); } } } else if (value.base instanceof nodes_1.IdentifierLiteral) { // Do nothing; we don't need to consider this as a node to transform. } else { throw new Error('Unexpected property assignment in CSX.'); } } return resultProperties; } function mapCSXChildren(context, children) { if (!children) { return []; } if (!(children instanceof nodes_1.Value)) { throw new Error('Expected a value for the CSX children arg.'); } if (children.base instanceof nodes_1.StringLiteral) { return []; } else if (!(children.base instanceof nodes_1.StringWithInterpolations)) { throw new Error('Expected a valid CSX children arg.'); } var childInterpolatedString = children.base.body.expressions[0]; var unmappedExpressions = getTemplateLiteralComponents_1["default"](context, childInterpolatedString).unmappedExpressions; return unmappedExpressions.map(function (child) { return child ? mapAny_1["default"](context, child) : null; }); }