Skip to content

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

@molily

Description

@molily

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions