diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 9171d50710..ffb8cba8ca 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -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 diff --git a/packages/babel-parser/src/tokenizer/state.js b/packages/babel-parser/src/tokenizer/state.js index 41c184f1e5..f2bf621ee6 100644 --- a/packages/babel-parser/src/tokenizer/state.js +++ b/packages/babel-parser/src/tokenizer/state.js @@ -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(); }