Skip to content

Compiler throw error when IIFE used as key in each clause #3436

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

Closed
davidaq opened this issue Aug 20, 2019 · 2 comments
Closed

Compiler throw error when IIFE used as key in each clause #3436

davidaq opened this issue Aug 20, 2019 · 2 comments
Labels

Comments

@davidaq
Copy link
Contributor

davidaq commented Aug 20, 2019

Describe the bug
Compiler throw an error when using a IIFE as the key in {#each}.

Svelte version: 3.7.1

Logs

TypeError: Cannot set property 'maintain_context' of undefined
    at Expression.render (/orbit/node_modules/svelte/compiler.js:21143:37)
    at EachBlockWrapper.render_keyed (/orbit/node_modules/svelte/compiler.js:14591:23)
    at EachBlockWrapper.render (/orbit/node_modules/svelte/compiler.js:14510:19)
    at fragment.nodes.forEach (/orbit/node_modules/svelte/compiler.js:15757:24)
    at Array.forEach (<anonymous>)
    at ElementWrapper.render (/orbit/node_modules/svelte/compiler.js:15756:34)
    at FragmentWrapper.render (/orbit/node_modules/svelte/compiler.js:17432:28)
    at new Renderer (/orbit/node_modules/svelte/compiler.js:17463:24)
    at dom (/orbit/node_modules/svelte/compiler.js:17662:23)
    at compile (/orbit/node_modules/svelte/compiler.js:23840:16)

To Reproduce

Reproducible in REPL:

<script>
const arr = [1, 2, 3]
</script>

<div>
	{#each arr as item ((() => item)())}
		{item}
	{/each}
</div>

This error can be worked around by passing arguments into the IFFE:

<script>
const arr = [1, 2, 3]
</script>

<div>
	{#each arr as item (((item) => item)(item))}
		{item}
	{/each}
</div>

Expected behavior
Compile as is or just disable IIFE usage as key.

Severity
Minor, not a case usual developers would encounter.

@davidaq
Copy link
Contributor Author

davidaq commented Aug 20, 2019

Looking in to the code I believe block is missing here:

		block.builders.init.add_block(deindent`
			const ${get_key} = ctx => ${
			// @ts-ignore todo: probably error
			this.node.key.render()};   // <--------- .render(block) ?
			for (var #i = 0; #i < ${this.vars.each_block_value}.${length}; #i += 1) {

Inside file src/compiler/compile/render_dom/wrappers/EachBlock.ts:336. The compile outcome seems valid to me after doing this fix.

I haven't read the whole code, so I'm not sure what block is for, and thus not confident enough to make a patch myself.

@Conduitry Conduitry added the bug label Aug 22, 2019
@Conduitry
Copy link
Member

It looks like this will be fixed by #3539.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants