-
-
Notifications
You must be signed in to change notification settings - Fork 48
Add prefer-reactive-destructuring
rule
#205
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
Closed
Closed
Changes from all commits
Commits
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
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,55 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/prefer-reactive-destructuring" | ||
description: "Prefer destructuring from objects in reactive statements" | ||
--- | ||
|
||
# svelte/prefer-reactive-destructuring | ||
|
||
> Prefer destructuring from objects in reactive statements | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule triggers whenever a reactive statement contains an assignment that could be rewritten using destructuring. This is beneficial because it allows svelte's change-tracking to prevent wasteful redraws whenever the object is changed. | ||
|
||
This [svelte REPL](https://svelte.dev/repl/96759f4772314d0a840e11370ef76711) example shows just how effective this can be at preventing bogus redraws. | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script> | ||
/* eslint svelte/prefer-reactive-destructuring: "error" */ | ||
/* ✓ GOOD */ | ||
$: ({ foo } = info) | ||
$: ({ bar: baz } = info) | ||
|
||
/* ✗ BAD */ | ||
$: foo = info.foo | ||
$: baz = info.bar | ||
</script> | ||
|
||
|
||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing | ||
|
||
## :heart: Compatibility | ||
|
||
This rule was taken from [@tivac/eslint-plugin-svelte]. | ||
This rule is compatible with `@tivac/svelte/reactive-destructuring` rule. | ||
|
||
[@tivac/eslint-plugin-svelte]: https://github.com/tivac/eslint-plugin-svelte/ | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/prefer-reactive-destructuring.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/prefer-reactive-destructuring.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,67 @@ | ||
import type { TSESTree } from "@typescript-eslint/types" | ||
import { createRule } from "../utils" | ||
|
||
export default createRule("prefer-reactive-destructuring", { | ||
meta: { | ||
docs: { | ||
description: "Prefer destructuring from objects in reactive statements", | ||
category: "Best Practices", | ||
recommended: false, | ||
}, | ||
hasSuggestions: true, | ||
schema: [], | ||
messages: { | ||
useDestructuring: `Prefer destructuring in reactive statements`, | ||
suggestDestructuring: `Use destructuring to get finer-grained redraws`, | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
return { | ||
// Finds: $: info = foo.info | ||
// Suggests: $: ({ info } = foo); | ||
[`SvelteReactiveStatement > ExpressionStatement > AssignmentExpression[left.type="Identifier"][right.type="MemberExpression"]`]( | ||
tivac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: TSESTree.AssignmentExpression, | ||
) { | ||
const left = node.left as TSESTree.Identifier | ||
const right = node.right as TSESTree.MemberExpression | ||
|
||
const prop = (right.property as TSESTree.Identifier).name | ||
|
||
const source = context.getSourceCode() | ||
const lToken = source.getFirstToken(left) | ||
const rTokens = source.getLastTokens(right, { | ||
includeComments: true, | ||
count: 2, | ||
}) | ||
const matched = prop === left.name | ||
|
||
return context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: "useDestructuring", | ||
suggest: | ||
// Don't show suggestions for complex right-hand values, too tricky to get it right | ||
right.object.type !== "Identifier" || right.computed | ||
? [] | ||
: [ | ||
{ | ||
messageId: "suggestDestructuring", | ||
fix: (fixer) => [ | ||
fixer.insertTextBefore( | ||
lToken, | ||
matched ? `({ ` : `({ ${prop}: `, | ||
), | ||
fixer.insertTextAfter(lToken, ` }`), | ||
fixer.replaceTextRange( | ||
[rTokens[0].range[0], rTokens[1].range[1]], | ||
")", | ||
), | ||
], | ||
}, | ||
], | ||
}) | ||
}, | ||
} | ||
}, | ||
}) |
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
44 changes: 44 additions & 0 deletions
44
tests/fixtures/rules/prefer-reactive-destructuring/invalid/test01-errors.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,44 @@ | ||
[ | ||
{ | ||
"message": "Prefer destructuring in reactive statements", | ||
"line": 3, | ||
"column": 8, | ||
"suggestions": [ | ||
{ | ||
"desc": "Use destructuring to get finer-grained redraws", | ||
"messageId": "suggestDestructuring", | ||
"output": "<!-- prettier-ignore -->\n<script>\n $: ({ info } = foo)\n $: bar = foo.something\n $: baz = foo.bar.baz\n $: qux = foo[bar]\n $: dux = foo.baz().bar\n</script>\n" | ||
} | ||
] | ||
}, | ||
{ | ||
"message": "Prefer destructuring in reactive statements", | ||
"line": 4, | ||
"column": 8, | ||
"suggestions": [ | ||
{ | ||
"desc": "Use destructuring to get finer-grained redraws", | ||
"messageId": "suggestDestructuring", | ||
"output": "<!-- prettier-ignore -->\n<script>\n $: info = foo.info\n $: ({ something: bar } = foo)\n $: baz = foo.bar.baz\n $: qux = foo[bar]\n $: dux = foo.baz().bar\n</script>\n" | ||
} | ||
] | ||
}, | ||
{ | ||
"message": "Prefer destructuring in reactive statements", | ||
"line": 5, | ||
"column": 8, | ||
"suggestions": null | ||
}, | ||
{ | ||
"message": "Prefer destructuring in reactive statements", | ||
"line": 6, | ||
"column": 8, | ||
"suggestions": null | ||
}, | ||
{ | ||
"message": "Prefer destructuring in reactive statements", | ||
"line": 7, | ||
"column": 8, | ||
"suggestions": null | ||
} | ||
] |
8 changes: 8 additions & 0 deletions
8
tests/fixtures/rules/prefer-reactive-destructuring/invalid/test01-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,8 @@ | ||
<!-- prettier-ignore --> | ||
<script> | ||
$: info = foo.info | ||
$: bar = foo.something | ||
$: baz = foo.bar.baz | ||
$: qux = foo[bar] | ||
$: dux = foo.baz().bar | ||
</script> |
6 changes: 6 additions & 0 deletions
6
tests/fixtures/rules/prefer-reactive-destructuring/valid/test01-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,6 @@ | ||
<!-- prettier-ignore --> | ||
<script> | ||
$: ({ info } = foo) | ||
$: bar = baz | ||
$: qux = `bar ${baz}` | ||
</script> |
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,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/prefer-reactive-destructuring" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"prefer-reactive-destructuring", | ||
rule as any, | ||
loadTestCases("prefer-reactive-destructuring"), | ||
) |
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.
The first letter should be lowercase to be consistent with existing rules. (see #218)