UNPKG

decaffeinate-parser

Version:

A better AST for CoffeeScript, inspired by CoffeeScriptRedux.

104 lines (103 loc) 4.94 kB
import { ExportAllDeclaration as CoffeeExportAllDeclaration, ExportDefaultDeclaration as CoffeeExportDefaultDeclaration, ExportNamedDeclaration as CoffeeExportNamedDeclaration, ExportSpecifierList, IdentifierLiteral, ImportDeclaration as CoffeeImportDeclaration, ImportNamespaceSpecifier, ImportSpecifierList, Literal } from 'decaffeinate-coffeescript2/lib/coffeescript/nodes'; import * as nodes from '../nodes'; import getLocation from '../util/getLocation'; import UnsupportedNodeError from '../util/UnsupportedNodeError'; import mapAny from './mapAny'; import mapLiteral from './mapLiteral'; import notNull from '../util/notNull'; export default function mapModuleDeclaration(context, node) { var _a = getLocation(context, node), line = _a.line, column = _a.column, start = _a.start, end = _a.end, raw = _a.raw; if (node instanceof CoffeeImportDeclaration) { 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 ImportNamespaceSpecifier) { namespaceImport = mapStarSpecifier(context, clause.namedImports); } else if (clause.namedImports instanceof 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 CoffeeExportNamedDeclaration) { if (node.clause instanceof 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(context, notNull(node.clause)); return new nodes.ExportNamedDeclaration(line, column, start, end, raw, expression); } } else if (node instanceof CoffeeExportDefaultDeclaration) { var expression = mapAny(context, notNull(node.clause)); return new nodes.ExportDefaultDeclaration(line, column, start, end, raw, expression); } else if (node instanceof CoffeeExportAllDeclaration) { 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(node); } } function mapSource(context, coffeeSource) { var source = mapLiteral(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(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 IdentifierLiteral) { return mapLiteral(context, literal); } else if (literal.constructor === Literal) { var _a = getLocation(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.'); } }