From 88d7e2012c903ce1858ac51645270050d2026ad0 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sun, 17 Jul 2016 11:08:25 +0200 Subject: [PATCH] Fix lookahead to not add comments to arrays which are not cloned (#76) We do not clone arrays in lookahead() but comments were added to leading/trailing arrays during lookahead, leading to leak to the next next() call. Also extracted parsing of JSXSpreadChild to own parse function. --- src/plugins/jsx/index.js | 24 ++++++++++++++++-------- src/tokenizer/index.js | 3 +-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/plugins/jsx/index.js b/src/plugins/jsx/index.js index 18b92e9fac..6bc98d396e 100644 --- a/src/plugins/jsx/index.js +++ b/src/plugins/jsx/index.js @@ -265,6 +265,18 @@ pp.jsxParseEmptyExpression = function() { return this.finishNodeAt(node, "JSXEmptyExpression", this.start, this.startLoc); }; +// Parse JSX spread child + +pp.jsxParseSpreadChild = function() { + let node = this.startNode(); + this.expect(tt.braceL); + this.expect(tt.ellipsis); + node.expression = this.parseExpression(); + this.expect(tt.braceR); + + return this.finishNode(node, "JSXSpreadChild"); +}; + // Parses JSX expression enclosed into curly brackets. @@ -346,15 +358,11 @@ pp.jsxParseElementAt = function(startPos, startLoc) { case tt.braceL: if (this.lookahead().type === tt.ellipsis) { - let node = this.startNode(); - this.next(); - this.next(); - node.expression = this.parseExpression(); - this.expect(tt.braceR); - children.push(this.finishNode(node, "JSXSpreadChild")); - break; + children.push(this.jsxParseSpreadChild()); + } else { + children.push(this.jsxParseExpressionContainer()); } - children.push(this.jsxParseExpressionContainer()); + break; default: diff --git a/src/tokenizer/index.js b/src/tokenizer/index.js index 1cd5b5c1fc..f787acb1b5 100644 --- a/src/tokenizer/index.js +++ b/src/tokenizer/index.js @@ -166,9 +166,8 @@ export default class Tokenizer { if (!this.isLookahead) { this.state.tokens.push(comment); this.state.comments.push(comment); + this.addComment(comment); } - - this.addComment(comment); } skipBlockComment() {