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 no-unnecessary-state-wrap rule #1062

Merged
merged 3 commits into from
Mar 16, 2025
Merged

feat: add no-unnecessary-state-wrap rule #1062

merged 3 commits into from
Mar 16, 2025

Conversation

baseballyama
Copy link
Member

close: #1052

Copy link

changeset-bot bot commented Feb 9, 2025

🦋 Changeset detected

Latest commit: fe121fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Feb 9, 2025

Try the Instant Preview in Online Playground

ESLint Online Playground

Install the Instant Preview to Your Local

npm i https://pkg.pr.new/eslint-plugin-svelte@1062

Published Instant Preview Packages:

View Commit

@baseballyama baseballyama force-pushed the feat/1052 branch 10 times, most recently from 49448ae to e4bbfe5 Compare February 9, 2025 09:41

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@marekdedic
Copy link
Contributor

Hi,
sorry if this is unfinished and you're planning to add this, but atm, this ignores #1052 (comment)

@baseballyama
Copy link
Member Author

Since the clear function can be used, $state is usually unnecessary. If there is a reason to use $state(new SvelteMap()), this rule can simply be disabled. ESLint is fully configurable.

@baseballyama baseballyama marked this pull request as ready for review February 9, 2025 12:13
@marekdedic
Copy link
Contributor

I still have to disagree - the rule now has a clear false positive that can be detected by its implementation, but just isn't.
And I'm sorry, but yeah, you can disable any rule for any line, but if the FP can be detected by the rule, it should be. Only when it's not possible (like what we're discussing in #910 ) should users be forced to manually disable rules...

@baseballyama
Copy link
Member Author

Can you provide an example of fp?

@marekdedic
Copy link
Contributor

marekdedic commented Feb 10, 2025

Of course, see #1052 (comment) for a simple example or here for a real-world example (this one is in $derived, but the same semantics apply)

@baseballyama
Copy link
Member Author

Then, let’s add an option to not report an error when a reassignment exists.

@marekdedic
Copy link
Contributor

Thanks :) I don't even think it needs to be an option - if a $state(SvelteMap) is reassigned, the $state is always necessary...

@baseballyama
Copy link
Member Author

I don’t think so. I believe it might simply be a mistake. At least in my project, I want to consistently treat it as an error. For example, in the following case, it can clearly use “clear”.

  let map = $state(new SvelteMap());
  map = new SvelteMap();

@marekdedic
Copy link
Contributor

marekdedic commented Feb 11, 2025

Hmm, you are absolutely right that the code snippet is incorrect, thanks, I din't realize that!

I am thinking if this isn't a separate issue, though - the anti-pattern here is IMO assigning a new SvelteMap() to an object which is a SvelteMap instead of using .clear(). And that would also be an issue with the regular Map, wouldn't it? So maybe you found a more general issue here that assigning new X() to a variable of type X is incorrect and can be replaced with X.clear() for X being any of SvelteMap, SvelteSet, Map, Set (I checked MDN and that's all I could find)

Now if that were a separate rule it would nicely dovetail with this one even if this rule ignored all re-assigned variables, the other rule would replace the re-assignment with .clear() and this rule would then remove the $state.

I feel I am rambling a little bit - do I make any sense to you? :D

@baseballyama
Copy link
Member Author

In any case, for those who don’t want this rule enabled, they can simply disable it. In most cases, $state(new SvelteMap()) doesn’t really make sense, so I think having it enabled by default isn’t a bad idea.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@baseballyama
Copy link
Member Author

I added allowReassign option.

@baseballyama baseballyama requested a review from Copilot February 27, 2025 02:06

Choose a reason for hiding this comment

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

PR Overview

This PR introduces a new ESLint rule, svelte/no-unnecessary-state-wrap, to disallow redundant $state wrapping for reactive classes in Svelte. Key changes include:

  • Implementation of the new rule in the plugin with appropriate configurations.
  • Comprehensive test fixtures for basic errors, additional custom classes, reassign scenarios, and import alias handling.
  • Updates to documentation, configuration files, and the rule types file to integrate the new rule.

Reviewed Changes

File Description
docs/rules/no-unnecessary-state-wrap.md Adds documentation for the new rule.
packages/eslint-plugin-svelte/src/rules/no-unnecessary-state-wrap.ts Implements the rule logic with suggestions for removal of unnecessary wrapping.
packages/eslint-plugin-svelte/tests/fixtures/rules/no-unnecessary-state-wrap/* Provides test fixtures covering various error scenarios.
.changeset/popular-needles-remain.md Updates the changeset with the feature.
packages/eslint-plugin-svelte/src/rule-types.ts Extends the rule types for the new rule.
packages/eslint-plugin-svelte/src/configs/flat/recommended.ts, README.md, docs/rules.md, packages/eslint-plugin-svelte/src/utils/rules.ts Updates configuration and documentation to include the new rule.

Copilot reviewed 26 out of 26 changed files in this pull request and generated no comments.

Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks!

@ota-meshi ota-meshi merged commit a4d3b29 into main Mar 16, 2025
17 checks passed
@ota-meshi ota-meshi deleted the feat/1052 branch March 16, 2025 23:59
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.

disallow $state(new SvelteSet) and similar
3 participants