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

Function parseFunctionBody

packages/babel-parser/src/parser/expression.ts:2612–2689  ·  view source on GitHub ↗
(
    this: Parser,
    node: Undone<N.Function>,
    allowExpression?: boolean | null,
    isMethod: boolean = false,
  )

Source from the content-addressed store, hash-verified

2610
2611 // Parse function body and check parameters.
2612 parseFunctionBody(
2613 this: Parser,
2614 node: Undone<N.Function>,
2615 allowExpression?: boolean | null,
2616 isMethod: boolean = false,
2617 ): void {
2618 const isExpression = allowExpression && !this.match(tt.braceL);
2619 this.expressionScope.enter(sharedExpressionScope);
2620
2621 if (isExpression) {
2622 // https://tc39.es/ecma262/#prod-ExpressionBody
2623 (node as Undone<N.ArrowFunctionExpression>).body =
2624 this.parseMaybeAssign();
2625 this.checkParams(node, false, allowExpression, false);
2626 } else {
2627 const oldStrict = this.state.strict;
2628 // Start a new scope with regard to labels
2629 // flag (restore them to their old value afterwards).
2630 const oldLabels = this.state.labels;
2631 this.state.labels = [];
2632
2633 // FunctionBody[Yield, Await]:
2634 // StatementList[?Yield, ?Await, +Return] opt
2635 this.prodParam.enter(
2636 this.prodParam.currentFlags() | ParamKind.PARAM_RETURN,
2637 );
2638 node.body = this.parseBlock(
2639 true,
2640 false,
2641 // Strict mode function checks after we parse the statements in the function body.
2642 (hasStrictModeDirective: boolean) => {
2643 const nonSimple = !this.isSimpleParamList(node.params);
2644
2645 if (hasStrictModeDirective && nonSimple) {
2646 // This logic is here to align the error location with the ESTree plugin.
2647 this.raise(
2648 Errors.IllegalLanguageModeDirective,
2649 // @ts-expect-error kind may not index node
2650 (node.kind === "method" || node.kind === "constructor") &&
2651 // @ts-expect-error key may not index node
2652 !!node.key
2653 ? this.optionFlags & OptionFlags.Locations
2654 ? // @ts-expect-error node.key has been guarded
2655 node.key.loc.end
2656 : // @ts-expect-error node.key has been guarded
2657 node.key
2658 : node,
2659 );
2660 }
2661
2662 const strictModeChanged = !oldStrict && this.state.strict;
2663
2664 // Add the params to varDeclaredNames to ensure that an error is thrown
2665 // if a let/const declaration in the function clashes with one of the params.
2666 this.checkParams(
2667 node,
2668 !this.state.strict && !allowExpression && !isMethod && !nonSimple,
2669 allowExpression,

Callers

nothing calls this directly

Calls 7

matchMethod · 0.80
checkParamsMethod · 0.80
currentFlagsMethod · 0.80
parseBlockMethod · 0.80
enterMethod · 0.45
parseMaybeAssignMethod · 0.45
exitMethod · 0.45

Tested by

no test coverage detected