diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index 94eb85440f63..493ca1d082ce 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -42,7 +42,10 @@ import { isIdentifierStart, canBeReservedWord, } from "../util/identifier"; -import { Position, createPositionWithColumnOffset } from "../util/location"; +import { + type Position, + createPositionWithColumnOffset, +} from "../util/location"; import * as charCodes from "charcodes"; import { BIND_OUTSIDE, @@ -223,11 +226,10 @@ export default abstract class ExpressionParser extends LValParser { this: Parser, refExpressionErrors?: ExpressionErrors, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; const expr = this.parseMaybeAssign(refExpressionErrors); if (this.match(tt.comma)) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.expressions = [expr]; while (this.eat(tt.comma)) { node.expressions.push(this.parseMaybeAssign(refExpressionErrors)); @@ -278,13 +280,12 @@ export default abstract class ExpressionParser extends LValParser { refExpressionErrors?: ExpressionErrors | null, afterLeftParse?: Function, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; if (this.isContextual(tt._yield)) { if (this.prodParam.hasYield) { let left = this.parseYield(); if (afterLeftParse) { - left = afterLeftParse.call(this, left, startPos, startLoc); + left = afterLeftParse.call(this, left, startLoc); } return left; } @@ -305,10 +306,10 @@ export default abstract class ExpressionParser extends LValParser { let left = this.parseMaybeConditional(refExpressionErrors); if (afterLeftParse) { - left = afterLeftParse.call(this, left, startPos, startLoc); + left = afterLeftParse.call(this, left, startLoc); } if (tokenIsAssignment(this.state.type)) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); const operator = this.state.value; node.operator = operator; @@ -316,21 +317,22 @@ export default abstract class ExpressionParser extends LValParser { this.toAssignable(left, /* isLHS */ true); node.left = left; + const startIndex = startLoc.index; if ( refExpressionErrors.doubleProtoLoc != null && - refExpressionErrors.doubleProtoLoc.index >= startPos + refExpressionErrors.doubleProtoLoc.index >= startIndex ) { refExpressionErrors.doubleProtoLoc = null; // reset because double __proto__ is valid in assignment expression } if ( refExpressionErrors.shorthandAssignLoc != null && - refExpressionErrors.shorthandAssignLoc.index >= startPos + refExpressionErrors.shorthandAssignLoc.index >= startIndex ) { refExpressionErrors.shorthandAssignLoc = null; // reset because shorthand default was used correctly } if ( refExpressionErrors.privateKeyLoc != null && - refExpressionErrors.privateKeyLoc.index >= startPos + refExpressionErrors.privateKeyLoc.index >= startIndex ) { this.checkDestructuringPrivate(refExpressionErrors); refExpressionErrors.privateKeyLoc = null; // reset because `({ #x: x })` is an assignable pattern @@ -360,7 +362,6 @@ export default abstract class ExpressionParser extends LValParser { this: Parser, refExpressionErrors: ExpressionErrors, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; const potentialArrowAt = this.state.potentialArrowAt; const expr = this.parseExprOps(refExpressionErrors); @@ -369,19 +370,18 @@ export default abstract class ExpressionParser extends LValParser { return expr; } - return this.parseConditional(expr, startPos, startLoc, refExpressionErrors); + return this.parseConditional(expr, startLoc, refExpressionErrors); } parseConditional( this: Parser, expr: N.Expression, - startPos: number, startLoc: Position, // eslint-disable-next-line @typescript-eslint/no-unused-vars refExpressionErrors?: ExpressionErrors | null, ): N.Expression { if (this.eat(tt.question)) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.test = expr; node.consequent = this.parseMaybeAssignAllowIn(); this.expect(tt.colon); @@ -407,7 +407,6 @@ export default abstract class ExpressionParser extends LValParser { this: Parser, refExpressionErrors: ExpressionErrors, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; const potentialArrowAt = this.state.potentialArrowAt; const expr = this.parseMaybeUnaryOrPrivate(refExpressionErrors); @@ -416,7 +415,7 @@ export default abstract class ExpressionParser extends LValParser { return expr; } - return this.parseExprOp(expr, startPos, startLoc, -1); + return this.parseExprOp(expr, startLoc, -1); } // Parse binary operators with the operator precedence parsing @@ -428,7 +427,6 @@ export default abstract class ExpressionParser extends LValParser { parseExprOp( this: Parser, left: N.Expression | N.PrivateName, - leftStartPos: number, leftStartLoc: Position, minPrec: number, ): N.Expression { @@ -465,7 +463,6 @@ export default abstract class ExpressionParser extends LValParser { this.checkPipelineAtInfixOperator(left, leftStartLoc); } const node = this.startNodeAt( - leftStartPos, leftStartLoc, ); node.left = left; @@ -513,12 +510,7 @@ export default abstract class ExpressionParser extends LValParser { }); } - return this.parseExprOp( - finishedNode, - leftStartPos, - leftStartLoc, - minPrec, - ); + return this.parseExprOp(finishedNode, leftStartLoc, minPrec); } } return left; @@ -532,7 +524,6 @@ export default abstract class ExpressionParser extends LValParser { op: TokenType, prec: number, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; switch (op) { case tt.pipeline: @@ -551,7 +542,6 @@ export default abstract class ExpressionParser extends LValParser { } return this.parseSmartPipelineBodyInStyle( this.parseExprOpBaseRightExpr(op, prec), - startPos, startLoc, ); }); @@ -576,12 +566,10 @@ export default abstract class ExpressionParser extends LValParser { op: TokenType, prec: number, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; return this.parseExprOp( this.parseMaybeUnaryOrPrivate(), - startPos, startLoc, tokenIsRightAssociative(op) ? prec - 1 : prec, ); @@ -628,13 +616,12 @@ export default abstract class ExpressionParser extends LValParser { refExpressionErrors?: ExpressionErrors | null, sawUnary?: boolean, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; const isAwait = this.isContextual(tt._await); if (isAwait && this.isAwaitAllowed()) { this.next(); - const expr = this.parseAwait(startPos, startLoc); + const expr = this.parseAwait(startLoc); if (!sawUnary) this.checkExponentialAfterUnary(expr); return expr; } @@ -686,7 +673,7 @@ export default abstract class ExpressionParser extends LValParser { : tokenCanStartExpression(type) && !this.match(tt.modulo); if (startsExpr && !this.isAmbiguousAwait()) { this.raiseOverwrite(Errors.AwaitNotInAsyncContext, { at: startLoc }); - return this.parseAwait(startPos, startLoc); + return this.parseAwait(startLoc); } } @@ -709,12 +696,11 @@ export default abstract class ExpressionParser extends LValParser { return node; } - const startPos = this.state.start; const startLoc = this.state.startLoc; let expr = this.parseExprSubscripts(refExpressionErrors); if (this.checkExpressionErrors(refExpressionErrors, false)) return expr; while (tokenIsPostfix(this.state.type) && !this.canInsertSemicolon()) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.operator = this.state.value; node.prefix = false; node.argument = expr; @@ -732,7 +718,6 @@ export default abstract class ExpressionParser extends LValParser { this: Parser, refExpressionErrors?: ExpressionErrors | null, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; const potentialArrowAt = this.state.potentialArrowAt; const expr = this.parseExprAtom(refExpressionErrors); @@ -741,13 +726,12 @@ export default abstract class ExpressionParser extends LValParser { return expr; } - return this.parseSubscripts(expr, startPos, startLoc); + return this.parseSubscripts(expr, startLoc); } parseSubscripts( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, noCalls?: boolean | null, ): N.Expression { @@ -757,7 +741,7 @@ export default abstract class ExpressionParser extends LValParser { stop: false, }; do { - base = this.parseSubscript(base, startPos, startLoc, noCalls, state); + base = this.parseSubscript(base, startLoc, noCalls, state); // After parsing a subscript, this isn't "async" for sure. state.maybeAsyncArrow = false; @@ -772,21 +756,15 @@ export default abstract class ExpressionParser extends LValParser { parseSubscript( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, noCalls: boolean | undefined | null, state: N.ParseSubscriptState, ): N.Expression { const { type } = this.state; if (!noCalls && type === tt.doubleColon) { - return this.parseBind(base, startPos, startLoc, noCalls, state); + return this.parseBind(base, startLoc, noCalls, state); } else if (tokenIsTemplate(type)) { - return this.parseTaggedTemplateExpression( - base, - startPos, - startLoc, - state, - ); + return this.parseTaggedTemplateExpression(base, startLoc, state); } let optional = false; @@ -804,7 +782,6 @@ export default abstract class ExpressionParser extends LValParser { if (!noCalls && this.match(tt.parenL)) { return this.parseCoverCallAndAsyncArrowHead( base, - startPos, startLoc, state, optional, @@ -812,14 +789,7 @@ export default abstract class ExpressionParser extends LValParser { } else { const computed = this.eat(tt.bracketL); if (computed || optional || this.eat(tt.dot)) { - return this.parseMember( - base, - startPos, - startLoc, - state, - computed, - optional, - ); + return this.parseMember(base, startLoc, state, computed, optional); } else { state.stop = true; return base; @@ -834,7 +804,6 @@ export default abstract class ExpressionParser extends LValParser { parseMember( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, state: N.ParseSubscriptState, computed: boolean, @@ -842,7 +811,7 @@ export default abstract class ExpressionParser extends LValParser { ): N.OptionalMemberExpression | N.MemberExpression { const node = this.startNodeAt< N.OptionalMemberExpression | N.MemberExpression - >(startPos, startLoc); + >(startLoc); node.object = base; node.computed = computed; if (computed) { @@ -870,19 +839,17 @@ export default abstract class ExpressionParser extends LValParser { parseBind( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, noCalls: boolean | undefined | null, state: N.ParseSubscriptState, ): N.Expression { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.object = base; this.next(); // eat '::' node.callee = this.parseNoCallExpr(); state.stop = true; return this.parseSubscripts( this.finishNode(node, "BindExpression"), - startPos, startLoc, noCalls, ); @@ -895,7 +862,6 @@ export default abstract class ExpressionParser extends LValParser { parseCoverCallAndAsyncArrowHead( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, state: N.ParseSubscriptState, optional: boolean, @@ -907,7 +873,6 @@ export default abstract class ExpressionParser extends LValParser { this.next(); // eat `(` const node = this.startNodeAt( - startPos, startLoc, ); node.callee = base; @@ -950,7 +915,7 @@ export default abstract class ExpressionParser extends LValParser { this.expressionScope.validateAsPattern(); this.expressionScope.exit(); finishedNode = this.parseAsyncArrowFromCallExpression( - this.startNodeAt(startPos, startLoc), + this.startNodeAt(startLoc), finishedNode as N.CallExpression, ); } else { @@ -978,14 +943,10 @@ export default abstract class ExpressionParser extends LValParser { parseTaggedTemplateExpression( this: Parser, base: N.Expression, - startPos: number, startLoc: Position, state: N.ParseSubscriptState, ): N.TaggedTemplateExpression { - const node = this.startNodeAt( - startPos, - startLoc, - ); + const node = this.startNodeAt(startLoc); node.tag = base; node.quasi = this.parseTemplate(true); if (state.optionalChainMember) { @@ -1120,9 +1081,8 @@ export default abstract class ExpressionParser extends LValParser { // Parse a no-call expression (like argument of `new` or `::` operators). // https://tc39.es/ecma262/#prod-MemberExpression parseNoCallExpr(this: Parser): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; - return this.parseSubscripts(this.parseExprAtom(), startPos, startLoc, true); + return this.parseSubscripts(this.parseExprAtom(), startLoc, true); } // Parse an atomic expression — either a single token that is an @@ -1619,14 +1579,9 @@ export default abstract class ExpressionParser extends LValParser { parsePrivateName(): N.PrivateName { const node = this.startNode(); const id = this.startNodeAt( - this.state.start + 1, // The position is hardcoded because we merge `#` and name into a single // tt.privateName token - new Position( - this.state.curLine, - this.state.start + 1 - this.state.lineStart, - this.state.start + 1, - ), + createPositionWithColumnOffset(this.state.startLoc, 1), ); const name = this.state.value; this.next(); // eat #name; @@ -1773,7 +1728,6 @@ export default abstract class ExpressionParser extends LValParser { this: Parser, canBeArrow: boolean, ): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; let val; @@ -1785,7 +1739,6 @@ export default abstract class ExpressionParser extends LValParser { this.state.maybeInArrowParameters = true; this.state.inFSharpPipelineDirectBody = false; - const innerStartPos = this.state.start; const innerStartLoc = this.state.startLoc; const exprList: N.Expression[] = []; const refExpressionErrors = new ExpressionErrors(); @@ -1810,15 +1763,10 @@ export default abstract class ExpressionParser extends LValParser { } if (this.match(tt.ellipsis)) { - const spreadNodeStartPos = this.state.start; const spreadNodeStartLoc = this.state.startLoc; spreadStartLoc = this.state.startLoc; exprList.push( - this.parseParenItem( - this.parseRestBinding(), - spreadNodeStartPos, - spreadNodeStartLoc, - ), + this.parseParenItem(this.parseRestBinding(), spreadNodeStartLoc), ); if (!this.checkCommaAfterRest(charCodes.rightParenthesis)) { @@ -1840,10 +1788,7 @@ export default abstract class ExpressionParser extends LValParser { this.state.maybeInArrowParameters = oldMaybeInArrowParameters; this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody; - let arrowNode = this.startNodeAt( - startPos, - startLoc, - ); + let arrowNode = this.startNodeAt(startLoc); if ( canBeArrow && this.shouldParseArrow(exprList) && @@ -1867,10 +1812,7 @@ export default abstract class ExpressionParser extends LValParser { this.toReferencedListDeep(exprList, /* isParenthesizedExpr */ true); if (exprList.length > 1) { - val = this.startNodeAt( - innerStartPos, - innerStartLoc, - ); + val = this.startNodeAt(innerStartLoc); val.expressions = exprList; // finish node at current location so it can pick up comments after `)` this.finishNode(val, "SequenceExpression"); @@ -1880,35 +1822,28 @@ export default abstract class ExpressionParser extends LValParser { } return this.wrapParenthesis( - startPos, startLoc, // @ts-expect-error todo(flow->ts) val, ); } - wrapParenthesis( - startPos: number, - startLoc: Position, - expression: N.Expression, - ): N.Expression { + wrapParenthesis(startLoc: Position, expression: N.Expression): N.Expression { if (!this.options.createParenthesizedExpressions) { this.addExtra(expression, "parenthesized", true); - this.addExtra(expression, "parenStart", startPos); + this.addExtra(expression, "parenStart", startLoc.index); this.takeSurroundingComments( expression, - startPos, + startLoc.index, this.state.lastTokEndLoc.index, ); return expression; } - const parenExpression = this.startNodeAt( - startPos, - startLoc, - ); + const parenExpression = + this.startNodeAt(startLoc); parenExpression.expression = expression; return this.finishNode(parenExpression, "ParenthesizedExpression"); } @@ -1929,8 +1864,6 @@ export default abstract class ExpressionParser extends LValParser { parseParenItem( node: N.Expression, // eslint-disable-next-line @typescript-eslint/no-unused-vars - startPos: number, - // eslint-disable-next-line @typescript-eslint/no-unused-vars startLoc: Position, ): N.Expression { return node; @@ -2004,7 +1937,6 @@ export default abstract class ExpressionParser extends LValParser { const { start, startLoc, end, value } = this.state; const elemStart = start + 1; const elem = this.startNodeAt( - elemStart, createPositionWithColumnOffset(startLoc, 1), ); if (value === null) { @@ -2189,7 +2121,6 @@ export default abstract class ExpressionParser extends LValParser { const prop = this.startNode(); let isAsync = false; let isAccessor = false; - let startPos; let startLoc; if (this.match(tt.ellipsis)) { @@ -2205,7 +2136,6 @@ export default abstract class ExpressionParser extends LValParser { prop.method = false; if (refExpressionErrors) { - startPos = this.state.start; startLoc = this.state.startLoc; } @@ -2244,7 +2174,6 @@ export default abstract class ExpressionParser extends LValParser { return this.parseObjPropValue( prop, - startPos, startLoc, isGenerator, isAsync, @@ -2331,7 +2260,6 @@ export default abstract class ExpressionParser extends LValParser { parseObjectProperty( this: Parser, prop: Undone, - startPos: number | undefined | null, startLoc: Position | undefined | null, isPattern: boolean, refExpressionErrors?: ExpressionErrors | null, @@ -2340,7 +2268,7 @@ export default abstract class ExpressionParser extends LValParser { if (this.eat(tt.colon)) { prop.value = isPattern - ? this.parseMaybeDefault(this.state.start, this.state.startLoc) + ? this.parseMaybeDefault(this.state.startLoc) : this.parseMaybeAssignAllowIn(refExpressionErrors); return this.finishNode(prop, "ObjectProperty"); @@ -2355,7 +2283,6 @@ export default abstract class ExpressionParser extends LValParser { if (isPattern) { prop.value = this.parseMaybeDefault( - startPos, startLoc, cloneIdentifier(prop.key), ); @@ -2371,7 +2298,6 @@ export default abstract class ExpressionParser extends LValParser { }); } prop.value = this.parseMaybeDefault( - startPos, startLoc, cloneIdentifier(prop.key), ); @@ -2387,7 +2313,6 @@ export default abstract class ExpressionParser extends LValParser { parseObjPropValue( this: Parser, prop: Undone, - startPos: number | undefined | null, startLoc: Position | undefined | null, isGenerator: boolean, isAsync: boolean, @@ -2405,7 +2330,6 @@ export default abstract class ExpressionParser extends LValParser { ) || this.parseObjectProperty( prop as Undone, - startPos, startLoc, isPattern, refExpressionErrors, @@ -2781,12 +2705,10 @@ export default abstract class ExpressionParser extends LValParser { } elt = null; } else if (this.match(tt.ellipsis)) { - const spreadNodeStartPos = this.state.start; const spreadNodeStartLoc = this.state.startLoc; elt = this.parseParenItem( this.parseSpread(refExpressionErrors), - spreadNodeStartPos, spreadNodeStartLoc, ); } else if (this.match(tt.question)) { @@ -2816,7 +2738,7 @@ export default abstract class ExpressionParser extends LValParser { parseIdentifier(liberal?: boolean): N.Identifier { const node = this.startNode(); - const name = this.parseIdentifierName(node.start, liberal); + const name = this.parseIdentifierName(liberal); return this.createIdentifier(node, name); } @@ -2831,7 +2753,7 @@ export default abstract class ExpressionParser extends LValParser { return this.finishNode(node, "Identifier"); } - parseIdentifierName(pos: number, liberal?: boolean): string { + parseIdentifierName(liberal?: boolean): string { let name: string; const { startLoc, type } = this.state; @@ -2933,12 +2855,8 @@ export default abstract class ExpressionParser extends LValParser { // Parses await expression inside async function. - parseAwait( - this: Parser, - startPos: number, - startLoc: Position, - ): N.AwaitExpression { - const node = this.startNodeAt(startPos, startLoc); + parseAwait(this: Parser, startLoc: Position): N.AwaitExpression { + const node = this.startNodeAt(startLoc); this.expressionScope.recordParameterInitializerError( Errors.AwaitExpressionFormalParameter, @@ -3043,23 +2961,13 @@ export default abstract class ExpressionParser extends LValParser { } } - parseSmartPipelineBodyInStyle( - childExpr: N.Expression, - startPos: number, - startLoc: Position, - ) { + parseSmartPipelineBodyInStyle(childExpr: N.Expression, startLoc: Position) { if (this.isSimpleReference(childExpr)) { - const bodyNode = this.startNodeAt( - startPos, - startLoc, - ); + const bodyNode = this.startNodeAt(startLoc); bodyNode.callee = childExpr; return this.finishNode(bodyNode, "PipelineBareFunction"); } else { - const bodyNode = this.startNodeAt( - startPos, - startLoc, - ); + const bodyNode = this.startNodeAt(startLoc); this.checkSmartPipeTopicBodyEarlyErrors(startLoc); bodyNode.expression = childExpr; return this.finishNode(bodyNode, "PipelineTopicExpression"); @@ -3212,7 +3120,6 @@ export default abstract class ExpressionParser extends LValParser { } parseFSharpPipelineBody(this: Parser, prec: number): N.Expression { - const startPos = this.state.start; const startLoc = this.state.startLoc; this.state.potentialArrowAt = this.state.start; @@ -3221,7 +3128,6 @@ export default abstract class ExpressionParser extends LValParser { const ret = this.parseExprOp( this.parseMaybeUnaryOrPrivate(), - startPos, startLoc, prec, ); diff --git a/packages/babel-parser/src/parser/lval.ts b/packages/babel-parser/src/parser/lval.ts index a1312a7b0e0f..67b52575d1fd 100644 --- a/packages/babel-parser/src/parser/lval.ts +++ b/packages/babel-parser/src/parser/lval.ts @@ -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, @@ -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(); - const { type, start: startPos, startLoc } = this.state; + const { type, startLoc } = this.state; if (type === tt.ellipsis) { return this.parseBindingRestProperty(prop as Undone); } else if (type === tt.privateName) { @@ -452,7 +451,6 @@ export default abstract class LValParser extends NodeUtils { (prop as Undone).method = false; return this.parseObjPropValue( prop as Undone, - startPos, startLoc, false /* isGenerator */, false /* isAsync */, @@ -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; } @@ -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(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.left = left; node.right = this.parseMaybeAssignAllowIn(); return this.finishNode(node, "AssignmentPattern"); diff --git a/packages/babel-parser/src/parser/node.ts b/packages/babel-parser/src/parser/node.ts index 7649ffc0b89c..22dc50124672 100644 --- a/packages/babel-parser/src/parser/node.ts +++ b/packages/babel-parser/src/parser/node.ts @@ -102,14 +102,14 @@ export abstract class NodeUtils extends UtilParser { return new Node(this, this.state.start, this.state.startLoc); } - startNodeAt(pos: number, loc: Position): Undone { + startNodeAt(loc: Position): Undone { // @ts-expect-error cast Node as Undone - return new Node(this, pos, loc); + return new Node(this, loc.index, loc); } /** Start a new node with a previous node's location. */ startNodeAtNode(type: Undone): Undone { - return this.startNodeAt(type.start, type.loc.start); + return this.startNodeAt(type.loc.start); } // Finish an AST node, adding `type` and `end` properties. @@ -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( @@ -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); } } diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 879af34b3dcc..63ca9bf70a4e 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -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); @@ -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( @@ -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(); diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index 89531c705b1b..0e0c2fb1f8bb 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -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, @@ -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 @@ -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); } diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index ce57987624aa..4fb506daa9b9 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -1026,7 +1026,7 @@ export default (superClass: typeof Parser) => node.method = true; node.optional = false; node.value = this.flowParseObjectTypeMethodish( - this.startNodeAt(node.start, node.loc.start), + this.startNodeAt(node.loc.start), ); } else { node.method = false; @@ -1303,7 +1303,7 @@ export default (superClass: typeof Parser) => } node.value = this.flowParseObjectTypeMethodish( - this.startNodeAt(node.start, node.loc.start), + this.startNodeAt(node.loc.start), ); if (kind === "get" || kind === "set") { this.flowCheckGetterSetterParams(node); @@ -1382,20 +1382,15 @@ export default (superClass: typeof Parser) => } flowParseQualifiedTypeIdentifier( - startPos?: number, startLoc?: Position, id?: N.Identifier, ): N.FlowQualifiedTypeIdentifier { - startPos = startPos || this.state.start; - startLoc = startLoc || this.state.startLoc; + startLoc ??= this.state.startLoc; let node: N.Identifier | N.FlowQualifiedTypeIdentifier = id || this.flowParseRestrictedIdentifier(true); while (this.eat(tt.dot)) { - const node2 = this.startNodeAt( - startPos, - startLoc, - ); + const node2 = this.startNodeAt(startLoc); node2.qualification = node; node2.id = this.flowParseRestrictedIdentifier(true); node = this.finishNode(node2, "QualifiedTypeIdentifier"); @@ -1405,14 +1400,13 @@ export default (superClass: typeof Parser) => } flowParseGenericType( - startPos: number, startLoc: Position, id: N.Identifier, ): N.FlowGenericTypeAnnotation { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.typeParameters = null; - node.id = this.flowParseQualifiedTypeIdentifier(startPos, startLoc, id); + node.id = this.flowParseQualifiedTypeIdentifier(startLoc, id); if (this.match(tt.lt)) { node.typeParameters = this.flowParseTypeParameterInstantiation(); @@ -1474,7 +1468,7 @@ export default (superClass: typeof Parser) => reinterpretTypeAsFunctionTypeParam( type: N.FlowType, ): N.FlowFunctionTypeParam { - const node = this.startNodeAt(type.start, type.loc.start); + const node = this.startNodeAt(type.loc.start); node.name = null; node.optional = false; node.typeAnnotation = type; @@ -1509,7 +1503,6 @@ export default (superClass: typeof Parser) => } flowIdentToTypeAnnotation( - startPos: number, startLoc: Position, node: Undone, id: N.Identifier, @@ -1539,7 +1532,7 @@ export default (superClass: typeof Parser) => default: this.checkNotUnderscore(id.name); - return this.flowParseGenericType(startPos, startLoc, id); + return this.flowParseGenericType(startLoc, id); } } @@ -1547,7 +1540,6 @@ export default (superClass: typeof Parser) => // primary types are kind of like primary expressions...they're the // primitives with which other types are constructed. flowParsePrimaryType(): N.FlowTypeAnnotation { - const startPos = this.state.start; const startLoc = this.state.startLoc; const node = this.startNode(); let tmp; @@ -1733,7 +1725,6 @@ export default (superClass: typeof Parser) => } return this.flowIdentToTypeAnnotation( - startPos, startLoc, node, this.parseIdentifier(), @@ -1745,7 +1736,6 @@ export default (superClass: typeof Parser) => } flowParsePostfixType(): N.FlowTypeAnnotation { - const startPos = this.state.start; const startLoc = this.state.startLoc; let type = this.flowParsePrimaryType(); let seenOptionalIndexedAccess = false; @@ -1753,7 +1743,7 @@ export default (superClass: typeof Parser) => (this.match(tt.bracketL) || this.match(tt.questionDot)) && !this.canInsertSemicolon() ) { - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); const optional = this.eat(tt.questionDot); seenOptionalIndexedAccess = seenOptionalIndexedAccess || optional; this.expect(tt.bracketL); @@ -1798,7 +1788,7 @@ export default (superClass: typeof Parser) => const param = this.flowParsePrefixType(); if (!this.state.noAnonFunctionType && this.eat(tt.arrow)) { // TODO: This should be a type error. Passing in a SourceLocation, and it expects a Position. - const node = this.startNodeAt(param.start, param.loc.start); + const node = this.startNodeAt(param.loc.start); node.params = [this.reinterpretTypeAsFunctionTypeParam(param)]; node.rest = null; node.this = null; @@ -1845,10 +1835,9 @@ export default (superClass: typeof Parser) => flowParseTypeOrImplicitInstantiation(): N.FlowTypeAnnotation { if (this.state.type === tt.name && this.state.value === "_") { - const startPos = this.state.start; const startLoc = this.state.startLoc; const node = this.parseIdentifier(); - return this.flowParseGenericType(startPos, startLoc, node); + return this.flowParseGenericType(startLoc, node); } else { return this.flowParseType(); } @@ -2031,7 +2020,7 @@ export default (superClass: typeof Parser) => parseConditional( expr: N.Expression, - startPos: number, + startLoc: Position, refExpressionErrors?: ExpressionErrors | null, ): N.Expression { @@ -2057,7 +2046,7 @@ export default (superClass: typeof Parser) => this.expect(tt.question); const state = this.state.clone(); const originalNoArrowAt = this.state.noArrowAt; - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); let { consequent, failed } = this.tryParseConditionalConsequent(); let [valid, invalid] = this.getArrowLikeExpressions(consequent); @@ -2198,10 +2187,10 @@ export default (superClass: typeof Parser) => parseParenItem( node: N.Expression, - startPos: number, + startLoc: Position, ): N.Expression { - node = super.parseParenItem(node, startPos, startLoc); + node = super.parseParenItem(node, startLoc); if (this.eat(tt.question)) { node.optional = true; // Include questionmark in location of node @@ -2211,7 +2200,7 @@ export default (superClass: typeof Parser) => } if (this.match(tt.colon)) { - const typeCastNode = this.startNodeAt(startPos, startLoc); + const typeCastNode = this.startNodeAt(startLoc); typeCastNode.expression = node; typeCastNode.typeAnnotation = this.flowParseTypeAnnotation(); @@ -2636,7 +2625,6 @@ export default (superClass: typeof Parser) => // parse type parameters for object method shorthand parseObjPropValue( prop: Undone, - startPos: number | undefined | null, startLoc: Position | undefined | null, isGenerator: boolean, isAsync: boolean, @@ -2659,7 +2647,6 @@ export default (superClass: typeof Parser) => const result = super.parseObjPropValue( prop, - startPos, startLoc, isGenerator, isAsync, @@ -2702,11 +2689,10 @@ export default (superClass: typeof Parser) => } parseMaybeDefault( - startPos?: number | null, startLoc?: Position | null, left?: N.Pattern | null, ): N.Pattern { - const node = super.parseMaybeDefault(startPos, startLoc, left); + const node = super.parseMaybeDefault(startLoc, left); if ( node.type === "AssignmentPattern" && @@ -3164,18 +3150,18 @@ export default (superClass: typeof Parser) => parseSubscripts( base: N.Expression, - startPos: number, + startLoc: Position, noCalls?: boolean | null, ): N.Expression { if ( base.type === "Identifier" && base.name === "async" && - this.state.noArrowAt.indexOf(startPos) !== -1 + this.state.noArrowAt.indexOf(startLoc.index) !== -1 ) { this.next(); - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.callee = base; node.arguments = super.parseCallExpressionArguments(tt.parenR, false); base = this.finishNode(node, "CallExpression"); @@ -3186,9 +3172,7 @@ export default (superClass: typeof Parser) => ) { const state = this.state.clone(); const arrow = this.tryParse( - abort => - this.parseAsyncArrowWithTypeParameters(startPos, startLoc) || - abort(), + abort => this.parseAsyncArrowWithTypeParameters(startLoc) || abort(), state, ); @@ -3197,7 +3181,7 @@ export default (superClass: typeof Parser) => if (!arrow.error && !arrow.aborted) return arrow.node; const result = this.tryParse( - () => super.parseSubscripts(base, startPos, startLoc, noCalls), + () => super.parseSubscripts(base, startLoc, noCalls), state, ); @@ -3217,12 +3201,12 @@ export default (superClass: typeof Parser) => throw arrow.error || result.error; } - return super.parseSubscripts(base, startPos, startLoc, noCalls); + return super.parseSubscripts(base, startLoc, noCalls); } parseSubscript( base: N.Expression, - startPos: number, + startLoc: Position, noCalls: boolean | undefined | null, subscriptState: N.ParseSubscriptState, @@ -3234,10 +3218,7 @@ export default (superClass: typeof Parser) => return base; } this.next(); - const node = this.startNodeAt( - startPos, - startLoc, - ); + const node = this.startNodeAt(startLoc); node.callee = base; node.typeArguments = this.flowParseTypeParameterInstantiation(); this.expect(tt.parenL); @@ -3247,7 +3228,7 @@ export default (superClass: typeof Parser) => } else if (!noCalls && this.shouldParseTypes() && this.match(tt.lt)) { const node = this.startNodeAt< N.OptionalCallExpression | N.CallExpression - >(startPos, startLoc); + >(startLoc); node.callee = base; const result = this.tryParse(() => { @@ -3272,7 +3253,7 @@ export default (superClass: typeof Parser) => return super.parseSubscript( base, - startPos, + startLoc, noCalls, subscriptState, @@ -3292,13 +3273,9 @@ export default (superClass: typeof Parser) => } parseAsyncArrowWithTypeParameters( - startPos: number, startLoc: Position, ): N.ArrowFunctionExpression | undefined | null { - const node = this.startNodeAt( - startPos, - startLoc, - ); + const node = this.startNodeAt(startLoc); this.parseFunctionParams(node); if (!this.parseArrow(node)) return; return super.parseArrowExpression( diff --git a/packages/babel-parser/src/plugins/jsx/index.ts b/packages/babel-parser/src/plugins/jsx/index.ts index f62ce7938bdc..36ae7b2d7b69 100644 --- a/packages/babel-parser/src/plugins/jsx/index.ts +++ b/packages/babel-parser/src/plugins/jsx/index.ts @@ -278,12 +278,11 @@ export default (superClass: typeof Parser) => // Parse namespaced identifier. jsxParseNamespacedName(): N.JSXNamespacedName { - const startPos = this.state.start; const startLoc = this.state.startLoc; const name = this.jsxParseIdentifier(); if (!this.eat(tt.colon)) return name; - const node = this.startNodeAt(startPos, startLoc); + const node = this.startNodeAt(startLoc); node.namespace = name; node.name = this.jsxParseIdentifier(); return this.finishNode(node, "JSXNamespacedName"); @@ -296,14 +295,13 @@ export default (superClass: typeof Parser) => | N.JSXIdentifier | N.JSXNamespacedName | N.JSXMemberExpression { - const startPos = this.state.start; const startLoc = this.state.startLoc; let node = this.jsxParseNamespacedName(); if (node.type === "JSXNamespacedName") { return node; } while (this.eat(tt.dot)) { - const newNode = this.startNodeAt(startPos, startLoc); + const newNode = this.startNodeAt(startLoc); newNode.object = node; newNode.property = this.jsxParseIdentifier(); node = this.finishNode(newNode, "JSXMemberExpression"); @@ -342,10 +340,7 @@ export default (superClass: typeof Parser) => // at the beginning of the next one (right brace). jsxParseEmptyExpression(): N.JSXEmptyExpression { - const node = this.startNodeAt( - this.state.lastTokEndLoc.index, - this.state.lastTokEndLoc, - ); + const node = this.startNodeAt(this.state.lastTokEndLoc); return this.finishNodeAt(node, "JSXEmptyExpression", this.state.startLoc); } @@ -413,12 +408,8 @@ export default (superClass: typeof Parser) => // Parses JSX opening tag starting after "<". - jsxParseOpeningElementAt( - startPos: number, - startLoc: Position, - ): N.JSXOpeningElement { + jsxParseOpeningElementAt(startLoc: Position): N.JSXOpeningElement { const node = this.startNodeAt( - startPos, startLoc, ); if (this.eat(tt.jsxTagEnd)) { @@ -446,11 +437,8 @@ export default (superClass: typeof Parser) => // Parses JSX closing tag starting after " // Parses entire JSX element, including it"s opening tag // (starting after "<"), attributes, contents and closing tag. - jsxParseElementAt(startPos: number, startLoc: Position): N.JSXElement { - const node = this.startNodeAt(startPos, startLoc); + jsxParseElementAt(startLoc: Position): N.JSXElement { + const node = this.startNodeAt(startLoc); const children = []; - const openingElement = this.jsxParseOpeningElementAt(startPos, startLoc); + const openingElement = this.jsxParseOpeningElementAt(startLoc); let closingElement = null; if (!openingElement.selfClosing) { contents: for (;;) { switch (this.state.type) { case tt.jsxTagStart: - startPos = this.state.start; startLoc = this.state.startLoc; this.next(); if (this.eat(tt.slash)) { - closingElement = this.jsxParseClosingElementAt( - startPos, - startLoc, - ); + closingElement = this.jsxParseClosingElementAt(startLoc); break contents; } - children.push(this.jsxParseElementAt(startPos, startLoc)); + children.push(this.jsxParseElementAt(startLoc)); break; case tt.jsxText: @@ -564,10 +548,9 @@ export default (superClass: typeof Parser) => // Parses entire JSX element from current position. jsxParseElement(): N.JSXElement { - const startPos = this.state.start; const startLoc = this.state.startLoc; this.next(); - return this.jsxParseElementAt(startPos, startLoc); + return this.jsxParseElementAt(startLoc); } setContext(newContext: TokContext) { diff --git a/packages/babel-parser/src/plugins/typescript/index.ts b/packages/babel-parser/src/plugins/typescript/index.ts index f9b908131575..d1595c163a9b 100644 --- a/packages/babel-parser/src/plugins/typescript/index.ts +++ b/packages/babel-parser/src/plugins/typescript/index.ts @@ -1100,7 +1100,7 @@ export default (superClass: ClassWithMixin) => tsParseTupleElementType(): N.TsType | N.TsNamedTupleMember { // parses `...TsType[]` - const { start: startPos, startLoc } = this.state; + const { startLoc } = this.state; const rest = this.eat(tt.ellipsis); let type: N.TsType | N.TsNamedTupleMember = this.tsParseType(); @@ -1133,7 +1133,7 @@ export default (superClass: ClassWithMixin) => } if (rest) { - const restNode = this.startNodeAt(startPos, startLoc); + const restNode = this.startNodeAt(startLoc); restNode.typeAnnotation = type; type = this.finishNode(restNode, "TSRestType"); } @@ -1241,12 +1241,12 @@ export default (superClass: ClassWithMixin) => case tt.parenL: if (process.env.BABEL_8_BREAKING) { if (!this.options.createParenthesizedExpressions) { - const startPos = this.state.start; + const startLoc = this.state.startLoc; this.next(); const type = this.tsParseType(); this.expect(tt.parenR); this.addExtra(type, "parenthesized", true); - this.addExtra(type, "parenStart", startPos); + this.addExtra(type, "parenStart", startLoc.index); return type; } } @@ -2179,7 +2179,6 @@ export default (superClass: ClassWithMixin) => } tsTryParseGenericAsyncArrowFunction( - startPos: number, startLoc: Position, ): N.ArrowFunctionExpression | undefined | null { if (!this.match(tt.lt)) { @@ -2191,10 +2190,7 @@ export default (superClass: ClassWithMixin) => const res: Undone | undefined | null = this.tsTryParseAndCatch(() => { - const node = this.startNodeAt( - startPos, - startLoc, - ); + const node = this.startNodeAt(startLoc); node.typeParameters = this.tsParseTypeParameters(); // Don't use overloaded parseFunctionParams which would look for "<" again. super.parseFunctionParams(node); @@ -2261,8 +2257,7 @@ export default (superClass: ClassWithMixin) => allowModifiers: boolean | undefined | null, decorators: N.Decorator[], ): N.Pattern | N.TSParameterProperty { - // Store original location/position to include modifiers in range - const startPos = this.state.start; + // Store original location to include modifiers in range const startLoc = this.state.startLoc; let accessibility: N.Accessibility | undefined | null; @@ -2293,9 +2288,9 @@ export default (superClass: ClassWithMixin) => 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 (accessibility || readonly || override) { - const pp = this.startNodeAt(startPos, startLoc); + const pp = this.startNodeAt(startLoc); if (decorators.length) { pp.decorators = decorators; } @@ -2409,7 +2404,7 @@ export default (superClass: ClassWithMixin) => parseSubscript( base: N.Expression, - startPos: number, + startLoc: Position, noCalls: boolean | undefined | null, state: N.ParseSubscriptState, @@ -2421,10 +2416,8 @@ export default (superClass: ClassWithMixin) => this.state.canStartJSXElement = false; this.next(); - const nonNullExpression = this.startNodeAt( - startPos, - startLoc, - ); + const nonNullExpression = + this.startNodeAt(startLoc); nonNullExpression.expression = base; return this.finishNode(nonNullExpression, "TSNonNullExpression"); } @@ -2452,10 +2445,8 @@ export default (superClass: ClassWithMixin) => if (!noCalls && this.atPossibleAsyncArrow(base)) { // Almost certainly this is a generic async function `async () => ... // But it might be a call with a type argument `async();` - const asyncArrowFn = this.tsTryParseGenericAsyncArrowFunction( - startPos, - startLoc, - ); + const asyncArrowFn = + this.tsTryParseGenericAsyncArrowFunction(startLoc); if (asyncArrowFn) { return asyncArrowFn; } @@ -2472,7 +2463,7 @@ export default (superClass: ClassWithMixin) => if (tokenIsTemplate(this.state.type)) { const result = super.parseTaggedTemplateExpression( base, - startPos, + startLoc, state, ); @@ -2483,7 +2474,7 @@ export default (superClass: ClassWithMixin) => if (!noCalls && this.eat(tt.parenL)) { const node = this.startNodeAt< N.CallExpression | N.OptionalCallExpression - >(startPos, startLoc); + >(startLoc); node.callee = base; // possibleAsync always false here, because we would have handled it above. // @ts-expect-error (won't be any undefined arguments) @@ -2519,10 +2510,7 @@ export default (superClass: ClassWithMixin) => return; } - const node = this.startNodeAt( - startPos, - startLoc, - ); + const node = this.startNodeAt(startLoc); node.expression = base; node.typeParameters = typeArguments; return this.finishNode(node, "TSInstantiationExpression"); @@ -2548,7 +2536,7 @@ export default (superClass: ClassWithMixin) => } } - return super.parseSubscript(base, startPos, startLoc, noCalls, state); + return super.parseSubscript(base, startLoc, noCalls, state); } parseNewCallee(node: N.NewExpression): void { @@ -2566,7 +2554,6 @@ export default (superClass: ClassWithMixin) => parseExprOp( left: N.Expression, - leftStartPos: number, leftStartLoc: Position, minPrec: number, ): N.Expression { @@ -2575,10 +2562,7 @@ export default (superClass: ClassWithMixin) => !this.hasPrecedingLineBreak() && this.isContextual(tt._as) ) { - const node = this.startNodeAt( - leftStartPos, - leftStartLoc, - ); + const node = this.startNodeAt(leftStartLoc); node.expression = left; const _const = this.tsTryNextParseConstantContext(); if (_const) { @@ -2592,13 +2576,12 @@ export default (superClass: ClassWithMixin) => return this.parseExprOp( // @ts-expect-error todo(flow->ts) node, - leftStartPos, leftStartLoc, minPrec, ); } - return super.parseExprOp(left, leftStartPos, leftStartLoc, minPrec); + return super.parseExprOp(left, leftStartLoc, minPrec); } checkReservedWord( @@ -2990,7 +2973,7 @@ export default (superClass: ClassWithMixin) => // An apparent conditional expression could actually be an optional parameter in an arrow function. parseConditional( expr: N.Expression, - startPos: number, + startLoc: Position, refExpressionErrors?: ExpressionErrors | null, ): N.Expression { @@ -2999,14 +2982,14 @@ export default (superClass: ClassWithMixin) => if (!this.state.maybeInArrowParameters || !this.match(tt.question)) { return super.parseConditional( expr, - startPos, + startLoc, refExpressionErrors, ); } const result = this.tryParse(() => - super.parseConditional(expr, startPos, startLoc), + super.parseConditional(expr, startLoc), ); if (!result.node) { @@ -3025,10 +3008,10 @@ export default (superClass: ClassWithMixin) => // But we parse them here and change them when completing the arrow function. parseParenItem( node: N.Expression, - startPos: number, + startLoc: Position, ): N.Expression { - node = super.parseParenItem(node, startPos, startLoc); + node = super.parseParenItem(node, startLoc); if (this.eat(tt.question)) { node.optional = true; // Include questionmark in location of node @@ -3038,10 +3021,7 @@ export default (superClass: ClassWithMixin) => } if (this.match(tt.colon)) { - const typeCastNode = this.startNodeAt( - startPos, - startLoc, - ); + const typeCastNode = this.startNodeAt(startLoc); typeCastNode.expression = node; typeCastNode.typeAnnotation = this.tsParseTypeAnnotation(); @@ -3058,8 +3038,7 @@ export default (superClass: ClassWithMixin) => return this.tsInAmbientContext(() => this.parseExportDeclaration(node)); } - // Store original location/position - const startPos = this.state.start; + // Store original location const startLoc = this.state.startLoc; const isDeclare = this.eatContextual(tt._declare); @@ -3090,7 +3069,7 @@ export default (superClass: ClassWithMixin) => if (isDeclare) { // Reset location to include `declare` in range - this.resetStartLocation(declaration, startPos, startLoc); + this.resetStartLocation(declaration, startLoc); declaration.declare = true; } @@ -3247,7 +3226,6 @@ export default (superClass: ClassWithMixin) => parseObjPropValue( prop: Undone, - startPos: number | undefined | null, startLoc: Position | undefined | null, isGenerator: boolean, isAsync: boolean, @@ -3260,7 +3238,7 @@ export default (superClass: ClassWithMixin) => return super.parseObjPropValue( prop, - startPos, + startLoc, isGenerator, isAsync, @@ -3674,11 +3652,10 @@ export default (superClass: ClassWithMixin) => } parseMaybeDefault( - startPos?: number | null, startLoc?: Position | null, left?: Pattern | null, ): N.Pattern { - const node = super.parseMaybeDefault(startPos, startLoc, left); + const node = super.parseMaybeDefault(startLoc, left); if ( node.type === "AssignmentPattern" && diff --git a/packages/babel-parser/src/plugins/v8intrinsic.ts b/packages/babel-parser/src/plugins/v8intrinsic.ts index 4c657cd09a90..38a2237f1cde 100644 --- a/packages/babel-parser/src/plugins/v8intrinsic.ts +++ b/packages/babel-parser/src/plugins/v8intrinsic.ts @@ -12,7 +12,7 @@ export default (superClass: typeof Parser) => const node = this.startNode(); this.next(); // eat '%' if (tokenIsIdentifier(this.state.type)) { - const name = this.parseIdentifierName(this.state.start); + const name = this.parseIdentifierName(); const identifier = this.createIdentifier(node, name); // @ts-expect-error: avoid mutating AST types identifier.type = "V8IntrinsicIdentifier";