Skip to content

feat: added the prefer-svelte-reactivity rule #1151

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

marekdedic
Copy link
Contributor

@marekdedic marekdedic commented Mar 24, 2025

Copy link

changeset-bot bot commented Mar 24, 2025

🦋 Changeset detected

Latest commit: dcf3e34

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

@marekdedic marekdedic force-pushed the prefer-svelte-reactivity branch from d061a99 to 6999c90 Compare March 24, 2025 09:03
Copy link
Contributor

github-actions bot commented Mar 24, 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@dcf3e34

Published Instant Preview Packages:

View Commit

@marekdedic marekdedic force-pushed the prefer-svelte-reactivity branch 2 times, most recently from 5cede00 to f44a65c Compare March 24, 2025 09:25
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 think we need to add tests for .svelte.js files also.

Comment on lines +1 to +5
<script>
const variable = new Date(8.64e15);
</script>

{variable}
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think this needs to be reported. If there’s no reassignment, it shouldn’t need to be reactive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not about reassignment, it's about internal changes. I'd prefer to be this overly-broad. The alternative is tracking calls to every method that modifies the object (I count 16 on Date alone) or writing to any property.

On top of that, once something is exported, it should be reported as well (mostly relevant for .svelte.ts files)

I am not sure it's worth it, what do you think?

Copy link
Member

@baseballyama baseballyama Mar 24, 2025

Choose a reason for hiding this comment

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

What I’m saying is the same thing.

// It should be SvelteMap because there is internal changes.
const foo = new Map();
foo.set("a", 0);

// It should not be reported because there is no internal changes.
const bar = new Map();

On top of that, once something is exported, it should be reported as well (mostly relevant for .svelte.ts files)

If we think of svelte.js as “reactive files,” it should be fine to report aggressively.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I didn't resolve this... Hmmm...

Copy link
Member

Choose a reason for hiding this comment

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

How about tracking instances and checking mutable function calls?
For example, tracking new Set() variables and checking add() calls.

For example, I might use:

const ALLOWED_VALUES = new Set(["a", "b"]); // Not reported
function isAllowedValue(v) {
  return ALLOWED_VALUES.has(v);
}

const usedValues ​​= new Set();
function process(v) {
  if (usedValues.has(v)) return;
  usedValues.add(v); // Reported
  // ...
}

@marekdedic
Copy link
Contributor Author

I think we need to add tests for .svelte.js files also.

Yes, I wanted to ask about whether that's something we're set up to do, I couldn't find any such tests on the repo...

@marekdedic
Copy link
Contributor Author

@ota-meshi Can you please take a look at the test file? I tried the solution from eslint-community/eslint-utils#249 (comment), but it didn't work for me :/

@ota-meshi
Copy link
Member

@marekdedic What have you tried? Have you added URL here?

@marekdedic marekdedic force-pushed the prefer-svelte-reactivity branch from 7603712 to d1230de Compare May 3, 2025 10:11
@marekdedic
Copy link
Contributor Author

@marekdedic What have you tried? Have you added URL here?

That's exactly what I tried, but it didn't work :(

@ota-meshi
Copy link
Member

ota-meshi commented May 8, 2025

Is it url01-input.svelte that you're saying didn't work? I've tried adding URL: 'readonly', URLSearchParams: 'readonly' to globals in your repository and it seems to work fine.

image

@marekdedic marekdedic force-pushed the prefer-svelte-reactivity branch 5 times, most recently from 6804432 to 18dad7e Compare May 8, 2025 14:05
@marekdedic marekdedic force-pushed the prefer-svelte-reactivity branch from 18dad7e to dcf3e34 Compare May 8, 2025 14:09
@marekdedic
Copy link
Contributor Author

Ok, I was adding it in a different place and the value got overriden, thanks :)

@marekdedic marekdedic marked this pull request as ready for review May 8, 2025 14:13
@marekdedic marekdedic requested a review from baseballyama May 8, 2025 14:13
Comment on lines +222 to +223
**Visual Studio Code**
Install [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).
Copy link
Member

Choose a reason for hiding this comment

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

Could you please revert the unrelated change? Or add \ to the end of the line?

Suggested change
**Visual Studio Code**
Install [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).
**Visual Studio Code**\
Install [dbaeumer.vscode-eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint).\

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was done by pnpm run update 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Hmm. Maybe it's because .editorconfig was added recently? I don't use the EditorConfig plugin so I'm not sure 😓

Copy link
Member

@ota-meshi ota-meshi May 9, 2025

Choose a reason for hiding this comment

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

I've changed it to use the \ notation, so could you rebase it? #1215

Comment on lines +1 to +5
<script>
const variable = new Date(8.64e15);
</script>

{variable}
Copy link
Member

Choose a reason for hiding this comment

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

How about tracking instances and checking mutable function calls?
For example, tracking new Set() variables and checking add() calls.

For example, I might use:

const ALLOWED_VALUES = new Set(["a", "b"]); // Not reported
function isAllowedValue(v) {
  return ALLOWED_VALUES.has(v);
}

const usedValues ​​= new Set();
function process(v) {
  if (usedValues.has(v)) return;
  usedValues.add(v); // Reported
  // ...
}

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.

Add rule prefer-svelte-reactivity
3 participants