UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

107 lines (106 loc) 4.91 kB
"use strict"; exports.__esModule = true; var nodes_1 = require("decaffeinate-coffeescript2/lib/coffeescript/nodes"); var nodes = require("../nodes"); var getLocation_1 = require("../util/getLocation"); var UnsupportedNodeError_1 = require("../util/UnsupportedNodeError"); var mapAny_1 = require("./mapAny"); var mapLiteral_1 = require("./mapLiteral"); var notNull_1 = require("../util/notNull"); function mapModuleDeclaration(context, node) { var _a = getLocation_1["default"](context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; if (node instanceof nodes_1.ImportDeclaration) { var clause = node.clause; var defaultBinding = null; var namespaceImport = null; var namedImports = null; if (clause) { defaultBinding = clause.defaultBinding && mapIdentifierSpecifier(context, clause.defaultBinding); if (clause.namedImports instanceof nodes_1.ImportNamespaceSpecifier) { namespaceImport = mapStarSpecifier(context, clause.namedImports); } else if (clause.namedImports instanceof nodes_1.ImportSpecifierList) { namedImports = clause.namedImports.specifiers.map(function (specifier) { return mapSpecifier(context, specifier); }); } } if (!node.source) { throw new Error('Expected non-null source for import.'); } var source = mapSource(context, node.source); return new nodes.ImportDeclaration(line, column, start, end, raw, defaultBinding, namespaceImport, namedImports, source); } else if (node instanceof nodes_1.ExportNamedDeclaration) { if (node.clause instanceof nodes_1.ExportSpecifierList) { var namedExports = node.clause.specifiers.map(function (specifier) { return mapSpecifier(context, specifier); }); var source = node.source ? mapSource(context, node.source) : null; return new nodes.ExportBindingsDeclaration(line, column, start, end, raw, namedExports, source); } else { var expression = mapAny_1["default"](context, notNull_1["default"](node.clause)); return new nodes.ExportNamedDeclaration(line, column, start, end, raw, expression); } } else if (node instanceof nodes_1.ExportDefaultDeclaration) { var expression = mapAny_1["default"](context, notNull_1["default"](node.clause)); return new nodes.ExportDefaultDeclaration(line, column, start, end, raw, expression); } else if (node instanceof nodes_1.ExportAllDeclaration) { if (!node.source) { throw new Error('Expected non-null source for star export.'); } var source = mapSource(context, node.source); return new nodes.ExportAllDeclaration(line, column, start, end, raw, source); } else { throw new UnsupportedNodeError_1["default"](node); } } exports["default"] = mapModuleDeclaration; function mapSource(context, coffeeSource) { var source = mapLiteral_1["default"](context, coffeeSource); if (!(source instanceof nodes.String)) { throw new Error('Expected string literal as import source.'); } return source; } function mapIdentifierSpecifier(context, specifier) { if (specifier.alias) { throw new Error('Expected no alias for identifier specifier.'); } return mapLiteralToIdentifier(context, specifier.original); } function mapStarSpecifier(context, specifier) { if (specifier.original.value !== '*') { throw new Error('Expected a star on the LHS of a star specifier.'); } if (!specifier.alias) { throw new Error('Expected node on the RHS of star specifier.'); } return mapLiteralToIdentifier(context, specifier.alias); } function mapSpecifier(context, specifier) { var _a = getLocation_1["default"](context, specifier), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; var original = mapLiteralToIdentifier(context, specifier.original); var alias = specifier.alias ? mapLiteralToIdentifier(context, specifier.alias) : null; return new nodes.ModuleSpecifier(line, column, start, end, raw, original, alias); } function mapLiteralToIdentifier(context, literal) { if (literal instanceof nodes_1.IdentifierLiteral) { return mapLiteral_1["default"](context, literal); } else if (literal.constructor === nodes_1.Literal) { var _a = getLocation_1["default"](context, literal), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; return new nodes.Identifier(line, column, start, end, raw, literal.value); } else { throw new Error('Expected identifier in module declaration.'); } }