MCPcopy
hub / github.com/jquery/esprima / parseImportDeclaration

Method parseImportDeclaration

src/parser.ts:3424–3473  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3422 }
3423
3424 parseImportDeclaration(): Node.ImportDeclaration {
3425 if (this.context.inFunctionBody) {
3426 this.throwError(Messages.IllegalImportDeclaration);
3427 }
3428
3429 const node = this.createNode();
3430 this.expectKeyword('import');
3431
3432 let src: Node.Literal;
3433 let specifiers: Node.ImportDeclarationSpecifier[] = [];
3434 if (this.lookahead.type === Token.StringLiteral) {
3435 // import 'foo';
3436 src = this.parseModuleSpecifier();
3437 } else {
3438 if (this.match('{')) {
3439 // import {bar}
3440 specifiers = specifiers.concat(this.parseNamedImports());
3441 } else if (this.match('*')) {
3442 // import * as foo
3443 specifiers.push(this.parseImportNamespaceSpecifier());
3444 } else if (this.isIdentifierName(this.lookahead) && !this.matchKeyword('default')) {
3445 // import foo
3446 specifiers.push(this.parseImportDefaultSpecifier());
3447 if (this.match(',')) {
3448 this.nextToken();
3449 if (this.match('*')) {
3450 // import foo, * as foo
3451 specifiers.push(this.parseImportNamespaceSpecifier());
3452 } else if (this.match('{')) {
3453 // import foo, {bar}
3454 specifiers = specifiers.concat(this.parseNamedImports());
3455 } else {
3456 this.throwUnexpectedToken(this.lookahead);
3457 }
3458 }
3459 } else {
3460 this.throwUnexpectedToken(this.nextToken());
3461 }
3462
3463 if (!this.matchContextualKeyword('from')) {
3464 const message = this.lookahead.value ? Messages.UnexpectedToken : Messages.MissingFromClause;
3465 this.throwError(message, this.lookahead.value);
3466 }
3467 this.nextToken();
3468 src = this.parseModuleSpecifier();
3469 }
3470 this.consumeSemicolon();
3471
3472 return this.finalize(node, new Node.ImportDeclaration(specifiers, src));
3473 }
3474
3475 // https://tc39.github.io/ecma262/#sec-exports
3476

Callers 1

Calls 15

throwErrorMethod · 0.95
createNodeMethod · 0.95
expectKeywordMethod · 0.95
parseModuleSpecifierMethod · 0.95
matchMethod · 0.95
parseNamedImportsMethod · 0.95
isIdentifierNameMethod · 0.95
matchKeywordMethod · 0.95
nextTokenMethod · 0.95
throwUnexpectedTokenMethod · 0.95

Tested by

no test coverage detected