brute force new keyed each block syntax

pull/1330/head
Rich-Harris 6 years ago
parent cc0055cf82
commit 90a372ebf3

@ -274,7 +274,25 @@ export default function mustache(parser: Parser) {
parser.allowWhitespace(); 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(); block.key = parser.readIdentifier();
if (!block.key) parser.error(`Expected name`); if (!block.key) parser.error(`Expected name`);
parser.allowWhitespace(); parser.allowWhitespace();

@ -2,7 +2,7 @@ import assert from 'assert';
import fs from 'fs'; import fs from 'fs';
import { svelte, tryToLoadJson } from '../helpers.js'; import { svelte, tryToLoadJson } from '../helpers.js';
describe.only('parse', () => { describe('parse', () => {
fs.readdirSync('test/parser/samples').forEach(dir => { fs.readdirSync('test/parser/samples').forEach(dir => {
if (dir[0] === '.') return; if (dir[0] === '.') return;

@ -7,30 +7,38 @@
"children": [ "children": [
{ {
"start": 0, "start": 0,
"end": 35, "end": 56,
"type": "Text", "type": "EachBlock",
"data": "{#each todos as todo key todo.id}\n\t" "expression": {
}, "type": "Identifier",
{ "start": 7,
"start": 35, "end": 12,
"end": 48, "name": "todos"
"type": "Element", },
"name": "p",
"attributes": [],
"children": [ "children": [
{ {
"start": 38, "start": 35,
"end": 44, "end": 48,
"type": "Text", "type": "Element",
"data": "{todo}" "name": "p",
"attributes": [],
"children": [
{
"start": 38,
"end": 44,
"type": "MustacheTag",
"expression": {
"type": "Identifier",
"start": 39,
"end": 43,
"name": "todo"
}
}
]
} }
] ],
}, "context": "todo",
{ "key": "id"
"start": 48,
"end": 56,
"type": "Text",
"data": "\n{/each}"
} }
] ]
}, },

@ -1,36 +1,36 @@
{ {
"hash": 2025411181, "hash": "3sm0ec",
"html": { "html": {
"start": 0, "start": 0,
"end": 54, "end": 56,
"type": "Fragment", "type": "Fragment",
"children": [ "children": [
{ {
"start": 0, "start": 0,
"end": 54, "end": 56,
"type": "EachBlock", "type": "EachBlock",
"expression": { "expression": {
"type": "Identifier", "type": "Identifier",
"start": 8, "start": 7,
"end": 13, "end": 12,
"name": "todos" "name": "todos"
}, },
"children": [ "children": [
{ {
"start": 29, "start": 35,
"end": 44, "end": 48,
"type": "Element", "type": "Element",
"name": "p", "name": "p",
"attributes": [], "attributes": [],
"children": [ "children": [
{ {
"start": 32, "start": 38,
"end": 40, "end": 44,
"type": "MustacheTag", "type": "MustacheTag",
"expression": { "expression": {
"type": "Identifier", "type": "Identifier",
"start": 34, "start": 39,
"end": 38, "end": 43,
"name": "todo" "name": "todo"
} }
} }

Loading…
Cancel
Save