MCPcopy Create free account
hub / github.com/compilets/compilets / parseImportDeclaration

Method parseImportDeclaration

src/parser.ts:378–407  ·  view source on GitHub ↗
(node: ts.ImportDeclaration)

Source from the content-addressed store, hash-verified

376 }
377
378 parseImportDeclaration(node: ts.ImportDeclaration): syntax.ImportDeclaration {
379 const {importClause, moduleSpecifier} = node;
380 if (!ts.isStringLiteral(moduleSpecifier))
381 throw new UnsupportedError(node, 'Module name must be string literal');
382 const fileName = getFileNameFromModuleSpecifier(moduleSpecifier.text);
383 const decl = new syntax.ImportDeclaration(fileName, getNamespaceFromFileName(fileName));
384 // import 'module'
385 if (!importClause)
386 return decl;
387 const {name, namedBindings} = importClause;
388 if (!name && !namedBindings)
389 throw new UnimplementedError(node, 'Import module without names is not supported');
390 // import xxx from 'module'
391 if (name)
392 throw new UnimplementedError(node, 'Default import has not been implemented');
393 if (namedBindings) {
394 if (ts.isNamedImports(namedBindings)) {
395 // import {A, B, C} from 'module'
396 const {elements} = namedBindings;
397 decl.names = elements.filter(e => !e.propertyName)
398 .map(e => e.name.text);
399 decl.aliases = elements.filter(e => e.propertyName)
400 .map(e => [ e.propertyName!.text, e.name.text ]);
401 } else {
402 // import * as xxx from 'module'
403 decl.namespaceAlias = namedBindings.name.text;
404 }
405 }
406 return decl;
407 }
408
409 parseVariableDeclarationList(node: ts.VariableDeclarationList): syntax.VariableDeclarationList {
410 const decls = node.declarations.map(this.parseVariableDeclaration.bind(this));

Callers 1

parseSourceFileMethod · 0.95

Calls 3

getNamespaceFromFileNameFunction · 0.90
filterMethod · 0.80

Tested by

no test coverage detected