Avoid unnecessary work during lookahead (#9982)

* Do not call pushComment when doing lookahead

* Do no updateContext when doing lookahead

* Do not clone contexts anymore
This commit is contained in:
Daniel Tschinder 2019-05-15 17:02:12 -07:00 committed by GitHub
parent 47eb1ddfe0
commit 4da7a01aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -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 {
@ -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),
@ -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),
@ -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

View File

@ -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();
}