From 6349459955082ab355515b48057619b9796442bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 21 May 2021 17:15:17 -0400 Subject: [PATCH] fix: guard curPosition with isLookahead --- packages/babel-parser/src/tokenizer/index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 8a1f7550d890..524d05a694e9 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -320,7 +320,8 @@ export default class Tokenizer extends ParserErrors { } skipBlockComment(): void { - const startLoc = this.state.curPosition(); + let startLoc; + if (!this.isLookahead) startLoc = this.state.curPosition(); const start = this.state.pos; const end = this.input.indexOf("*/", this.state.pos + 2); if (end === -1) throw this.raise(start, Errors.UnterminatedComment); @@ -339,6 +340,7 @@ export default class Tokenizer extends ParserErrors { // 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; + /*:: invariant(startLoc) */ this.pushComment( true, @@ -352,7 +354,8 @@ export default class Tokenizer extends ParserErrors { skipLineComment(startSkip: number): void { const start = this.state.pos; - const startLoc = this.state.curPosition(); + let startLoc; + if (!this.isLookahead) startLoc = this.state.curPosition(); let ch = this.input.charCodeAt((this.state.pos += startSkip)); if (this.state.pos < this.length) { while (!isNewLine(ch) && ++this.state.pos < this.length) { @@ -363,6 +366,7 @@ export default class Tokenizer extends ParserErrors { // 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; + /*:: invariant(startLoc) */ this.pushComment( false,