From 138213ca3c1ca91998461e961933d630aeb45b15 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sun, 23 Feb 2020 16:35:25 -0500 Subject: [PATCH] fix dev mode each block validation when using strings (#4451) --- CHANGELOG.md | 4 ++++ src/runtime/internal/dev.ts | 2 +- test/runtime/samples/each-block-string/_config.js | 10 ++++++++++ test/runtime/samples/each-block-string/main.svelte | 3 +++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/runtime/samples/each-block-string/_config.js create mode 100644 test/runtime/samples/each-block-string/main.svelte diff --git a/CHANGELOG.md b/CHANGELOG.md index f81b198154..3f4460cde4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte changelog +## Unreleased + +* Fix dev mode validation of `{#each}` blocks using strings ([#4450](https://github.com/sveltejs/svelte/issues/4450)) + ## 3.19.0 * Fix indirect bindings involving elements with spreads ([#3680](https://github.com/sveltejs/svelte/issues/3680)) diff --git a/src/runtime/internal/dev.ts b/src/runtime/internal/dev.ts index 2d60ffb985..8aefc88ba7 100644 --- a/src/runtime/internal/dev.ts +++ b/src/runtime/internal/dev.ts @@ -80,7 +80,7 @@ export function set_data_dev(text, data) { } export function validate_each_argument(arg) { - if (!arg || !('length' in arg)) { + if (typeof arg !== 'string' && !(arg && typeof arg === 'object' && 'length' in arg)) { let msg = '{#each} only iterates over array-like objects.'; if (typeof Symbol === 'function' && arg && Symbol.iterator in arg) { msg += ' You can use a spread to convert this iterable into an array.'; diff --git a/test/runtime/samples/each-block-string/_config.js b/test/runtime/samples/each-block-string/_config.js new file mode 100644 index 0000000000..7366c964eb --- /dev/null +++ b/test/runtime/samples/each-block-string/_config.js @@ -0,0 +1,10 @@ +export default { + compileOptions: { + dev: true + }, + html: ` +
f
+
o
+
o
+ ` +}; diff --git a/test/runtime/samples/each-block-string/main.svelte b/test/runtime/samples/each-block-string/main.svelte new file mode 100644 index 0000000000..ae60f0f6b3 --- /dev/null +++ b/test/runtime/samples/each-block-string/main.svelte @@ -0,0 +1,3 @@ +{#each 'foo' as c} +
{c}
+{/each}