Skip to content

Commit

Permalink
refactor: add nextTokenStart method
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 29, 2019
1 parent 8244a7a commit 4f6dcc4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
20 changes: 4 additions & 16 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -8,7 +8,7 @@ import {
isIdentifierStart,
keywordRelationalOperator,
} from "../util/identifier";
import { lineBreak, skipWhiteSpace } from "../util/whitespace";
import { lineBreak } from "../util/whitespace";
import * as charCodes from "charcodes";
import {
BIND_CLASS,
Expand Down Expand Up @@ -105,10 +105,7 @@ export default class StatementParser extends ExpressionParser {
if (!this.isContextual("let")) {
return false;
}
skipWhiteSpace.lastIndex = this.state.pos;
const skip = skipWhiteSpace.exec(this.input);
// $FlowIgnore
const next = this.state.pos + skip[0].length;
const next = this.nextTokenStart();
const nextCh = this.input.charCodeAt(next);
// For ambiguous cases, determine if a LexicalDeclaration (or only a
// Statement) is allowed here. If context is not empty then only a Statement
Expand Down Expand Up @@ -1760,18 +1757,9 @@ export default class StatementParser extends ExpressionParser {

isAsyncFunction(): boolean {
if (!this.isContextual("async")) return false;

const { pos } = this.state;

skipWhiteSpace.lastIndex = pos;
const skip = skipWhiteSpace.exec(this.input);

if (!skip || !skip.length) return false;

const next = pos + skip[0].length;

const next = this.nextTokenStart();
return (
!lineBreak.test(this.input.slice(pos, next)) &&
!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)))
Expand Down
11 changes: 7 additions & 4 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -13,11 +13,11 @@ import {
lineBreakG,
isNewLine,
isWhitespace,
skipWhiteSpace,
} from "../util/whitespace";
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 @@ -169,13 +169,16 @@ export default class Tokenizer extends LocationParser {
return curr;
}

lookaheadCharCode(): number {
nextTokenStart(): number {
const thisTokEnd = this.state.pos;
skipWhiteSpace.lastIndex = thisTokEnd;
const skip = skipWhiteSpace.exec(this.input);
// $FlowIgnore: The skipWhiteSpace ensures to match any string
const next = thisTokEnd + skip[0].length;
return this.input.charCodeAt(next);
return thisTokEnd + skip[0].length;
}

lookaheadCharCode(): number {
return this.input.charCodeAt(this.nextTokenStart());
}

// Toggle strict mode. Re-reads the next number or string to please
Expand Down

0 comments on commit 4f6dcc4

Please sign in to comment.