Skip to content

Commit

Permalink
refactor: replace refShorthandDefaultPos by refExpressionErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 15, 2020
1 parent a0a9c64 commit 2e9b91b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 81 deletions.
115 changes: 58 additions & 57 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -41,6 +41,7 @@ import {
SCOPE_PROGRAM,
SCOPE_ASYNC,
} from "../util/scopeflags";
import { ExpressionErrors } from "./util";

export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js
Expand Down Expand Up @@ -127,17 +128,18 @@ export default class ExpressionParser extends LValParser {
// and object pattern might appear (so it's possible to raise
// delayed syntax error at correct position).

parseExpression(noIn?: boolean, refShorthandDefaultPos?: Pos): N.Expression {
parseExpression(
noIn?: boolean,
refExpressionErrors?: ExpressionErrors,
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const expr = this.parseMaybeAssign(noIn, refShorthandDefaultPos);
const expr = this.parseMaybeAssign(noIn, refExpressionErrors);
if (this.match(tt.comma)) {
const node = this.startNodeAt(startPos, startLoc);
node.expressions = [expr];
while (this.eat(tt.comma)) {
node.expressions.push(
this.parseMaybeAssign(noIn, refShorthandDefaultPos),
);
node.expressions.push(this.parseMaybeAssign(noIn, refExpressionErrors));
}
this.toReferencedList(node.expressions);
return this.finishNode(node, "SequenceExpression");
Expand All @@ -150,7 +152,7 @@ export default class ExpressionParser extends LValParser {

parseMaybeAssign(
noIn?: ?boolean,
refShorthandDefaultPos?: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
afterLeftParse?: Function,
refNeedsArrowPos?: ?Pos,
): N.Expression {
Expand All @@ -170,12 +172,12 @@ export default class ExpressionParser extends LValParser {
}
}

let failOnShorthandAssign;
if (refShorthandDefaultPos) {
failOnShorthandAssign = false;
let ownExpressionErrors;
if (refExpressionErrors) {
ownExpressionErrors = false;
} else {
refShorthandDefaultPos = { start: 0 };
failOnShorthandAssign = true;
refExpressionErrors = new ExpressionErrors();
ownExpressionErrors = true;
}

if (this.match(tt.parenL) || this.match(tt.name)) {
Expand All @@ -184,7 +186,7 @@ export default class ExpressionParser extends LValParser {

let left = this.parseMaybeConditional(
noIn,
refShorthandDefaultPos,
refExpressionErrors,
refNeedsArrowPos,
);
if (afterLeftParse) {
Expand All @@ -205,17 +207,17 @@ export default class ExpressionParser extends LValParser {
? this.toAssignable(left, undefined, "assignment expression")
: left;

if (refShorthandDefaultPos.start >= node.left.start) {
refShorthandDefaultPos.start = 0; // reset because shorthand default was used correctly
if (refExpressionErrors.shorthandAssign >= node.left.start) {
refExpressionErrors.shorthandAssign = -1; // reset because shorthand default was used correctly
}

this.checkLVal(left, undefined, undefined, "assignment expression");

this.next();
node.right = this.parseMaybeAssign(noIn);
return this.finishNode(node, "AssignmentExpression");
} else if (failOnShorthandAssign && refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
} else if (ownExpressionErrors) {
this.checkExpressionErrors(refExpressionErrors, true);
}

return left;
Expand All @@ -225,21 +227,21 @@ export default class ExpressionParser extends LValParser {

parseMaybeConditional(
noIn: ?boolean,
refShorthandDefaultPos: Pos,
refExpressionErrors: ExpressionErrors,
refNeedsArrowPos?: ?Pos,
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseExprOps(noIn, refShorthandDefaultPos);
const expr = this.parseExprOps(noIn, refExpressionErrors);

if (
expr.type === "ArrowFunctionExpression" &&
expr.start === potentialArrowAt
) {
return expr;
}
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;

return this.parseConditional(
expr,
Expand Down Expand Up @@ -272,19 +274,22 @@ export default class ExpressionParser extends LValParser {

// Start the precedence parser.

parseExprOps(noIn: ?boolean, refShorthandDefaultPos: Pos): N.Expression {
parseExprOps(
noIn: ?boolean,
refExpressionErrors: ExpressionErrors,
): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseMaybeUnary(refShorthandDefaultPos);
const expr = this.parseMaybeUnary(refExpressionErrors);

if (
expr.type === "ArrowFunctionExpression" &&
expr.start === potentialArrowAt
) {
return expr;
}
if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
if (this.checkExpressionErrors(refExpressionErrors, false)) {
return expr;
}

Expand Down Expand Up @@ -463,7 +468,7 @@ export default class ExpressionParser extends LValParser {

// Parse unary operators, both prefix and postfix.

parseMaybeUnary(refShorthandDefaultPos: ?Pos): N.Expression {
parseMaybeUnary(refExpressionErrors: ?ExpressionErrors): N.Expression {
if (this.isContextual("await") && this.isAwaitAllowed()) {
return this.parseAwait();
} else if (this.state.type.prefix) {
Expand All @@ -479,9 +484,7 @@ export default class ExpressionParser extends LValParser {

node.argument = this.parseMaybeUnary();

if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}
this.checkExpressionErrors(refExpressionErrors, true);

if (update) {
this.checkLVal(node.argument, undefined, undefined, "prefix operation");
Expand All @@ -506,8 +509,8 @@ export default class ExpressionParser extends LValParser {

const startPos = this.state.start;
const startLoc = this.state.startLoc;
let expr = this.parseExprSubscripts(refShorthandDefaultPos);
if (refShorthandDefaultPos && refShorthandDefaultPos.start) return expr;
let expr = this.parseExprSubscripts(refExpressionErrors);
if (this.checkExpressionErrors(refExpressionErrors, false)) return expr;
while (this.state.type.postfix && !this.canInsertSemicolon()) {
const node = this.startNodeAt(startPos, startLoc);
node.operator = this.state.value;
Expand All @@ -522,11 +525,11 @@ export default class ExpressionParser extends LValParser {

// Parse call, dot, and `[]`-subscript expressions.

parseExprSubscripts(refShorthandDefaultPos: ?Pos): N.Expression {
parseExprSubscripts(refExpressionErrors: ?ExpressionErrors): N.Expression {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
const potentialArrowAt = this.state.potentialArrowAt;
const expr = this.parseExprAtom(refShorthandDefaultPos);
const expr = this.parseExprAtom(refExpressionErrors);

if (
expr.type === "ArrowFunctionExpression" &&
Expand All @@ -535,7 +538,7 @@ export default class ExpressionParser extends LValParser {
return expr;
}

if (refShorthandDefaultPos && refShorthandDefaultPos.start) {
if (this.checkExpressionErrors(refExpressionErrors, false)) {
return expr;
}

Expand Down Expand Up @@ -816,7 +819,7 @@ export default class ExpressionParser extends LValParser {
elts.push(
this.parseExprListItem(
false,
possibleAsyncArrow ? { start: 0 } : undefined,
possibleAsyncArrow ? new ExpressionErrors() : undefined,
possibleAsyncArrow ? { start: 0 } : undefined,
allowPlaceholder,
),
Expand Down Expand Up @@ -864,7 +867,7 @@ export default class ExpressionParser extends LValParser {
// `new`, or an expression wrapped in punctuation like `()`, `[]`,
// or `{}`.

parseExprAtom(refShorthandDefaultPos?: ?Pos): N.Expression {
parseExprAtom(refExpressionErrors?: ?ExpressionErrors): N.Expression {
// If a division operator appears in an expression position, the
// tokenizer got confused, and we force it to read a regexp instead.
if (this.state.type === tt.slash) this.readRegexp();
Expand Down Expand Up @@ -1038,7 +1041,7 @@ export default class ExpressionParser extends LValParser {
node.elements = this.parseExprList(
tt.bracketR,
true,
refShorthandDefaultPos,
refExpressionErrors,
node,
);
if (!this.state.maybeInArrowParameters) {
Expand All @@ -1056,7 +1059,7 @@ export default class ExpressionParser extends LValParser {
const oldInFSharpPipelineDirectBody = this.state
.inFSharpPipelineDirectBody;
this.state.inFSharpPipelineDirectBody = false;
const ret = this.parseObj(false, refShorthandDefaultPos);
const ret = this.parseObj(false, refExpressionErrors);
this.state.inFSharpPipelineDirectBody = oldInFSharpPipelineDirectBody;
return ret;
}
Expand Down Expand Up @@ -1263,7 +1266,7 @@ export default class ExpressionParser extends LValParser {
const innerStartPos = this.state.start;
const innerStartLoc = this.state.startLoc;
const exprList = [];
const refShorthandDefaultPos = { start: 0 };
const refExpressionErrors = new ExpressionErrors();
const refNeedsArrowPos = { start: 0 };
let first = true;
let spreadStart;
Expand Down Expand Up @@ -1299,7 +1302,7 @@ export default class ExpressionParser extends LValParser {
exprList.push(
this.parseMaybeAssign(
false,
refShorthandDefaultPos,
refExpressionErrors,
this.parseParenItem,
refNeedsArrowPos,
),
Expand Down Expand Up @@ -1343,9 +1346,7 @@ export default class ExpressionParser extends LValParser {
}
if (optionalCommaStart) this.unexpected(optionalCommaStart);
if (spreadStart) this.unexpected(spreadStart);
if (refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}
this.checkExpressionErrors(refExpressionErrors, true);
if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);

this.toReferencedListDeep(exprList, /* isParenthesizedExpr */ true);
Expand Down Expand Up @@ -1490,7 +1491,7 @@ export default class ExpressionParser extends LValParser {

parseObj<T: N.ObjectPattern | N.ObjectExpression>(
isPattern: boolean,
refShorthandDefaultPos?: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
): T {
const propHash: any = Object.create(null);
let first = true;
Expand All @@ -1511,7 +1512,7 @@ export default class ExpressionParser extends LValParser {
}
}

const prop = this.parseObjectMember(isPattern, refShorthandDefaultPos);
const prop = this.parseObjectMember(isPattern, refExpressionErrors);
// $FlowIgnore RestElement will never be returned if !isPattern
if (!isPattern) this.checkDuplicatedProto(prop, propHash);

Expand Down Expand Up @@ -1550,7 +1551,7 @@ export default class ExpressionParser extends LValParser {

parseObjectMember(
isPattern: boolean,
refShorthandDefaultPos: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
): N.ObjectMember | N.SpreadElement | N.RestElement {
let decorators = [];
if (this.match(tt.at)) {
Expand Down Expand Up @@ -1594,7 +1595,7 @@ export default class ExpressionParser extends LValParser {

prop.method = false;

if (isPattern || refShorthandDefaultPos) {
if (isPattern || refExpressionErrors) {
startPos = this.state.start;
startLoc = this.state.startLoc;
}
Expand All @@ -1621,7 +1622,7 @@ export default class ExpressionParser extends LValParser {
isGenerator,
isAsync,
isPattern,
refShorthandDefaultPos,
refExpressionErrors,
containsEsc,
);

Expand Down Expand Up @@ -1715,14 +1716,14 @@ export default class ExpressionParser extends LValParser {
startPos: ?number,
startLoc: ?Position,
isPattern: boolean,
refShorthandDefaultPos: ?Pos,
refExpressionErrors: ?ExpressionErrors,
): ?N.ObjectProperty {
prop.shorthand = false;

if (this.eat(tt.colon)) {
prop.value = isPattern
? this.parseMaybeDefault(this.state.start, this.state.startLoc)
: this.parseMaybeAssign(false, refShorthandDefaultPos);
: this.parseMaybeAssign(false, refExpressionErrors);

return this.finishNode(prop, "ObjectProperty");
}
Expand All @@ -1736,9 +1737,9 @@ export default class ExpressionParser extends LValParser {
startLoc,
prop.key.__clone(),
);
} else if (this.match(tt.eq) && refShorthandDefaultPos) {
if (!refShorthandDefaultPos.start) {
refShorthandDefaultPos.start = this.state.start;
} else if (this.match(tt.eq) && refExpressionErrors) {
if (refExpressionErrors.shorthandAssign === -1) {
refExpressionErrors.shorthandAssign = this.state.start;
}
prop.value = this.parseMaybeDefault(
startPos,
Expand All @@ -1761,7 +1762,7 @@ export default class ExpressionParser extends LValParser {
isGenerator: boolean,
isAsync: boolean,
isPattern: boolean,
refShorthandDefaultPos: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
containsEsc: boolean,
): void {
const node =
Expand All @@ -1777,7 +1778,7 @@ export default class ExpressionParser extends LValParser {
startPos,
startLoc,
isPattern,
refShorthandDefaultPos,
refExpressionErrors,
);

if (!node) this.unexpected();
Expand Down Expand Up @@ -2019,7 +2020,7 @@ export default class ExpressionParser extends LValParser {
parseExprList(
close: TokenType,
allowEmpty?: boolean,
refShorthandDefaultPos?: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
nodeForExtra?: ?N.Node,
): $ReadOnlyArray<?N.Expression> {
const elts = [];
Expand All @@ -2043,14 +2044,14 @@ export default class ExpressionParser extends LValParser {
}
}

elts.push(this.parseExprListItem(allowEmpty, refShorthandDefaultPos));
elts.push(this.parseExprListItem(allowEmpty, refExpressionErrors));
}
return elts;
}

parseExprListItem(
allowEmpty: ?boolean,
refShorthandDefaultPos: ?Pos,
refExpressionErrors?: ?ExpressionErrors,
refNeedsArrowPos: ?Pos,
allowPlaceholder: ?boolean,
): ?N.Expression {
Expand All @@ -2061,7 +2062,7 @@ export default class ExpressionParser extends LValParser {
const spreadNodeStartPos = this.state.start;
const spreadNodeStartLoc = this.state.startLoc;
elt = this.parseParenItem(
this.parseSpread(refShorthandDefaultPos, refNeedsArrowPos),
this.parseSpread(refExpressionErrors, refNeedsArrowPos),
spreadNodeStartPos,
spreadNodeStartLoc,
);
Expand All @@ -2076,7 +2077,7 @@ export default class ExpressionParser extends LValParser {
} else {
elt = this.parseMaybeAssign(
false,
refShorthandDefaultPos,
refExpressionErrors,
this.parseParenItem,
refNeedsArrowPos,
);
Expand Down

0 comments on commit 2e9b91b

Please sign in to comment.