Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary work during lookahead #9982

Merged
merged 3 commits into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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