-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
marekdedic
wants to merge
3
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
246 changes: 246 additions & 0 deletions
246
packages/eslint-plugin-svelte/src/rules/prefer-svelte-reactivity.ts
This file contains hidden or 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,246 @@ | ||
import { ReferenceTracker } from '@eslint-community/eslint-utils'; | ||
import { createRule } from '../utils/index.js'; | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
|
||
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: { | ||
mutableDateUsed: | ||
'Found a mutable instance of the built-in Date class. Use SvelteDate instead.', | ||
mutableMapUsed: 'Found a mutable instance of the built-in Map class. Use SvelteMap instead.', | ||
mutableSetUsed: 'Found a mutable instance of the built-in Set class. Use SvelteSet instead.', | ||
mutableURLUsed: 'Found a mutable instance of the built-in URL class. Use SvelteURL instead.', | ||
mutableURLSearchParamsUsed: | ||
'Found a mutable instance of the built-in URLSearchParams class. Use SvelteURLSearchParams instead.' | ||
}, | ||
type: 'problem', // 'problem', or 'layout', | ||
conditions: [ | ||
{ | ||
svelteVersions: ['5'], | ||
svelteFileTypes: ['.svelte', '.svelte.[js|ts]'] | ||
} | ||
] | ||
}, | ||
create(context) { | ||
return { | ||
Program() { | ||
const referenceTracker = new ReferenceTracker(context.sourceCode.scopeManager.globalScope!); | ||
for (const { node, path } of referenceTracker.iterateGlobalReferences({ | ||
Date: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
Map: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
Set: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
URL: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
}, | ||
URLSearchParams: { | ||
[ReferenceTracker.CONSTRUCT]: true | ||
} | ||
})) { | ||
if (path[0] === 'Date' && isDateMutable(referenceTracker, node as TSESTree.Expression)) { | ||
context.report({ | ||
messageId: 'mutableDateUsed', | ||
node | ||
}); | ||
} | ||
if (path[0] === 'Map' && isMapMutable(referenceTracker, node as TSESTree.Expression)) { | ||
context.report({ | ||
messageId: 'mutableMapUsed', | ||
node | ||
}); | ||
} | ||
if (path[0] === 'Set' && isSetMutable(referenceTracker, node as TSESTree.Expression)) { | ||
context.report({ | ||
messageId: 'mutableSetUsed', | ||
node | ||
}); | ||
} | ||
if (path[0] === 'URL' && isURLMutable(referenceTracker, node as TSESTree.Expression)) { | ||
context.report({ | ||
messageId: 'mutableURLUsed', | ||
node | ||
}); | ||
} | ||
if ( | ||
path[0] === 'URLSearchParams' && | ||
isURLSearchParamsMutable(referenceTracker, node as TSESTree.Expression) | ||
) { | ||
context.report({ | ||
messageId: 'mutableURLSearchParamsUsed', | ||
node | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
function isDateMutable(referenceTracker: ReferenceTracker, ctorNode: TSESTree.Expression): boolean { | ||
return ( | ||
Array.from( | ||
referenceTracker.iteratePropertyReferences(ctorNode, { | ||
setDate: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setFullYear: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setHours: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setMilliseconds: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setMinutes: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setMonth: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setSeconds: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setTime: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCDate: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCFullYear: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCHours: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCMilliseconds: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCMinutes: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCMonth: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setUTCSeconds: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
setYear: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
}) | ||
).length > 0 | ||
); | ||
} | ||
|
||
function isMapMutable(referenceTracker: ReferenceTracker, ctorNode: TSESTree.Expression): boolean { | ||
return ( | ||
Array.from( | ||
referenceTracker.iteratePropertyReferences(ctorNode, { | ||
clear: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
delete: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
set: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
}) | ||
).length > 0 | ||
); | ||
} | ||
|
||
function isSetMutable(referenceTracker: ReferenceTracker, ctorNode: TSESTree.Expression): boolean { | ||
return ( | ||
Array.from( | ||
referenceTracker.iteratePropertyReferences(ctorNode, { | ||
add: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
clear: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
delete: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
}) | ||
).length > 0 | ||
); | ||
} | ||
|
||
function isURLMutable(referenceTracker: ReferenceTracker, ctorNode: TSESTree.Expression): boolean { | ||
for (const { node } of referenceTracker.iteratePropertyReferences(ctorNode, { | ||
hash: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
host: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
hostname: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
href: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
password: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
pathname: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
port: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
protocol: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
search: { | ||
[ReferenceTracker.READ]: true | ||
}, | ||
username: { | ||
[ReferenceTracker.READ]: true | ||
} | ||
})) { | ||
if (node.parent.type === 'AssignmentExpression') { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function isURLSearchParamsMutable( | ||
referenceTracker: ReferenceTracker, | ||
ctorNode: TSESTree.Expression | ||
): boolean { | ||
return ( | ||
Array.from( | ||
referenceTracker.iteratePropertyReferences(ctorNode, { | ||
append: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
delete: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
set: { | ||
[ReferenceTracker.CALL]: true | ||
}, | ||
sort: { | ||
[ReferenceTracker.CALL]: true | ||
} | ||
}) | ||
).length > 0 | ||
); | ||
} |
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
...nt-plugin-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/_requirements.json
This file contains hidden or 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,3 @@ | ||
{ | ||
"svelte": ">=5.0.0" | ||
} |
4 changes: 4 additions & 0 deletions
4
...n-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/date/setDate01-errors.yaml
This file contains hidden or 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,4 @@ | ||
- message: Found a mutable instance of the built-in Date class. Use SvelteDate instead. | ||
line: 2 | ||
column: 20 | ||
suggestions: null |
7 changes: 7 additions & 0 deletions
7
...-svelte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/date/setDate01-input.svelte
This file contains hidden or 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> | ||
const variable = new Date(8.64e15); | ||
|
||
variable.setDate(24); | ||
</script> | ||
|
||
{variable} |
4 changes: 4 additions & 0 deletions
4
...elte/tests/fixtures/rules/prefer-svelte-reactivity/invalid/date/setFullYear01-errors.yaml
This file contains hidden or 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,4 @@ | ||
- message: Found a mutable instance of the built-in Date class. Use SvelteDate instead. | ||
line: 2 | ||
column: 20 | ||
suggestions: null |
Oops, something went wrong.
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 think it can be written as follows:
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.
Great, I was struggling a little bit with iterators :D