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

simplify isLookaheadRelational method #11922

Merged
merged 2 commits into from Aug 6, 2020
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
13 changes: 0 additions & 13 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -6,7 +6,6 @@ import State from "../tokenizer/state";
import type { Node } from "../types";
import { lineBreak } from "../util/whitespace";
import { isIdentifierChar } from "../util/identifier";
import * as charCodes from "charcodes";
import { Errors } from "./error";

type TryParse<Node, Error, Thrown, Aborted, FailState> = {
Expand Down Expand Up @@ -35,18 +34,6 @@ export default class UtilParser extends Tokenizer {
return this.match(tt.relational) && this.state.value === op;
}

isLookaheadRelational(op: "<" | ">"): boolean {
const next = this.nextTokenStart();
if (this.input.charAt(next) === op) {
if (next + 1 === this.input.length) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is removed in isLookaheadToken_lt because if next + 1 is the length, afterNext is NaN, thus it always satisfies

afterNext !== charCodes.lessThan && afterNext !== charCodes.equalsTo

return true;
}
const afterNext = this.input.charCodeAt(next + 1);
return afterNext !== op.charCodeAt(0) && afterNext !== charCodes.equalsTo;
}
return false;
}

// TODO

expectRelational(op: "<" | ">"): void {
Expand Down
14 changes: 13 additions & 1 deletion packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2873,7 +2873,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
noCalls: ?boolean,
subscriptState: N.ParseSubscriptState,
): N.Expression {
if (this.match(tt.questionDot) && this.isLookaheadRelational("<")) {
if (this.match(tt.questionDot) && this.isLookaheadToken_lt()) {
subscriptState.optionalChainMember = true;
if (noCalls) {
subscriptState.stop = true;
Expand Down Expand Up @@ -3475,4 +3475,16 @@ export default (superClass: Class<Parser>): Class<Parser> =>
super.updateContext(prevType);
}
}

// check if the next token is a tt.relation("<")
isLookaheadToken_lt(): boolean {
const next = this.nextTokenStart();
if (this.input.charCodeAt(next) === charCodes.lessThan) {
const afterNext = this.input.charCodeAt(next + 1);
return (
afterNext !== charCodes.lessThan && afterNext !== charCodes.equalsTo
);
}
return false;
}
};