refactor(parser): remove refNeedsArrowPos (#13419)
* fix(parser:expression): remove refNeedsArrowPos in parseExprListItem * feat: use refExpressionErrors to catch optional parameters * feat: delete useless refNeedArrowPos * fix: forgotten deletion * fix: lint and factorize * fix: delete useless comment * fix: review corrections * fix: review corrections pt2 * fix: review correction pt2 * fix: nit correction * fix: update types * Update packages/babel-parser/src/parser/util.js * fix: add invariant comment in flow and ts file Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
import type Parser from "../../parser";
|
||||
import { types as tt, type TokenType } from "../../tokenizer/types";
|
||||
import * as N from "../../types";
|
||||
import type { Pos, Position } from "../../util/location";
|
||||
import type { Position } from "../../util/location";
|
||||
import { types as tc } from "../../tokenizer/context";
|
||||
import * as charCodes from "charcodes";
|
||||
import { isIteratorStart, isKeyword } from "../../util/identifier";
|
||||
@@ -1906,20 +1906,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
expr: N.Expression,
|
||||
startPos: number,
|
||||
startLoc: Position,
|
||||
refNeedsArrowPos?: ?Pos,
|
||||
refExpressionErrors?: ?ExpressionErrors,
|
||||
): N.Expression {
|
||||
if (!this.match(tt.question)) return expr;
|
||||
|
||||
// only use the expensive "tryParse" method if there is a question mark
|
||||
// and if we come from inside parens
|
||||
if (refNeedsArrowPos) {
|
||||
if (this.state.maybeInArrowParameters) {
|
||||
const result = this.tryParse(() =>
|
||||
super.parseConditional(expr, startPos, startLoc),
|
||||
);
|
||||
|
||||
if (!result.node) {
|
||||
// $FlowIgnore
|
||||
refNeedsArrowPos.start = result.error.pos || this.state.start;
|
||||
if (result.error) {
|
||||
/*:: invariant(refExpressionErrors != null) */
|
||||
super.setOptionalParametersError(refExpressionErrors, result.error);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
@@ -1973,7 +1976,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
node.test = expr;
|
||||
node.consequent = consequent;
|
||||
node.alternate = this.forwardNoArrowParamsConversionAt(node, () =>
|
||||
this.parseMaybeAssign(undefined, undefined, undefined),
|
||||
this.parseMaybeAssign(undefined, undefined),
|
||||
);
|
||||
|
||||
return this.finishNode(node, "ConditionalExpression");
|
||||
@@ -2820,7 +2823,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
parseMaybeAssign(
|
||||
refExpressionErrors?: ?ExpressionErrors,
|
||||
afterLeftParse?: Function,
|
||||
refNeedsArrowPos?: ?Pos,
|
||||
): N.Expression {
|
||||
let state = null;
|
||||
|
||||
@@ -2833,16 +2835,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
state = this.state.clone();
|
||||
|
||||
jsx = this.tryParse(
|
||||
() =>
|
||||
super.parseMaybeAssign(
|
||||
refExpressionErrors,
|
||||
afterLeftParse,
|
||||
refNeedsArrowPos,
|
||||
),
|
||||
() => super.parseMaybeAssign(refExpressionErrors, afterLeftParse),
|
||||
state,
|
||||
);
|
||||
/*:: invariant(!jsx.aborted) */
|
||||
|
||||
/*:: invariant(!jsx.aborted) */
|
||||
/*:: invariant(jsx.node != null) */
|
||||
if (!jsx.error) return jsx.node;
|
||||
|
||||
// Remove `tc.j_expr` and `tc.j_oTag` from context added
|
||||
@@ -2870,7 +2868,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
const result = super.parseMaybeAssign(
|
||||
refExpressionErrors,
|
||||
afterLeftParse,
|
||||
refNeedsArrowPos,
|
||||
);
|
||||
|
||||
this.resetStartLocationFromNode(result, typeParameters);
|
||||
@@ -2950,11 +2947,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
);
|
||||
}
|
||||
|
||||
return super.parseMaybeAssign(
|
||||
refExpressionErrors,
|
||||
afterLeftParse,
|
||||
refNeedsArrowPos,
|
||||
);
|
||||
return super.parseMaybeAssign(refExpressionErrors, afterLeftParse);
|
||||
}
|
||||
|
||||
// handle return types for arrow functions
|
||||
@@ -3068,6 +3061,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
state,
|
||||
);
|
||||
|
||||
/*:: invariant(arrow.node != null) */
|
||||
if (!arrow.error && !arrow.aborted) return arrow.node;
|
||||
|
||||
const result = this.tryParse(
|
||||
|
||||
@@ -10,7 +10,7 @@ import type State from "../../tokenizer/state";
|
||||
import { types as tt } from "../../tokenizer/types";
|
||||
import { types as ct } from "../../tokenizer/context";
|
||||
import * as N from "../../types";
|
||||
import type { Pos, Position } from "../../util/location";
|
||||
import type { Position } from "../../util/location";
|
||||
import type Parser from "../../parser";
|
||||
import {
|
||||
type BindingTypes,
|
||||
@@ -2467,16 +2467,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
expr: N.Expression,
|
||||
startPos: number,
|
||||
startLoc: Position,
|
||||
refNeedsArrowPos?: ?Pos,
|
||||
refExpressionErrors?: ?ExpressionErrors,
|
||||
): N.Expression {
|
||||
// only do the expensive clone if there is a question mark
|
||||
// and if we come from inside parens
|
||||
if (!refNeedsArrowPos || !this.match(tt.question)) {
|
||||
if (!this.state.maybeInArrowParameters || !this.match(tt.question)) {
|
||||
return super.parseConditional(
|
||||
expr,
|
||||
startPos,
|
||||
startLoc,
|
||||
refNeedsArrowPos,
|
||||
refExpressionErrors,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2485,8 +2485,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
);
|
||||
|
||||
if (!result.node) {
|
||||
// $FlowIgnore
|
||||
refNeedsArrowPos.start = result.error.pos || this.state.start;
|
||||
if (result.error) {
|
||||
/*:: invariant(refExpressionErrors != null) */
|
||||
super.setOptionalParametersError(refExpressionErrors, result.error);
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
if (result.error) this.state = result.failState;
|
||||
@@ -2734,8 +2737,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
state = this.state.clone();
|
||||
|
||||
jsx = this.tryParse(() => super.parseMaybeAssign(...args), state);
|
||||
/*:: invariant(!jsx.aborted) */
|
||||
|
||||
/*:: invariant(!jsx.aborted) */
|
||||
/*:: invariant(jsx.node != null) */
|
||||
if (!jsx.error) return jsx.node;
|
||||
|
||||
// Remove `tc.j_expr` and `tc.j_oTag` from context added
|
||||
@@ -2778,6 +2782,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return expr;
|
||||
}, state);
|
||||
|
||||
/*:: invariant(arrow.node != null) */
|
||||
if (!arrow.error && !arrow.aborted) return arrow.node;
|
||||
|
||||
if (!jsx) {
|
||||
@@ -2790,6 +2795,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
// But don't directly call `this.tsParseTypeAssertion` because we want to handle any binary after it.
|
||||
typeCast = this.tryParse(() => super.parseMaybeAssign(...args), state);
|
||||
/*:: invariant(!typeCast.aborted) */
|
||||
/*:: invariant(typeCast.node != null) */
|
||||
if (!typeCast.error) return typeCast.node;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user