Skip to content

implement Dev mode validation of {#each} block argument - disallow Sets, Maps, nonArrayLikes #4419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 18, 2020

Conversation

swyxio
Copy link
Contributor

@swyxio swyxio commented Feb 15, 2020

closes #4408

Implemented two tests:

dev-warning-each-block-require-arraylike:

<script>
	const foo = new Set([1,2,3])
</script>

{#each foo.values() as item}
	<div>{item}</div>
{/each}

gets you the error: Svelte needs an array-like value for the {#each} block. You can spread your iterable into an array instead, e.g. [...iterable]

dev-warning-each-block-no-sets-maps:

<script>
	const foo = new Set([1,2,3])
</script>

{#each foo as item}
	<div>{item}</div>
{/each}

gets you the error: Svelte does not allow Sets or Maps in an {#each} block. Use [...arg.values()] instead.

image

I'm not sure what other info to provide, happy to take input on what the wording of the error should be

@swyxio swyxio changed the title implement Dev mode validation of {#each} block argument implement Dev mode validation of {#each} block argument - disallow Sets, Maps, nonArrayLikes Feb 15, 2020
@swyxio
Copy link
Contributor Author

swyxio commented Feb 15, 2020

I, uh, also blogged about it in case you want this content somewhere

@swyxio
Copy link
Contributor Author

swyxio commented Feb 16, 2020

I should also note that i went for two messages instead of one message because i wanted to make it abundantly clear that Svelte has considered allowing the {#each} block for sets, maps, and nonarraylike iterables and has rejected the idea to the point of specifically building in dev errors to say as much. the two messages/tests capture the two main ways in which Svelte devs will shoot themselves in the foot before finding out about this.

Copy link
Member

@antony antony left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this. At some point it might be nice to provide support for Sets and Maps in an {#each}, as it immediately seems like something that would be supported. This closes the gap until this functionality can be looked at.

- handle falsy {#each} arg
- only mention spread if this is an iterable
@Conduitry Conduitry merged commit 3fbafe3 into sveltejs:master Feb 18, 2020
@swyxio swyxio deleted the swyx/4408/validateEachBlockArgs branch February 18, 2020 15:45
jesseskinner pushed a commit to jesseskinner/svelte that referenced this pull request Feb 27, 2020
@ghost ghost mentioned this pull request Mar 2, 2020
taylorzane pushed a commit to taylorzane/svelte that referenced this pull request Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dev mode validation of {#each} block argument
4 participants