Faster tokenizer lookahead (#13341)
* refactor: simplify token context structure * add benchmark * perf: return a sub-state on tokenizer lookahead * Update packages/babel-parser/src/tokenizer/index.js Co-authored-by: Brian Ng <bng412@gmail.com> * Update packages/babel-parser/src/tokenizer/index.js Co-authored-by: Brian Ng <bng412@gmail.com> * remove irrelevant comment * fix: guard curPosition with isLookahead * add test cases Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
@@ -9,7 +9,6 @@ import type Parser from "../../parser";
|
||||
import { types as tt, type TokenType } from "../../tokenizer/types";
|
||||
import * as N from "../../types";
|
||||
import type { Pos, Position } from "../../util/location";
|
||||
import type State from "../../tokenizer/state";
|
||||
import { types as tc } from "../../tokenizer/context";
|
||||
import * as charCodes from "charcodes";
|
||||
import { isIteratorStart, isKeyword } from "../../util/identifier";
|
||||
@@ -154,7 +153,7 @@ function hasTypeImportKind(node: N.Node): boolean {
|
||||
return node.importKind === "type" || node.importKind === "typeof";
|
||||
}
|
||||
|
||||
function isMaybeDefaultImport(state: State): boolean {
|
||||
function isMaybeDefaultImport(state: { type: TokenType, value: any }): boolean {
|
||||
return (
|
||||
(state.type === tt.name || !!state.type.keyword) && state.value !== "from"
|
||||
);
|
||||
|
||||
@@ -15,6 +15,10 @@ import { isIdentifierChar, isIdentifierStart } from "../../util/identifier";
|
||||
import type { Position } from "../../util/location";
|
||||
import { isNewLine } from "../../util/whitespace";
|
||||
import { Errors, makeErrorTemplates, ErrorCodes } from "../../parser/error";
|
||||
import type { LookaheadState } from "../../tokenizer/state";
|
||||
import State from "../../tokenizer/state";
|
||||
|
||||
type JSXLookaheadState = LookaheadState & { inPropertyName: boolean };
|
||||
|
||||
const HEX_NUMBER = /^[\da-fA-F]+$/;
|
||||
const DECIMAL_NUMBER = /^\d+$/;
|
||||
@@ -573,6 +577,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
createLookaheadState(state: State): JSXLookaheadState {
|
||||
const lookaheadState = ((super.createLookaheadState(
|
||||
state,
|
||||
): any): JSXLookaheadState);
|
||||
lookaheadState.inPropertyName = state.inPropertyName;
|
||||
return lookaheadState;
|
||||
}
|
||||
|
||||
getTokenFromCode(code: number): void {
|
||||
if (this.state.inPropertyName) return super.getTokenFromCode(code);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user