Skip to content

Commit

Permalink
perf: replace lookahead by lookaheadCharCode
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 28, 2019
1 parent 94e47c3 commit 23a29b6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -590,7 +590,7 @@ export default class ExpressionParser extends LValParser {
} else if (this.match(tt.questionDot)) {
this.expectPlugin("optionalChaining");
state.optionalChainMember = true;
if (noCalls && this.lookahead().type === tt.parenL) {
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
state.stop = true;
return base;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -170,7 +170,7 @@ export default class StatementParser extends ExpressionParser {
case tt._for:
return this.parseForStatement(node);
case tt._function:
if (this.lookahead().type === tt.dot) break;
if (this.lookaheadCharCode() === charCodes.dot) break;
if (context) {
if (this.state.strict) {
this.raise(
Expand Down Expand Up @@ -223,8 +223,11 @@ export default class StatementParser extends ExpressionParser {
return this.parseEmptyStatement(node);
case tt._export:
case tt._import: {
const nextToken = this.lookahead();
if (nextToken.type === tt.parenL || nextToken.type === tt.dot) {
const nextTokenCharCode = this.lookaheadCharCode();
if (
nextTokenCharCode === charCodes.leftParenthesis ||
nextTokenCharCode === charCodes.dot
) {
break;
}

Expand Down
9 changes: 7 additions & 2 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -19,6 +19,7 @@ import {
BIND_CLASS,
} from "../../util/scopeflags";
import TypeScriptScopeHandler from "./scope";
import * as charCodes from "charcodes";

type TsModifier =
| "readonly"
Expand Down Expand Up @@ -657,7 +658,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
: this.match(tt._null)
? "TSNullKeyword"
: keywordTypeFromName(this.state.value);
if (type !== undefined && this.lookahead().type !== tt.dot) {
if (
type !== undefined &&
this.lookaheadCharCode() !== charCodes.dot
) {
const node: N.TsKeywordType = this.startNode();
this.next();
return this.finishNode(node, type);
Expand Down Expand Up @@ -1203,7 +1207,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>

tsIsExternalModuleReference(): boolean {
return (
this.isContextual("require") && this.lookahead().type === tt.parenL
this.isContextual("require") &&
this.lookaheadCharCode() === charCodes.leftParenthesis
);
}

Expand Down
9 changes: 9 additions & 0 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -17,6 +17,7 @@ import {
import State from "./state";

const VALID_REGEX_FLAGS = new Set(["g", "m", "s", "i", "y", "u"]);
const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;

// The following character codes are forbidden from being
// an immediate sibling of NumericLiteralSeparator _
Expand Down Expand Up @@ -168,6 +169,14 @@ export default class Tokenizer extends LocationParser {
return curr;
}

lookaheadCharCode(): number {
const thisTokEnd = this.state.pos;
skipWhiteSpace.lastIndex = thisTokEnd;
const skip = skipWhiteSpace.exec(this.input);
const next = thisTokEnd + skip[0].length;
return this.input.charCodeAt(next);
}

// Toggle strict mode. Re-reads the next number or string to please
// pedantic tests (`"use strict"; 010;` should fail).

Expand Down

0 comments on commit 23a29b6

Please sign in to comment.