Skip to content

Commit 90a372e

Browse files
committed
brute force new keyed each block syntax
1 parent cc0055c commit 90a372e

File tree

5 files changed

+61
-35
lines changed

5 files changed

+61
-35
lines changed

src/parse/state/mustache.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,25 @@ export default function mustache(parser: Parser) {
274274
parser.allowWhitespace();
275275
}
276276

277-
if (parser.eat('@')) {
277+
if (parser.eat('key')) {
278+
parser.requireWhitespace();
279+
280+
const expression = readExpression(parser);
281+
282+
// TODO eventually, we should accept any expression, and turn
283+
// it into a function. For now, assume that every expression
284+
// follows the `foo.id` pattern, and equates to `@id`
285+
if (
286+
expression.type !== 'MemberExpression' ||
287+
expression.property.computed ||
288+
expression.property.type !== 'Identifier'
289+
) {
290+
parser.error('invalid key', expression.start);
291+
}
292+
293+
block.key = expression.property.name;
294+
parser.allowWhitespace();
295+
} else if (parser.eat('@')) {
278296
block.key = parser.readIdentifier();
279297
if (!block.key) parser.error(`Expected name`);
280298
parser.allowWhitespace();

test/parser/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'assert';
22
import fs from 'fs';
33
import { svelte, tryToLoadJson } from '../helpers.js';
44

5-
describe.only('parse', () => {
5+
describe('parse', () => {
66
fs.readdirSync('test/parser/samples').forEach(dir => {
77
if (dir[0] === '.') return;
88

test/parser/samples/each-block-keyed/_actual-v2.json

+29-21
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,38 @@
77
"children": [
88
{
99
"start": 0,
10-
"end": 35,
11-
"type": "Text",
12-
"data": "{#each todos as todo key todo.id}\n\t"
13-
},
14-
{
15-
"start": 35,
16-
"end": 48,
17-
"type": "Element",
18-
"name": "p",
19-
"attributes": [],
10+
"end": 56,
11+
"type": "EachBlock",
12+
"expression": {
13+
"type": "Identifier",
14+
"start": 7,
15+
"end": 12,
16+
"name": "todos"
17+
},
2018
"children": [
2119
{
22-
"start": 38,
23-
"end": 44,
24-
"type": "Text",
25-
"data": "{todo}"
20+
"start": 35,
21+
"end": 48,
22+
"type": "Element",
23+
"name": "p",
24+
"attributes": [],
25+
"children": [
26+
{
27+
"start": 38,
28+
"end": 44,
29+
"type": "MustacheTag",
30+
"expression": {
31+
"type": "Identifier",
32+
"start": 39,
33+
"end": 43,
34+
"name": "todo"
35+
}
36+
}
37+
]
2638
}
27-
]
28-
},
29-
{
30-
"start": 48,
31-
"end": 56,
32-
"type": "Text",
33-
"data": "\n{/each}"
39+
],
40+
"context": "todo",
41+
"key": "id"
3442
}
3543
]
3644
},

test/parser/samples/each-block-keyed/output-v2.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
{
2-
"hash": 2025411181,
2+
"hash": "3sm0ec",
33
"html": {
44
"start": 0,
5-
"end": 54,
5+
"end": 56,
66
"type": "Fragment",
77
"children": [
88
{
99
"start": 0,
10-
"end": 54,
10+
"end": 56,
1111
"type": "EachBlock",
1212
"expression": {
1313
"type": "Identifier",
14-
"start": 8,
15-
"end": 13,
14+
"start": 7,
15+
"end": 12,
1616
"name": "todos"
1717
},
1818
"children": [
1919
{
20-
"start": 29,
21-
"end": 44,
20+
"start": 35,
21+
"end": 48,
2222
"type": "Element",
2323
"name": "p",
2424
"attributes": [],
2525
"children": [
2626
{
27-
"start": 32,
28-
"end": 40,
27+
"start": 38,
28+
"end": 44,
2929
"type": "MustacheTag",
3030
"expression": {
3131
"type": "Identifier",
32-
"start": 34,
33-
"end": 38,
32+
"start": 39,
33+
"end": 43,
3434
"name": "todo"
3535
}
3636
}

test/parser/samples/yield/input.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{yield}}
1+
{{yield}}

0 commit comments

Comments
 (0)