sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.75k stars 4.13k forks source link

impure expression in each block causes infinite loop in runtime #6023

Open milahu opened 3 years ago

milahu commented 3 years ago

Describe the bug impure expression in each block causes infinite loop in runtime

Severity low priority

To Reproduce repl - uncomment //state[key] = val; to crash the browser tab

<script>
  const data = { k1: 'v1' };
  const state = {};
</script>
{#each
  ['k1'].map(key => {
    const val = data[key];
    console.log('step'); // should be called only once

    //state[key] = val;
    // this global assignment makes the each-expression impure
    // and causes an infinite loop in the svelte runtime
    // 'step' is printed over and over in the browser console

    return [key, val];
  }) as [key, val]
}
  <div>{key}: {val}</div>
{/each}

Expected behavior probably this impurity should be detected by the svelte compiler as fatal error at least i cannot think of a use case where this should work

Possible Solutions

1. compile time

parse the expression and look for assignments to non-local variables (sample code)

this would at least detect the simple case shown above. its harder to detect impure functions, that write to the global state

2. runtime

in the dev mode runtime, detect the infinite loop, stop it, and help to analyze (start devtools debugger?)

Related

Env Svelte version 3.32.3

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.