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

Function parseVar

packages/babel-parser/src/parser/statement.ts:1415–1467  ·  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

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