From 416fc0c81bd63afaf7a7d2fde19bfc1e1b0a485b Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 27 Mar 2018 20:07:03 -0400 Subject: [PATCH] include spread with other attributes --- src/parse/state/tag.ts | 45 +++++++++++--------------- test/parser/samples/spread/output.json | 25 +++++++------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index 897478c6dd..4f7e7a117b 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -178,8 +178,6 @@ export default function tag(parser: Parser) { parser.allowWhitespace(); } - element.spread = readSpread(parser); - const uniqueNames = new Set(); let attribute; @@ -291,6 +289,23 @@ function readTagName(parser: Parser) { function readAttribute(parser: Parser, uniqueNames: Set) { const start = parser.index; + if (parser.eat('{{')) { + parser.allowWhitespace(); + parser.eat('...', true, 'Expected spread operator (...)'); + + const expression = readExpression(parser); + + parser.allowWhitespace(); + parser.eat('}}', true); + + return { + start, + end: parser.index, + type: 'Spread', + expression + }; + } + let name = parser.readUntil(/(\s|=|\/|>)/); if (!name) return null; if (uniqueNames.has(name)) { @@ -385,28 +400,4 @@ function readSequence(parser: Parser, done: () => boolean) { } parser.error(`Unexpected end of input`); -} - -function readSpread(parser: Parser) { - const start = parser.index; - - if (parser.eat('{{...')) { - const expression = readExpression(parser); - parser.allowWhitespace(); - - if (!parser.eat('}}')) { - parser.error(`Expected }}`); - } - - parser.allowWhitespace(); - - return { - start, - end: parser.index, - type: 'Spread', - expression, - }; - } - - return null; -} +} \ No newline at end of file diff --git a/test/parser/samples/spread/output.json b/test/parser/samples/spread/output.json index b4ae6a7e01..365c6be625 100644 --- a/test/parser/samples/spread/output.json +++ b/test/parser/samples/spread/output.json @@ -10,19 +10,20 @@ "end": 24, "type": "Element", "name": "div", - "attributes": [], - "children": [], - "spread": { - "start": 5, - "end": 17, - "type": "Spread", - "expression": { - "type": "Identifier", - "start": 10, - "end": 15, - "name": "props" + "attributes": [ + { + "start": 5, + "end": 17, + "type": "Spread", + "expression": { + "type": "Identifier", + "start": 10, + "end": 15, + "name": "props" + } } - } + ], + "children": [] } ] },