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

feat: add svelte/no-reactive-reassign rule #440

Merged
merged 3 commits into from Apr 26, 2023
Merged

Conversation

ota-meshi
Copy link
Member

@ota-meshi ota-meshi commented Apr 22, 2023

This PR adds svelte/no-reactive-reassign rule.

svelte/no-reactive-reassign rule that disallow reassigning reactive values.

e.g.

<script>
  /* eslint svelte/no-reactive-reassign: "error" */
  let value = 0
  $: reactiveValue = value * 2
  function handleClick() {
    /* ✓ GOOD */
    value++
    /* ✗ BAD */
    reactiveValue = value * 3
    reactiveValue++
  }
</script>
<!-- ✓ GOOD -->
<input type="number" bind:value />
<!-- ✗ BAD -->
<input type="number" bind:value={reactiveValue} />

@ota-meshi ota-meshi marked this pull request as draft April 22, 2023 07:30
@ota-meshi ota-meshi marked this pull request as ready for review April 23, 2023 10:19
Copy link
Member

@baseballyama baseballyama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the below doesn't detect the ESLint error, but I think it's too difficult.😅

<script>
  /* eslint svelte/no-reactive-reassign: "error" */
  let object = { value: 0 }
  $: reactiveValue = { value: object.value * 2, foo: { bar: object.value * 2 } }
  function handleClick() {
    const a = reactiveValue;
    // It should be error, but it's too difficult to check...
    delete a["foo"];
  }
</script>

@ota-meshi
Copy link
Member Author

Oh, you're right. However, it is difficult to report it accurately as you say, so I would like to consider it if there is a request from the user in the future.

@ota-meshi ota-meshi merged commit ed68b20 into main Apr 26, 2023
10 checks passed
@ota-meshi ota-meshi deleted the no-reactive-reassign branch April 26, 2023 00:47
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

Successfully merging this pull request may close these issues.

None yet

2 participants