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

Function parseVar

packages/babel-parser/src/parser/statement.ts:1418–1470  ·  view source on GitHub ↗
(
    this: Parser,
    node: Undone<N.VariableDeclaration>,
    isFor: boolean,
    kind: "var" | "let" | "const" | "using" | "await using",
    allowMissingInitializer: boolean = false,
  )

Source from the content-addressed store, hash-verified

1416 // Parse a list of variable declarations.
1417
1418 parseVar(
1419 this: Parser,
1420 node: Undone<N.VariableDeclaration>,
1421 isFor: boolean,
1422 kind: "var" | "let" | "const" | "using" | "await using",
1423 allowMissingInitializer: boolean = false,
1424 ): Undone<N.VariableDeclaration> {
1425 const declarations: N.VariableDeclarator[] = (node.declarations = []);
1426 node.kind = kind;
1427 for (;;) {
1428 const decl = this.startNode<N.VariableDeclarator>();
1429 this.parseVarId(decl, kind);
1430 decl.init = !this.eat(tt.eq)
1431 ? null
1432 : isFor
1433 ? this.parseMaybeAssignDisallowIn()
1434 : this.parseMaybeAssignAllowIn();
1435
1436 if (
1437 decl.init === null &&
1438 !allowMissingInitializer &&
1439 !(
1440 isFor &&
1441 // Only the first binding of a for-in/of head may omit the initializer
1442 declarations.length === 0 &&
1443 (this.match(tt._in) || this.isContextual(tt._of))
1444 )
1445 ) {
1446 if (decl.id.type !== "Identifier") {
1447 this.raise(
1448 Errors.DeclarationMissingInitializer,
1449 this.state.lastTokEndLoc!,
1450 {
1451 kind: "destructuring",
1452 },
1453 );
1454 } else if (
1455 kind === "const" ||
1456 kind === "using" ||
1457 kind === "await using"
1458 ) {
1459 this.raise(
1460 Errors.DeclarationMissingInitializer,
1461 this.state.lastTokEndLoc!,
1462 { kind },
1463 );
1464 }
1465 }
1466 declarations.push(this.finishNode(decl, "VariableDeclarator"));
1467 if (!this.eat(tt.comma)) break;
1468 }
1469 return node;
1470 }
1471
1472 parseVarId(
1473 this: Parser,

Callers

nothing calls this directly

Calls 4

matchMethod · 0.80
finishNodeMethod · 0.80
parseVarIdMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected