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

Function parseFunction

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

Source from the content-addressed store, hash-verified

1501 // ParseFunctionFlag.Declaration flag).
1502
1503 parseFunction<T extends N.NormalFunction>(
1504 this: Parser,
1505 node: Undone<T>,
1506 flags: ParseFunctionFlag = ParseFunctionFlag.Expression,
1507 ): T {
1508 const isDeclaration = !!(flags & ParseFunctionFlag.Declaration);
1509 const requireId = isDeclaration && !(flags & ParseFunctionFlag.NullableId);
1510 const isAsync = !!(flags & ParseFunctionFlag.Async);
1511
1512 this.initFunction(node, isAsync);
1513
1514 if (this.match(tt.star)) {
1515 if (flags & ParseFunctionFlag.SingleStatement) {
1516 this.raise(
1517 Errors.GeneratorInSingleStatementContext,
1518 this.state.startLoc,
1519 );
1520 }
1521 this.next(); // eat *
1522 node.generator = true;
1523 }
1524
1525 if (isDeclaration) {
1526 node.id = this.parseFunctionId(requireId);
1527 }
1528
1529 this.scope.enter(ScopeFlag.FUNCTION);
1530 this.prodParam.enter(functionFlags(isAsync, node.generator));
1531
1532 if (!isDeclaration && tokenIsIdentifier(this.state.type)) {
1533 // The name of a function expression is not part of an enclosing async
1534 // arrow head, so `await` must not be recorded as an error there:
1535 // `async (x = function await() {}) => {}` is valid.
1536 this.expressionScope.enter(sharedExpressionScope);
1537 node.id = this.parseFunctionId();
1538 this.expressionScope.exit();
1539 }
1540
1541 this.parseFunctionParams(node, /* isConstructor */ false);
1542
1543 this.parseFunctionBodyAndFinish(
1544 node,
1545 isDeclaration ? "FunctionDeclaration" : "FunctionExpression",
1546 );
1547
1548 this.prodParam.exit();
1549 this.scope.exit();
1550
1551 if (isDeclaration && !(flags & ParseFunctionFlag.HangingDeclaration)) {
1552 // We need to register this _after_ parsing the function body
1553 // because of TypeScript body-less function declarations,
1554 // which shouldn't be added to the scope.
1555 this.registerFunctionStatementId(node as T);
1556 }
1557
1558 return node as T;
1559 }
1560

Callers

nothing calls this directly

Calls 11

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

Tested by

no test coverage detected