Skip to content

Commit

Permalink
Avoid unnecessary work during lookahead (#9982)
Browse files Browse the repository at this point in the history
* Do not call pushComment when doing lookahead

* Do no updateContext when doing lookahead

* Do not clone contexts anymore
  • Loading branch information
danez committed May 16, 2019
1 parent 47eb1dd commit 4da7a01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -226,11 +226,9 @@ export default class Tokenizer extends LocationParser {
loc: new SourceLocation(startLoc, endLoc),
};

if (!this.isLookahead) {
if (this.options.tokens) this.state.tokens.push(comment);
this.state.comments.push(comment);
this.addComment(comment);
}
if (this.options.tokens) this.state.tokens.push(comment);
this.state.comments.push(comment);
this.addComment(comment);
}

skipBlockComment(): void {
Expand All @@ -250,6 +248,10 @@ export default class Tokenizer extends LocationParser {
this.state.lineStart = match.index + match[0].length;
}

// If we are doing a lookahead right now we need to advance the position (above code)
// but we do not want to push the comment to the state.
if (this.isLookahead) return;

this.pushComment(
true,
this.input.slice(start + 2, end),
Expand All @@ -276,6 +278,10 @@ export default class Tokenizer extends LocationParser {
}
}

// If we are doing a lookahead right now we need to advance the position (above code)
// but we do not want to push the comment to the state.
if (this.isLookahead) return;

this.pushComment(
false,
this.input.slice(start + startSkip, this.state.pos),
Expand Down Expand Up @@ -350,7 +356,7 @@ export default class Tokenizer extends LocationParser {
this.state.type = type;
this.state.value = val;

this.updateContext(prevType);
if (!this.isLookahead) this.updateContext(prevType);
}

// ### Token reading
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -170,7 +170,7 @@ export default class State {
// $FlowIgnore
let val = this[key];

if ((!skipArrays || key === "context") && Array.isArray(val)) {
if (!skipArrays && Array.isArray(val)) {
val = val.slice();
}

Expand Down

0 comments on commit 4da7a01

Please sign in to comment.