mirror of https://github.com/sveltejs/svelte
parent
1cb63ed16f
commit
582e8bb5f7
@ -1,27 +1,38 @@
|
|||||||
import Node from './shared/Node';
|
import Node from './shared/Node';
|
||||||
import Expression from './shared/Expression';
|
|
||||||
import Component from '../Component';
|
import Component from '../Component';
|
||||||
|
import { walk } from 'estree-walker';
|
||||||
|
|
||||||
class Pattern {
|
const applicable = new Set(['Identifier', 'ObjectExpression', 'ArrayExpression', 'Property']);
|
||||||
constructor(node) {
|
|
||||||
// TODO implement `let:foo={bar}` and `let:contact={{ name, address }}` etc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Let extends Node {
|
export default class Let extends Node {
|
||||||
type: 'Let';
|
type: 'Let';
|
||||||
name: string;
|
name: string;
|
||||||
pattern: Pattern;
|
value: string;
|
||||||
names: string[];
|
names: string[] = [];
|
||||||
|
|
||||||
constructor(component: Component, parent, scope, info) {
|
constructor(component: Component, parent, scope, info) {
|
||||||
super(component, parent, scope, info);
|
super(component, parent, scope, info);
|
||||||
|
|
||||||
this.name = info.name;
|
this.name = info.name;
|
||||||
|
this.value = info.expression && `[✂${info.expression.start}-${info.expression.end}✂]`;
|
||||||
|
|
||||||
this.pattern = info.expression && new Pattern(info.expression);
|
if (info.expression) {
|
||||||
|
walk(info.expression, {
|
||||||
|
enter: node => {
|
||||||
|
if (!applicable.has(node.type)) {
|
||||||
|
component.error(node, {
|
||||||
|
code: 'invalid-let',
|
||||||
|
message: `let directive value must be an identifier or an object/array pattern`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// TODO
|
if (node.type === 'Identifier') {
|
||||||
this.names = [this.name];
|
this.names.push(node.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.names.push(this.name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +0,0 @@
|
|||||||
import Let from '../../../nodes/Let';
|
|
||||||
|
|
||||||
export function get_slot_context(lets: Let[]) {
|
|
||||||
if (lets.length === 0) return '';
|
|
||||||
return `{ ${lets.map(l => `${l.name}: ${l.name}`)} }`; // TODO support aliased/destructured lets
|
|
||||||
}
|
|
@ -0,0 +1,6 @@
|
|||||||
|
import Let from '../../../nodes/Let';
|
||||||
|
|
||||||
|
export function get_slot_scope(lets: Let[]) {
|
||||||
|
if (lets.length === 0) return '';
|
||||||
|
return `{ ${lets.map(l => l.value ? `${l.name}: ${l.value}` : l.name).join(', ')} }`;
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
<div>
|
||||||
|
{#each things as thing}
|
||||||
|
<slot {thing}/>
|
||||||
|
{/each}
|
||||||
|
</div>
|
@ -0,0 +1,24 @@
|
|||||||
|
export default {
|
||||||
|
props: {
|
||||||
|
things: [1, 2, 3]
|
||||||
|
},
|
||||||
|
|
||||||
|
html: `
|
||||||
|
<div>
|
||||||
|
<span>1</span>
|
||||||
|
<span>2</span>
|
||||||
|
<span>3</span>
|
||||||
|
</div>`,
|
||||||
|
|
||||||
|
test({ assert, component, target }) {
|
||||||
|
component.things = [1, 2, 3, 4];
|
||||||
|
assert.htmlEqual(target.innerHTML, `
|
||||||
|
<div>
|
||||||
|
<span>1</span>
|
||||||
|
<span>2</span>
|
||||||
|
<span>3</span>
|
||||||
|
<span>4</span>
|
||||||
|
</div>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,9 @@
|
|||||||
|
<script>
|
||||||
|
import Nested from './Nested.html';
|
||||||
|
|
||||||
|
export let things;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Nested {things} let:thing={x}>
|
||||||
|
<span>{x}</span>
|
||||||
|
</Nested>
|
@ -0,0 +1,5 @@
|
|||||||
|
<div>
|
||||||
|
{#each things as thing}
|
||||||
|
<slot {thing}/>
|
||||||
|
{/each}
|
||||||
|
</div>
|
@ -0,0 +1,34 @@
|
|||||||
|
export default {
|
||||||
|
props: {
|
||||||
|
things: [
|
||||||
|
{ num: 1 },
|
||||||
|
{ num: 2 },
|
||||||
|
{ num: 3 }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
|
||||||
|
html: `
|
||||||
|
<div>
|
||||||
|
<span>1</span>
|
||||||
|
<span>2</span>
|
||||||
|
<span>3</span>
|
||||||
|
</div>`,
|
||||||
|
|
||||||
|
test({ assert, component, target }) {
|
||||||
|
component.things = [
|
||||||
|
{ num: 1 },
|
||||||
|
{ num: 2 },
|
||||||
|
{ num: 3 },
|
||||||
|
{ num: 4 }
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.htmlEqual(target.innerHTML, `
|
||||||
|
<div>
|
||||||
|
<span>1</span>
|
||||||
|
<span>2</span>
|
||||||
|
<span>3</span>
|
||||||
|
<span>4</span>
|
||||||
|
</div>
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,9 @@
|
|||||||
|
<script>
|
||||||
|
import Nested from './Nested.html';
|
||||||
|
|
||||||
|
export let things;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Nested {things} let:thing="{{ num }}">
|
||||||
|
<span>{num}</span>
|
||||||
|
</Nested>
|
@ -1,15 +0,0 @@
|
|||||||
[{
|
|
||||||
"code": "invalid-slot-placement",
|
|
||||||
"message": "<slot> cannot be a child of an each-block",
|
|
||||||
"start": {
|
|
||||||
"line": 2,
|
|
||||||
"column": 1,
|
|
||||||
"character": 25
|
|
||||||
},
|
|
||||||
"end": {
|
|
||||||
"line": 2,
|
|
||||||
"column": 1,
|
|
||||||
"character": 25
|
|
||||||
},
|
|
||||||
"pos": 25
|
|
||||||
}]
|
|
@ -1,3 +0,0 @@
|
|||||||
{#each things as thing}
|
|
||||||
<slot name='foo'></slot>
|
|
||||||
{/each}
|
|
Loading…
Reference in new issue