From b89dc656c2242f2686ee3d03fca6d164cc92a5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Thu, 29 Aug 2019 15:45:27 -0400 Subject: [PATCH] refactor: remove lookahead usage from babylon core --- packages/babel-parser/src/parser/statement.js | 14 +++++----- packages/babel-parser/src/parser/util.js | 26 ++++++++++++++++--- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index a761e6dfc77b..2facc7108622 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1738,10 +1738,10 @@ export default class StatementParser extends ExpressionParser { maybeParseExportDeclaration(node: N.Node): boolean { if (this.shouldParseExportDeclaration()) { if (this.isContextual("async")) { - const next = this.lookahead(); + const next = this.nextTokenStart(); // export async; - if (next.type !== tt._function) { + if (!this.isUnparsedContextual(next, "function")) { this.unexpected(next.start, `Unexpected token, expected "function"`); } } @@ -1760,9 +1760,7 @@ export default class StatementParser extends ExpressionParser { const next = this.nextTokenStart(); return ( !lineBreak.test(this.input.slice(this.state.pos, next)) && - this.input.slice(next, next + 8) === "function" && - (next + 8 === this.length || - !isIdentifierChar(this.input.charCodeAt(next + 8))) + this.isUnparsedContextual(next, "function") ); } @@ -1824,10 +1822,10 @@ export default class StatementParser extends ExpressionParser { return false; } - const lookahead = this.lookahead(); + const next = this.nextTokenStart(); return ( - lookahead.type === tt.comma || - (lookahead.type === tt.name && lookahead.value === "from") + this.input.charCodeAt(next) === charCodes.comma || + this.isUnparsedContextual(next, "from") ); } diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index bd6139a2480a..678bb6b18379 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -4,6 +4,8 @@ import { types as tt, type TokenType } from "../tokenizer/types"; import Tokenizer from "../tokenizer"; import type { Node } from "../types"; import { lineBreak, skipWhiteSpace } from "../util/whitespace"; +import { isIdentifierChar } from "../util/identifier"; +import * as charCodes from "charcodes"; const literal = /^('|")((?:\\?.)*?)\1/; @@ -26,8 +28,15 @@ export default class UtilParser extends Tokenizer { } isLookaheadRelational(op: "<" | ">"): boolean { - const l = this.lookahead(); - return l.type === tt.relational && l.value === op; + 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 @@ -60,9 +69,18 @@ export default class UtilParser extends Tokenizer { ); } + isUnparsedContextual(nameStart: number, name: string): boolean { + const nameEnd = nameStart + name.length; + return ( + this.input.slice(nameStart, nameEnd) === name && + (nameEnd === this.input.length || + !isIdentifierChar(this.input.charCodeAt(nameEnd))) + ); + } + isLookaheadContextual(name: string): boolean { - const l = this.lookahead(); - return l.type === tt.name && l.value === name; + const next = this.nextTokenStart(); + return this.isUnparsedContextual(next, name); } // Consumes contextual keyword if possible.