Skip to content
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

{#each} block destructuring can't have default values refer to destructured params #5066

Closed
tivac opened this issue Jun 24, 2020 · 1 comment · Fixed by #5986
Closed

{#each} block destructuring can't have default values refer to destructured params #5066

tivac opened this issue Jun 24, 2020 · 1 comment · Fixed by #5986
Labels

Comments

@tivac
Copy link
Contributor

tivac commented Jun 24, 2020

Describe the bug

There's a useful pattern you can use while destructuring in normal JS, which is that within the destructure you can reference previously-extracted properties.

Live Example

const o = { one : 1 };

// Here's where the exciting stuff happens
//           v-----------v
const { one, two = one + 1 } = o;

console.log(one, two);

I wanted to use (abuse?) this functionality to avoid having to create an entire array or modify an array that I was passed, because allocating a whole array for something that can be easily calculated on a per-element basis while iterating didn't seem worth it.

To Reproduce
https://svelte.dev/repl/0ee7227e1b45465b9b47d7a5ae2d1252?version=3.23.2

Here's the problematic generated code:

function get_each_context(ctx, list, i) {
	const child_ctx = ctx.slice();
	child_ctx[1] = list[i].one;
	// This reference to one isn't rewritten to be child_ctx[1], so one is undefined
	//                                                       v---------v
	child_ctx[2] = list[i].two !== undefined ? list[i].two : add(one, 1);
	return child_ctx;
}

Expected behavior
1 - 2 is logged

Information about your Svelte project:

  • Firefox latest, but it's in the generated code so happens everywhere
  • Windows 10
  • [email protected]
  • Rollup, can see in REPL

Severity
It mostly means that if you need to look up data to make decisions inside an {#each} you either need to modify the array elements or create a second, shadow array based on the elements containing the extra data. This is all doable but I'd much prefer not to if possible. Since templates can't declare new variables I haven't been able to come up with an alternative that lets it be handled entirely in template code.

@Conduitry
Copy link
Member

This is fixed in 3.33.0 - https://svelte.dev/repl/0ee7227e1b45465b9b47d7a5ae2d1252?version=3.33.0

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