Skip to content

Commit

Permalink
refactor: remove lookahead usage from babylon core
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 29, 2019
1 parent 19690e8 commit b89dc65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
14 changes: 6 additions & 8 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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"`);
}
}
Expand All @@ -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")
);
}

Expand Down Expand Up @@ -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")
);
}

Expand Down
26 changes: 22 additions & 4 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -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/;

Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b89dc65

Please sign in to comment.