From 76f033f8c70f4cdb8f8382de97de5656bea91c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 5 Aug 2020 20:21:35 -0400 Subject: [PATCH] simplify isLookaheadRelational method (#11922) * refactor: move isLookaheadRelational to flow plugins * refactor: simplify isLookaheadRelational to isLookaheadToken_lt --- packages/babel-parser/src/parser/util.js | 13 ------------- packages/babel-parser/src/plugins/flow.js | 14 +++++++++++++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index d6aa742df695..8b47a85be631 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -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 = { @@ -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) { - return true; - } - const afterNext = this.input.charCodeAt(next + 1); - return afterNext !== op.charCodeAt(0) && afterNext !== charCodes.equalsTo; - } - return false; - } - // TODO expectRelational(op: "<" | ">"): void { diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index b532ce04f47e..1887cfa68a60 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -2873,7 +2873,7 @@ export default (superClass: Class): Class => 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; @@ -3475,4 +3475,16 @@ export default (superClass: Class): Class => 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; + } };