Skip to content

Commit

Permalink
Use startLoc.index instead of carrying around start (#15056)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 18, 2022
1 parent 061d5a3 commit ef0749d
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 308 deletions.
186 changes: 46 additions & 140 deletions packages/babel-parser/src/parser/expression.ts

Large diffs are not rendered by default.

12 changes: 4 additions & 8 deletions packages/babel-parser/src/parser/lval.ts
Expand Up @@ -67,7 +67,6 @@ export default abstract class LValParser extends NodeUtils {
): T;
abstract parseObjPropValue(
prop: any,
startPos: number | null,
startLoc: Position | null,
isGenerator: boolean,
isAsync: boolean,
Expand Down Expand Up @@ -439,7 +438,7 @@ export default abstract class LValParser extends NodeUtils {
// https://tc39.es/ecma262/#prod-BindingProperty
parseBindingProperty(this: Parser): ObjectMember | RestElement {
const prop = this.startNode<ObjectMember | RestElement>();
const { type, start: startPos, startLoc } = this.state;
const { type, startLoc } = this.state;
if (type === tt.ellipsis) {
return this.parseBindingRestProperty(prop as Undone<RestElement>);
} else if (type === tt.privateName) {
Expand All @@ -452,7 +451,6 @@ export default abstract class LValParser extends NodeUtils {
(prop as Undone<ObjectMember>).method = false;
return this.parseObjPropValue(
prop as Undone<ObjectMember>,
startPos,
startLoc,
false /* isGenerator */,
false /* isAsync */,
Expand All @@ -468,7 +466,7 @@ export default abstract class LValParser extends NodeUtils {
): Pattern | TSParameterProperty {
const left = this.parseMaybeDefault();
this.parseAssignableListItemTypes(left);
const elt = this.parseMaybeDefault(left.start, left.loc.start, left);
const elt = this.parseMaybeDefault(left.loc.start, left);
if (decorators.length) {
left.decorators = decorators;
}
Expand All @@ -484,16 +482,14 @@ export default abstract class LValParser extends NodeUtils {
// https://tc39.es/ecma262/#prod-BindingElement
parseMaybeDefault(
this: Parser,
startPos?: number | null,
startLoc?: Position | null,
left?: Pattern | null,
): Pattern {
startLoc = startLoc ?? this.state.startLoc;
startPos = startPos ?? this.state.start;
startLoc ??= this.state.startLoc;
left = left ?? this.parseBindingAtom();
if (!this.eat(tt.eq)) return left;

const node = this.startNodeAt<AssignmentPattern>(startPos, startLoc);
const node = this.startNodeAt<AssignmentPattern>(startLoc);
node.left = left;
node.right = this.parseMaybeAssignAllowIn();
return this.finishNode(node, "AssignmentPattern");
Expand Down
14 changes: 7 additions & 7 deletions packages/babel-parser/src/parser/node.ts
Expand Up @@ -102,14 +102,14 @@ export abstract class NodeUtils extends UtilParser {
return new Node(this, this.state.start, this.state.startLoc);
}

startNodeAt<T extends NodeType>(pos: number, loc: Position): Undone<T> {
startNodeAt<T extends NodeType>(loc: Position): Undone<T> {
// @ts-expect-error cast Node as Undone<T>
return new Node(this, pos, loc);
return new Node(this, loc.index, loc);
}

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

// Finish an AST node, adding `type` and `end` properties.
Expand Down Expand Up @@ -141,10 +141,10 @@ export abstract class NodeUtils extends UtilParser {
return node as T;
}

resetStartLocation(node: NodeBase, start: number, startLoc: Position): void {
node.start = start;
resetStartLocation(node: NodeBase, startLoc: Position): void {
node.start = startLoc.index;
node.loc.start = startLoc;
if (this.options.ranges) node.range[0] = start;
if (this.options.ranges) node.range[0] = startLoc.index;
}

resetEndLocation(
Expand All @@ -160,6 +160,6 @@ export abstract class NodeUtils extends UtilParser {
* Reset the start location of node to the start location of locationNode
*/
resetStartLocationFromNode(node: NodeBase, locationNode: NodeBase): void {
this.resetStartLocation(node, locationNode.start, locationNode.loc.start);
this.resetStartLocation(node, locationNode.loc.start);
}
}
11 changes: 3 additions & 8 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -573,17 +573,15 @@ export default abstract class StatementParser extends ExpressionParser {
// So that the decorators of any nested class expressions will be dealt with separately
this.state.decoratorStack.push([]);

const startPos = this.state.start;
const startLoc = this.state.startLoc;
let expr: N.Expression;

if (this.match(tt.parenL)) {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
this.next(); // eat '('
expr = this.parseExpression();
this.expect(tt.parenR);
expr = this.wrapParenthesis(startPos, startLoc, expr);
expr = this.wrapParenthesis(startLoc, expr);

const paramsStartLoc = this.state.startLoc;
node.expression = this.parseMaybeDecoratorArguments(expr);
Expand All @@ -600,7 +598,7 @@ export default abstract class StatementParser extends ExpressionParser {
expr = this.parseIdentifier(false);

while (this.eat(tt.dot)) {
const node = this.startNodeAt(startPos, startLoc);
const node = this.startNodeAt(startLoc);
node.object = expr;
if (this.match(tt.privateName)) {
this.classScope.usePrivateName(
Expand Down Expand Up @@ -2200,10 +2198,7 @@ export default abstract class StatementParser extends ExpressionParser {
if (this.isContextual(tt._as)) {
if (!node.specifiers) node.specifiers = [];

const specifier = this.startNodeAt(
this.state.lastTokStart,
this.state.lastTokStartLoc,
);
const specifier = this.startNodeAt(this.state.lastTokStartLoc);

this.next();

Expand Down
15 changes: 3 additions & 12 deletions packages/babel-parser/src/plugins/estree.ts
Expand Up @@ -349,14 +349,12 @@ export default (superClass: typeof Parser) =>

parseObjectProperty(
prop: N.ObjectProperty,
startPos: number | undefined | null,
startLoc: Position | undefined | null,
isPattern: boolean,
refExpressionErrors?: ExpressionErrors | null,
): N.ObjectProperty | undefined | null {
const node: N.EstreeProperty = super.parseObjectProperty(
prop,
startPos,
startLoc,
isPattern,
refExpressionErrors,
Expand Down Expand Up @@ -483,18 +481,11 @@ export default (superClass: typeof Parser) =>

parseSubscript(
base: N.Expression,
startPos: number,
startLoc: Position,
noCalls: boolean | undefined | null,
state: N.ParseSubscriptState,
) {
const node = super.parseSubscript(
base,
startPos,
startLoc,
noCalls,
state,
);
const node = super.parseSubscript(base, startLoc, noCalls, state);

if (state.optionalChainMember) {
// https://github.com/estree/estree/blob/master/es2020.md#chainexpression
Expand Down Expand Up @@ -547,8 +538,8 @@ export default (superClass: typeof Parser) =>
return toESTreeLocation(super.finishNodeAt(node, type, endLoc));
}

resetStartLocation(node: N.Node, start: number, startLoc: Position) {
super.resetStartLocation(node, start, startLoc);
resetStartLocation(node: N.Node, startLoc: Position) {
super.resetStartLocation(node, startLoc);
toESTreeLocation(node);
}

Expand Down

0 comments on commit ef0749d

Please sign in to comment.