pull/4419/head
swyx 6 years ago
parent 3de0e4bca4
commit a93242a85e

@ -204,6 +204,9 @@ export default class EachBlockWrapper extends Wrapper {
const snippet = this.node.expression.manipulate(block); const snippet = this.node.expression.manipulate(block);
block.chunks.init.push(b`let ${this.vars.each_block_value} = ${snippet};`); block.chunks.init.push(b`let ${this.vars.each_block_value} = ${snippet};`);
if (this.renderer.options.dev) {
block.chunks.init.push(b`@validate_each_argument(${this.vars.each_block_value});`);
}
// TODO which is better — Object.create(array) or array.slice()? // TODO which is better — Object.create(array) or array.slice()?
renderer.blocks.push(b` renderer.blocks.push(b`
@ -374,9 +377,12 @@ export default class EachBlockWrapper extends Wrapper {
block.chunks.init.push(b` block.chunks.init.push(b`
const ${get_key} = #ctx => ${this.node.key.manipulate(block)}; const ${get_key} = #ctx => ${this.node.key.manipulate(block)};
${this.renderer.options.dev && b`@validate_each_keys(#ctx, ${this.vars.each_block_value}, ${this.vars.get_each_context}, ${get_key});`} ${this.renderer.options.dev &&
b`@validate_each_keys(#ctx, ${this.vars.each_block_value}, ${this.vars.get_each_context}, ${get_key});`}
for (let #i = 0; #i < ${data_length}; #i += 1) { for (let #i = 0; #i < ${data_length}; #i += 1) {
let child_ctx = ${this.vars.get_each_context}(#ctx, ${this.vars.each_block_value}, #i); let child_ctx = ${this.vars.get_each_context}(#ctx, ${
this.vars.each_block_value
}, #i);
let key = ${get_key}(child_ctx); let key = ${get_key}(child_ctx);
${lookup}.set(key, ${iterations}[#i] = ${create_each_block}(key, child_ctx)); ${lookup}.set(key, ${iterations}[#i] = ${create_each_block}(key, child_ctx));
} }

@ -79,6 +79,17 @@ export function set_data_dev(text, data) {
text.data = data; text.data = data;
} }
export function validate_each_argument(arg) {
if (arg instanceof Set || arg instanceof Map) {
throw new Error(`Svelte does not allow Sets or Maps in an {#each} block. Use [...arg.values()] instead.`);
}
if (arg.length === undefined) {
throw new Error(
`Svelte needs an array-like value for the {#each} block. You can spread your iterable into an array instead, e.g. [...iterable]`
);
}
}
type Props = Record<string, any>; type Props = Record<string, any>;
export interface SvelteComponentDev { export interface SvelteComponentDev {

@ -0,0 +1,6 @@
export default {
compileOptions: {
dev: true
},
error: `Svelte does not allow Sets or Maps in an {#each} block. Use [...arg.values()] instead.`
};

@ -0,0 +1,7 @@
<script>
const foo = new Set([1,2,3])
</script>
{#each foo as item}
<div>{item}</div>
{/each}

@ -0,0 +1,6 @@
export default {
compileOptions: {
dev: true
},
error: `Svelte needs an array-like value for the {#each} block. You can spread your iterable into an array instead, e.g. [...iterable]`
};

@ -0,0 +1,7 @@
<script>
const foo = new Set([1,2,3])
</script>
{#each foo.values() as item}
<div>{item}</div>
{/each}
Loading…
Cancel
Save