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

Object property access in callbacks within @const statements are being treated as separate variables #7326

Closed
AdityaHegde opened this issue Mar 1, 2022 · 1 comment

Comments

@AdityaHegde
Copy link

AdityaHegde commented Mar 1, 2022

Describe the bug

Adding object property access, obj.key is I believe being treated as obj and key being separate variables.

Consider this code,

<script>
  const array1 = [{id: 0, name: "a"}, {id: 1, name: "b"}];
  const array2 = [{id: 1, value: "vb"}, {id: 0, value: "va"}];
</script>

<div>
  {#each array1 as {id, name} (id)}
    {@const item2 = array2.find(e2 => e2.id === id)}
    <div>{name}:{item2.value}</div>
  {/each}
</div>

I get Cannot read properties of undefined (reading '2') error at e2.id. Svelte attempts to access id in the current context.
This can be seen in the generated code,

function get_each_context(ctx, list, i) {
  const child_ctx = ctx.slice();
  child_ctx[2] = list[i].id;
  child_ctx[3] = list[i].name;
  const constants_0 = /*array2*/ child_ctx[1].find(e2 => e2./*id*/ child_ctx[2] === /*id*/ child_ctx[2]);
  child_ctx[4] = constants_0;
  return child_ctx;
}

Changing e2.id to e2["id"] fixes this.

Edit: Another observation, this only happens when the Identifier (id in this case) is present in the @each statement either in object destrcutring or as a key.

Reproduction

REPL

Logs

No response

System Info

System:
    OS: macOS 12.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 261.77 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    npm: 8.1.2 - /usr/local/bin/npm
  Browsers:
    Chrome: 98.0.4758.109
    Safari: 15.0
  npmPackages:
    svelte: ^3.46.4 => 3.46.4


### Severity

annoyance
@baseballyama
Copy link
Member

Duplicate of #7206

@bluwy bluwy closed this as completed Mar 3, 2022
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 a pull request may close this issue.

3 participants