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

Invalidating functions cannot be declared as @const (continuation) #9179

Open
molily opened this issue Sep 5, 2023 · 0 comments
Open

Invalidating functions cannot be declared as @const (continuation) #9179

molily opened this issue Sep 5, 2023 · 0 comments

Comments

@molily
Copy link
Contributor

molily commented Sep 5, 2023

Describe the bug

I believe this is a continuation of #7843.

The problem seems to be fixed for the original case but it still appears in 4.2.0 when there is a function that does not use the loop variable.

Thanks a lot for looking into this!

Reproduction

https://svelte.dev/repl/d573469069254061b887fb45cc3f0d18?version=4.2.0

<script>
  const items = [1, 2, 3, 4, 5, 6];

  /** @type {number | undefined} */
  let highlightedItem = undefined;
</script>

{#each items as item}
  {@const highlight = () => {
    highlightedItem = item;
  }}
  {@const unhighlight = () => {
    highlightedItem = undefined;
  }}
  <button
    on:pointermove={highlight}
    on:pointerleave={unhighlight}
  >
    {item}
  </button>
{/each}

<p>
  Highlighted: {#if highlightedItem !== undefined}{highlightedItem}{:else}n/a{/if}
</p>

Note: I understand that unhighlight probably should not be in the loop scope in this simple example. Nonetheless, this should not throw a runtime error in my opinion.

Logs

App.svelte:20 Uncaught ReferenceError: $$invalidate is not defined
    at HTMLButtonElement.constants_1 (eval at handle_message (about:srcdoc:21:8), <anonymous>:757:4)
    at HTMLButtonElement.eval (eval at handle_message (about:srcdoc:21:8), <anonymous>:796:73)

here:

const constants_1 = () => {
  $$invalidate(0, /*highlightedItem*/ child_ctx[0] = undefined);
};

For comparison, the first function is hoisted to instance and passed through the ctx:

const func = item => {
  $$invalidate(0, highlightedItem = item);
};

return [highlightedItem, items, func];

Call site:

const constants_1 = function func() {
  return /*func*/ ctx[2](/*item*/ child_ctx[3]);
};

The second (failing) function does not use the loop item variable. Probably that is why it is not treated the same?

System Info

REPL / Chrome & Firefox

Severity

annoyance

@molily molily changed the title Invalidating functions cannot be declared as @const () Invalidating functions cannot be declared as @const (continutation) Sep 5, 2023
@molily molily changed the title Invalidating functions cannot be declared as @const (continutation) Invalidating functions cannot be declared as @const (continuation) Sep 5, 2023
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

No branches or pull requests

1 participant