Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always pass type argument to .startNode #16417

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export default abstract class ExpressionParser extends LValParser {
const startLoc = this.state.startLoc;
const expr = this.parseMaybeAssign(refExpressionErrors);
if (this.match(tt.comma)) {
const node = this.startNodeAt(startLoc);
const node = this.startNodeAt<N.SequenceExpression>(startLoc);
node.expressions = [expr];
while (this.eat(tt.comma)) {
node.expressions.push(this.parseMaybeAssign(refExpressionErrors));
Expand Down Expand Up @@ -366,7 +366,7 @@ export default abstract class ExpressionParser extends LValParser {
refExpressionErrors?: ExpressionErrors | null,
): N.Expression {
if (this.eat(tt.question)) {
const node = this.startNodeAt(startLoc);
const node = this.startNodeAt<N.ConditionalExpression>(startLoc);
node.test = expr;
node.consequent = this.parseMaybeAssignAllowIn();
this.expect(tt.colon);
Expand Down Expand Up @@ -826,7 +826,7 @@ export default abstract class ExpressionParser extends LValParser {
noCalls: boolean | undefined | null,
state: N.ParseSubscriptState,
): N.Expression {
const node = this.startNodeAt(startLoc);
const node = this.startNodeAt<N.BindExpression>(startLoc);
node.object = base;
this.next(); // eat '::'
node.callee = this.parseNoCallExpr();
Expand Down Expand Up @@ -1120,7 +1120,7 @@ export default abstract class ExpressionParser extends LValParser {
}

case tt._this:
node = this.startNode();
node = this.startNode<N.ThisExpression>();
this.next();
return this.finishNode(node, "ThisExpression");

Expand Down Expand Up @@ -1213,7 +1213,7 @@ export default abstract class ExpressionParser extends LValParser {
// BindExpression[Yield]
// :: MemberExpression[?Yield]
case tt.doubleColon: {
node = this.startNode();
node = this.startNode<N.BindExpression>();
this.next();
node.object = null;
const callee = (node.callee = this.parseNoCallExpr());
Expand Down Expand Up @@ -2728,7 +2728,7 @@ export default abstract class ExpressionParser extends LValParser {
if (!allowPlaceholder) {
this.raise(Errors.UnexpectedArgumentPlaceholder, this.state.startLoc);
}
const node = this.startNode();
const node = this.startNode<N.ArgumentPlaceholder>();
this.next();
elt = this.finishNode(node, "ArgumentPlaceholder");
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export default class Parser extends StatementParser {

parse(): N.File {
this.enterInitialScopes();
const file = this.startNode() as N.File;
const program = this.startNode() as N.Program;
const file = this.startNode<N.File>();
const program = this.startNode<N.Program>();
this.nextToken();
file.errors = null;
this.parseTopLevel(file, program);
file.errors = this.state.errors;
file.comments.length = this.state.commentsLen;
return file;
return file as N.File;
}
}

Expand Down
8 changes: 5 additions & 3 deletions packages/babel-parser/src/parser/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,19 @@ export function cloneStringLiteral(node: any): any {
export type Undone<T extends NodeType> = Omit<T, "type">;

export abstract class NodeUtils extends UtilParser {
startNode<T extends NodeType>(): Undone<T> {
startNode<T extends NodeType = never>(): Undone<T> {
const loc = this.state.startLoc;
return new Node(this, loc.index, loc) as unknown as Undone<T>;
}

startNodeAt<T extends NodeType>(loc: Position): Undone<T> {
startNodeAt<T extends NodeType = never>(loc: Position): Undone<T> {
return new Node(this, loc.index, loc) as unknown as Undone<T>;
}

/** Start a new node with a previous node's location. */
startNodeAtNode<T extends NodeType>(type: Undone<NodeType>): Undone<T> {
startNodeAtNode<T extends NodeType = never>(
type: Undone<NodeType>,
): Undone<T> {
return this.startNodeAt(type.loc.start);
}

Expand Down
20 changes: 13 additions & 7 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ export default abstract class StatementParser extends ExpressionParser {
// `program` argument. If present, the statements will be appended
// to its body instead of creating a new node.

parseTopLevel(this: Parser, file: N.File, program: N.Program): N.File {
parseTopLevel(
this: Parser,
file: Undone<N.File>,
program: Undone<N.Program>,
): N.File {
file.program = this.parseProgram(program);
file.comments = this.comments;

Expand Down Expand Up @@ -767,7 +771,7 @@ export default abstract class StatementParser extends ExpressionParser {
expr = this.parseIdentifier(false);

while (this.eat(tt.dot)) {
const node = this.startNodeAt(startLoc);
const node = this.startNodeAt<N.MemberExpression>(startLoc);
node.object = expr;
if (this.match(tt.privateName)) {
this.classScope.usePrivateName(
Expand All @@ -792,7 +796,7 @@ export default abstract class StatementParser extends ExpressionParser {

parseMaybeDecoratorArguments(this: Parser, expr: N.Expression): N.Expression {
if (this.eat(tt.parenL)) {
const node = this.startNodeAtNode(expr);
const node = this.startNodeAtNode<N.CallExpression>(expr);
node.callee = expr;
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
this.toReferencedList(node.arguments);
Expand Down Expand Up @@ -1081,7 +1085,7 @@ export default abstract class StatementParser extends ExpressionParser {
const isCase = this.match(tt._case);
if (cur) this.finishNode(cur, "SwitchCase");
// @ts-expect-error Fixme
cases.push((cur = this.startNode()));
cases.push((cur = this.startNode<N.SwitchCase>()));
cur.consequent = [];
this.next();
if (isCase) {
Expand Down Expand Up @@ -2450,7 +2454,9 @@ export default abstract class StatementParser extends ExpressionParser {
if (this.isContextual(tt._as)) {
if (!node.specifiers) node.specifiers = [];

const specifier = this.startNodeAt(this.state.lastTokStartLoc);
const specifier = this.startNodeAt<N.ExportNamespaceSpecifier>(
this.state.lastTokStartLoc,
);

this.next();

Expand Down Expand Up @@ -2796,8 +2802,8 @@ export default abstract class StatementParser extends ExpressionParser {
}
const isMaybeTypeOnly = this.isContextual(tt._type);
const isString = this.match(tt.string);
const node = this.startNode();
node.local = this.parseModuleExportName();
const node = this.startNode<N.ExportSpecifier>();
node.local = this.parseModuleExportName() as N.Identifier;
nodes.push(
this.parseExportSpecifier(
node,
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-parser/src/plugins/estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ export default (superClass: typeof Parser) =>
case "ExportNamedDeclaration":
if (
node.specifiers.length === 1 &&
// @ts-expect-error mutating AST types
node.specifiers[0].type === "ExportNamespaceSpecifier"
) {
// @ts-expect-error mutating AST types
Expand Down Expand Up @@ -524,7 +523,7 @@ export default (superClass: typeof Parser) =>
node.type = node.type.substring(8); // strip Optional prefix
}
if (state.stop) {
const chain = this.startNodeAtNode(node);
const chain = this.startNodeAtNode<N.EstreeChainExpression>(node);
chain.expression = node;
return this.finishNode(chain, "ChainExpression");
}
Expand Down