-
-
Notifications
You must be signed in to change notification settings - Fork 48
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: added the prefer-svelte-reactivity
rule
#1151
Draft
marekdedic
wants to merge
4
commits into
sveltejs:main
Choose a base branch
from
marekdedic:prefer-svelte-reactivity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5ca61dd
chore(prefer-svelte-reactivity): added tests
marekdedic 7bc67b7
feat(prefer-svelte-reactivity): added rule implementation
marekdedic f44a65c
docs(prefer-svelte-reactivity): added rule docs
marekdedic 7603712
Added URL and URLSearchParams to globals
marekdedic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': minor | ||
--- | ||
|
||
feat: added the `prefer-svelte-reactivity` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/prefer-svelte-reactivity' | ||
description: 'disallow using built-in classes where a reactive alternative is provided by svelte/reactivity' | ||
--- | ||
|
||
# svelte/prefer-svelte-reactivity | ||
|
||
> disallow using built-in classes where a reactive alternative is provided by svelte/reactivity | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :gear: This rule is included in `"plugin:svelte/recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
The built-in `Date`, `Map`, `Set`, `URL` and `URLSearchParams` classes are often used in frontend code, however, their properties and methods are not reactive. Because of that, Svelte provides reactive versions of these 5 builtins as part of the "svelte/reactivity" package. This rule reports usage of the built-in versions in Svelte code. | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/prefer-svelte-reactivity: "error" */ | ||
|
||
import { | ||
SvelteDate, | ||
SvelteMap, | ||
SvelteSet, | ||
SvelteURL, | ||
SvelteURLSearchParams | ||
} from 'svelte/reactivity'; | ||
|
||
/* ✓ GOOD */ | ||
|
||
const a = new SvelteDate(8.64e15); | ||
const b = new SvelteMap([ | ||
[1, 'one'], | ||
[2, 'two'] | ||
]); | ||
const c = new SvelteSet([1, 2, 1, 3, 3]); | ||
const d = new SvelteURL('https://svelte.dev/'); | ||
const e = new SvelteURLSearchParams('foo=1&bar=2'); | ||
|
||
/* ✗ BAD */ | ||
|
||
const f = new Date(8.64e15); | ||
const g = new Map([ | ||
[1, 'one'], | ||
[2, 'two'] | ||
]); | ||
const h = new Set([1, 2, 1, 3, 3]); | ||
const i = new URL('https://svelte.dev/'); | ||
const j = new URLSearchParams('foo=1&bar=2'); | ||
</script> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :books: Further Reading | ||
|
||
- [svelte/reactivity documentation](https://svelte.dev/docs/svelte/svelte-reactivity) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/prefer-svelte-reactivity.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/prefer-svelte-reactivity.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin-svelte/src/rules/prefer-svelte-reactivity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ReferenceTracker } from '@eslint-community/eslint-utils'; | ||
import { getSourceCode } from '../utils/compat.js'; | ||
import { createRule } from '../utils/index.js'; | ||
|
||
export default createRule('prefer-svelte-reactivity', { | ||
meta: { | ||
docs: { | ||
description: | ||
'disallow using built-in classes where a reactive alternative is provided by svelte/reactivity', | ||
category: 'Possible Errors', | ||
recommended: true | ||
}, | ||
schema: [], | ||
messages: { | ||
dateUsed: 'Found a usage of the built-in Date class. Use a SvelteDate instead.', | ||
mapUsed: 'Found a usage of the built-in Map class. Use a SvelteMap instead.', | ||
setUsed: 'Found a usage of the built-in Set class. Use a SvelteSet instead.', | ||
urlUsed: 'Found a usage of the built-in URL class. Use a SvelteURL instead.', | ||
urlSearchParamsUsed: | ||
'Found a usage of the built-in URLSearchParams class. Use a SvelteURLSearchParams instead.' | ||
}, | ||
type: 'problem', // 'problem', or 'layout', | ||
conditions: [ | ||
{ | ||
svelteVersions: ['5'], | ||
svelteFileTypes: ['.svelte', '.svelte.[js|ts]'] | ||
} | ||
] | ||
}, | ||
create(context) { | ||
return { | ||
Program() { | ||
const referenceTracker = new ReferenceTracker( | ||
getSourceCode(context).scopeManager.globalScope! | ||
); | ||
for (const { node, path } of referenceTracker.iterateGlobalReferences({ | ||
Date: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
Map: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
Set: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
URL: { | ||
[ReferenceTracker.CALL]: true, | ||
[ReferenceTracker.CONSTRUCT]: true, | ||
[ReferenceTracker.READ]: true | ||
}, | ||
URLSearchParams: { | ||
[ReferenceTracker.CALL]: true, | ||
[ReferenceTracker.CONSTRUCT]: true, | ||
[ReferenceTracker.READ]: true | ||
} | ||
})) { | ||
const typeToMessageId: Record<string, string> = { | ||
Date: 'dateUsed', | ||
Map: 'mapUsed', | ||
Set: 'setUsed', | ||
URL: 'urlUsed', | ||
URLSearchParams: 'urlSearchParamsUsed' | ||
}; | ||
context.report({ | ||
messageId: typeToMessageId[path[0]], | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...t-plugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/date01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
const variable = new Date(8.64e15); | ||
</script> | ||
|
||
{variable} | ||
5 changes: 5 additions & 0 deletions
5
...nt-plugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/map01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
const variable = new Map([[1, "one"], [2, "two"]]); | ||
</script> | ||
|
||
{variable} |
5 changes: 5 additions & 0 deletions
5
...nt-plugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/set01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
const variable = new Set([1, 2, 1, 3, 3]); | ||
</script> | ||
|
||
{variable} |
5 changes: 5 additions & 0 deletions
5
...te/tests/fixtures/rules/prefer-svelte-reactivity/invalid/url-search-params01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
const variable = new URLSearchParams("foo=1&bar=2"); | ||
</script> | ||
|
||
{variable} |
5 changes: 5 additions & 0 deletions
5
...nt-plugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/url01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
const variable = new URL("https://svelte.dev/"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...in-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/aliased-date01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteDate as Date } from "svelte/reactivity"; | ||
|
||
const variable = new Date(8.64e15); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...gin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/aliased-map01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteMap as Map } from "svelte/reactivity"; | ||
|
||
const variable = new Map([[1, "one"], [2, "two"]]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...gin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/aliased-set01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteSet as Set } from "svelte/reactivity"; | ||
|
||
const variable = new Set([1, 2, 1, 3, 3]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...ts/fixtures/rules/prefer-svelte-reactivity/valid/aliased-url-search-params01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteURLSearchParams as URLSearchParams } from "svelte/reactivity"; | ||
|
||
const variable = new URLSearchParams("foo=1&bar=2"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...gin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/aliased-url01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteURL as URL } from "svelte/reactivity"; | ||
|
||
const variable = new URL("https://svelte.dev/"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...gin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/svelte-date01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteDate } from "svelte/reactivity"; | ||
|
||
const variable = new SvelteDate(8.64e15); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...ugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/svelte-map01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteMap } from "svelte/reactivity"; | ||
|
||
const variable = new SvelteMap([[1, "one"], [2, "two"]]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...ugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/svelte-set01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteSet } from "svelte/reactivity"; | ||
|
||
const variable = new SvelteSet([1, 2, 1, 3, 3]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...sts/fixtures/rules/prefer-svelte-reactivity/valid/svelte-url-search-params01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteURLSearchParams } from "svelte/reactivity"; | ||
|
||
const variable = new SvelteURLSearchParams("foo=1&bar=2"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...ugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/svelte-url01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { SvelteURL } from "svelte/reactivity"; | ||
|
||
const variable = new SvelteURL("https://svelte.dev/"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/unrelated-date01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { Date } from "package"; | ||
|
||
const variable = new Date(8.64e15); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...n-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/unrelated-map01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { Map } from "package"; | ||
|
||
const variable = new Map([[1, "one"], [2, "two"]]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...n-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/unrelated-set01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { Set } from "package"; | ||
|
||
const variable = new Set([1, 2, 1, 3, 3]); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
.../fixtures/rules/prefer-svelte-reactivity/valid/unrelated-url-search-params01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { URLSearchParams } from "package"; | ||
|
||
const variable = new URLSearchParams("foo=1&bar=2"); | ||
</script> | ||
|
||
{variable} |
7 changes: 7 additions & 0 deletions
7
...n-svelte/tests/fixtures/rules/prefer-svelte-reactivity/valid/unrelated-url01-input.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script> | ||
import { URL } from "package"; | ||
|
||
const variable = new URL("https://svelte.dev/"); | ||
</script> | ||
|
||
{variable} |
16 changes: 16 additions & 0 deletions
16
packages/eslint-plugin-svelte/tests/src/rules/prefer-svelte-reactivity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from '../../utils/eslint-compat.js'; | ||
import rule from '../../../src/rules/prefer-svelte-reactivity.js'; | ||
import { loadTestCases } from '../../utils/utils.js'; | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
ecmaVersion: 2020, | ||
globals: { | ||
URL: "readonly", | ||
URLSearchParams: "readonly", | ||
}, | ||
sourceType: 'module' | ||
} | ||
}); | ||
|
||
tester.run('prefer-svelte-reactivity', rule as any, loadTestCases('prefer-svelte-reactivity')); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
If we think of
svelte.js
as “reactive files,” it should be fine to report aggressively.