update validation logic and messages

- handle falsy {#each} arg
- only mention spread if this is an iterable
pull/4419/head
Conduitry 6 years ago
parent 73378c0398
commit fb75588163

@ -80,15 +80,12 @@ export function set_data_dev(text, data) {
} }
export function validate_each_argument(arg) { export function validate_each_argument(arg) {
if (arg instanceof Set || arg instanceof Map) { if (!arg || !('length' in arg)) {
throw new Error( let msg = '{#each} only iterates over array-like objects.';
`Svelte does not allow Sets or Maps in an {#each} block. Use [...arg.values()] or [...arg.entries()] instead.` if (typeof Symbol === 'function' && arg && Symbol.iterator in arg) {
); msg += ' You can use a spread to convert this iterable into an array.';
} }
if (arg.length === undefined) { throw new Error(msg);
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]`
);
} }
} }

Loading…
Cancel
Save