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

Function parseFunction

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

Source from the content-addressed store, hash-verified

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

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