MCPcopy Create free account
hub / github.com/babel/babel / parseFunction

Function parseFunction

packages/babel-parser/src/parser/statement.ts:1443–1495  ·  view source on GitHub ↗
(
    this: Parser,
    node: Undone<T>,
    flags: ParseFunctionFlag = ParseFunctionFlag.Expression,
  )

Source from the content-addressed store, hash-verified

1441 // ParseFunctionFlag.Declaration flag).
1442
1443 parseFunction<T extends N.NormalFunction>(
1444 this: Parser,
1445 node: Undone<T>,
1446 flags: ParseFunctionFlag = ParseFunctionFlag.Expression,
1447 ): T {
1448 const hangingDeclaration = flags & ParseFunctionFlag.HangingDeclaration;
1449 const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
1450 const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
1451 const isAsync = !!(flags & ParseFunctionFlag.Async);
1452
1453 this.initFunction(node, isAsync);
1454
1455 if (this.match(tt.star)) {
1456 if (hangingDeclaration) {
1457 this.raise(
1458 Errors.GeneratorInSingleStatementContext,
1459 this.state.startLoc,
1460 );
1461 }
1462 this.next(); // eat *
1463 node.generator = true;
1464 }
1465
1466 if (isDeclaration) {
1467 node.id = this.parseFunctionId(requireId);
1468 }
1469
1470 this.scope.enter(ScopeFlag.FUNCTION);
1471 this.prodParam.enter(functionFlags(isAsync, node.generator));
1472
1473 if (!isDeclaration) {
1474 node.id = this.parseFunctionId();
1475 }
1476
1477 this.parseFunctionParams(node, /* isConstructor */ false);
1478
1479 this.parseFunctionBodyAndFinish(
1480 node,
1481 isDeclaration ? "FunctionDeclaration" : "FunctionExpression",
1482 );
1483
1484 this.prodParam.exit();
1485 this.scope.exit();
1486
1487 if (isDeclaration && !hangingDeclaration) {
1488 // We need to register this _after_ parsing the function body
1489 // because of TypeScript body-less function declarations,
1490 // which shouldn't be added to the scope.
1491 this.registerFunctionStatementId(node as T);
1492 }
1493
1494 return node as T;
1495 }
1496
1497 parseFunctionId(requireId?: boolean): N.Identifier | undefined | null {
1498 return requireId || tokenIsIdentifier(this.state.type)

Callers

nothing calls this directly

Calls 10

functionFlagsFunction · 0.90
initFunctionMethod · 0.80
matchMethod · 0.80
nextMethod · 0.80
parseFunctionIdMethod · 0.80
enterMethod · 0.45
parseFunctionParamsMethod · 0.45
exitMethod · 0.45

Tested by

no test coverage detected