From 90a372ebf3243b799a4fa52bf3af132725869aed Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Tue, 10 Apr 2018 22:49:21 -0400 Subject: [PATCH] brute force new keyed each block syntax --- src/parse/state/mustache.ts | 20 +++++++- test/parser/index.js | 2 +- .../samples/each-block-keyed/_actual-v2.json | 50 +++++++++++-------- .../samples/each-block-keyed/output-v2.json | 22 ++++---- test/parser/samples/yield/input.html | 2 +- 5 files changed, 61 insertions(+), 35 deletions(-) diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index a73237cf21..de4d00037b 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -274,7 +274,25 @@ export default function mustache(parser: Parser) { parser.allowWhitespace(); } - if (parser.eat('@')) { + if (parser.eat('key')) { + parser.requireWhitespace(); + + const expression = readExpression(parser); + + // TODO eventually, we should accept any expression, and turn + // it into a function. For now, assume that every expression + // follows the `foo.id` pattern, and equates to `@id` + if ( + expression.type !== 'MemberExpression' || + expression.property.computed || + expression.property.type !== 'Identifier' + ) { + parser.error('invalid key', expression.start); + } + + block.key = expression.property.name; + parser.allowWhitespace(); + } else if (parser.eat('@')) { block.key = parser.readIdentifier(); if (!block.key) parser.error(`Expected name`); parser.allowWhitespace(); diff --git a/test/parser/index.js b/test/parser/index.js index d0417c8bfa..0bdb4ad740 100644 --- a/test/parser/index.js +++ b/test/parser/index.js @@ -2,7 +2,7 @@ import assert from 'assert'; import fs from 'fs'; import { svelte, tryToLoadJson } from '../helpers.js'; -describe.only('parse', () => { +describe('parse', () => { fs.readdirSync('test/parser/samples').forEach(dir => { if (dir[0] === '.') return; diff --git a/test/parser/samples/each-block-keyed/_actual-v2.json b/test/parser/samples/each-block-keyed/_actual-v2.json index c039c98ce9..514682b770 100644 --- a/test/parser/samples/each-block-keyed/_actual-v2.json +++ b/test/parser/samples/each-block-keyed/_actual-v2.json @@ -7,30 +7,38 @@ "children": [ { "start": 0, - "end": 35, - "type": "Text", - "data": "{#each todos as todo key todo.id}\n\t" - }, - { - "start": 35, - "end": 48, - "type": "Element", - "name": "p", - "attributes": [], + "end": 56, + "type": "EachBlock", + "expression": { + "type": "Identifier", + "start": 7, + "end": 12, + "name": "todos" + }, "children": [ { - "start": 38, - "end": 44, - "type": "Text", - "data": "{todo}" + "start": 35, + "end": 48, + "type": "Element", + "name": "p", + "attributes": [], + "children": [ + { + "start": 38, + "end": 44, + "type": "MustacheTag", + "expression": { + "type": "Identifier", + "start": 39, + "end": 43, + "name": "todo" + } + } + ] } - ] - }, - { - "start": 48, - "end": 56, - "type": "Text", - "data": "\n{/each}" + ], + "context": "todo", + "key": "id" } ] }, diff --git a/test/parser/samples/each-block-keyed/output-v2.json b/test/parser/samples/each-block-keyed/output-v2.json index 1a16afb82b..514682b770 100644 --- a/test/parser/samples/each-block-keyed/output-v2.json +++ b/test/parser/samples/each-block-keyed/output-v2.json @@ -1,36 +1,36 @@ { - "hash": 2025411181, + "hash": "3sm0ec", "html": { "start": 0, - "end": 54, + "end": 56, "type": "Fragment", "children": [ { "start": 0, - "end": 54, + "end": 56, "type": "EachBlock", "expression": { "type": "Identifier", - "start": 8, - "end": 13, + "start": 7, + "end": 12, "name": "todos" }, "children": [ { - "start": 29, - "end": 44, + "start": 35, + "end": 48, "type": "Element", "name": "p", "attributes": [], "children": [ { - "start": 32, - "end": 40, + "start": 38, + "end": 44, "type": "MustacheTag", "expression": { "type": "Identifier", - "start": 34, - "end": 38, + "start": 39, + "end": 43, "name": "todo" } } diff --git a/test/parser/samples/yield/input.html b/test/parser/samples/yield/input.html index 889d9eeadc..fb5c4b157d 100644 --- a/test/parser/samples/yield/input.html +++ b/test/parser/samples/yield/input.html @@ -1 +1 @@ -{{yield}} +{{yield}} \ No newline at end of file