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

Block using state in event handlers when state is set via binding on same element as event handler #921

Open
benglewis opened this issue Nov 21, 2024 · 0 comments
Labels
enhancement New feature or request new rule

Comments

@benglewis
Copy link

Motivation

In some cases, using bind:value or bind:checked or similar bindings of state in Svelte 5.0 alongside event handlers that access the same state can result in unexpected behavior (e.g. seeing old values in the event handler and not the latest from the binding).
This rule should help prevent code like this.

Description

The rule should detect any state that is bound to an element state, e.g. bind:this, bind:checked, bind:value, etc. and also accessed inside an event listening onchange, onkeyup, etc. explaining that such a value may not be up to date.

Examples

<script>
  let state = ''
</script>

<!-- ✓ GOOD -->
<input onchange={(e) => { state = e.target.value; console.log(state) }} ></input>

<!-- ✗ BAD -->
<input bind:value={state} onchange={(e) => { console.log(state) }} ></input>

Additional comments

I am happy to consider implementing this rule myself.

@benglewis benglewis added enhancement New feature or request new rule labels Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

No branches or pull requests

1 participant