Skip to content

noSvelteAddEventListener (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noSvelteAddEventListener": "error"
}
}
}
}

Disallow addEventListener in Svelte files.

Svelte relies on event delegation for performance and to guarantee the order in which event handlers run. Listeners attached with addEventListener bypass this mechanism, so they may run before or after delegated handlers in an unexpected order. The on function from svelte/events attaches listeners that cooperate with event delegation, and it returns a function that removes the listener.

The rule provides a fix when on is already imported from svelte/events.

<script>
window.addEventListener("resize", handler);
</script>
code-block.svelte:2:1 lint/nursery/noSvelteAddEventListener ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected use of addEventListener.

1 │ <script>
> 2 │ window.addEventListener("resize", handler);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ </script>
4 │

ℹ Listeners attached with addEventListener bypass Svelte's event delegation, so they may not run in the expected order relative to other event handlers.

ℹ Use the on function from svelte/events instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script>
addEventListener("message", handler);
</script>
code-block.svelte:2:1 lint/nursery/noSvelteAddEventListener ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Unexpected use of addEventListener.

1 │ <script>
> 2 │ addEventListener("message", handler);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ </script>
4 │

ℹ Listeners attached with addEventListener bypass Svelte's event delegation, so they may not run in the expected order relative to other event handlers.

ℹ Use the on function from svelte/events instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

<script>
import { on } from "svelte/events";
on(window, "resize", handler);
</script>