-
-
Notifications
You must be signed in to change notification settings - Fork 59
feat: Add require-store-callbacks-use-set-param rule
#284
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
Changes from 3 commits
e3a7e95
91744d9
40ab9b2
a6686e4
ec2d47d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "eslint-plugin-svelte": minor | ||
| --- | ||
|
|
||
| feat: add `require-store-callbacks-use-set-param` rule |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,66 @@ | ||||||||||||
| --- | ||||||||||||
| pageClass: "rule-details" | ||||||||||||
| sidebarDepth: 0 | ||||||||||||
| title: "svelte/require-store-callbacks-use-set-param" | ||||||||||||
| description: "store callbacks must use `set` param" | ||||||||||||
| --- | ||||||||||||
|
|
||||||||||||
| # svelte/require-store-callbacks-use-set-param | ||||||||||||
|
|
||||||||||||
| > Store callbacks must use `set` param. | ||||||||||||
|
|
||||||||||||
| ## :book: Rule Details | ||||||||||||
|
|
||||||||||||
| This rule disallows if readable / writable store's setter function doesn't use `set` parameter. | ||||||||||||
|
|
||||||||||||
| <ESLintCodeBlock> | ||||||||||||
|
|
||||||||||||
| <!--eslint-skip--> | ||||||||||||
|
|
||||||||||||
| ```svelte | ||||||||||||
| <script> | ||||||||||||
| /* eslint svelte/require-store-callbacks-use-set-param: "error" */ | ||||||||||||
| import { readable, writable, derived } from "svelte/store" | ||||||||||||
|
|
||||||||||||
| /** ✓ GOOD */ | ||||||||||||
| readable(false, (set) => set(true)) | ||||||||||||
| // `set` is unused but this rule doesn't report. | ||||||||||||
| // For that, please use `no-unused-vars` rule. | ||||||||||||
| // refer: https://eslint.org/docs/latest/rules/no-unused-vars | ||||||||||||
| readable(false, (set) => true) | ||||||||||||
|
|
||||||||||||
| writable(false, (set) => set(true)) | ||||||||||||
|
||||||||||||
| writable(false, (set) => set(true)) | |
| writable(false, (set) => { | |
| set(true) | |
| return () => {/* stop */} | |
| }) |
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 updated.
a6686e4
And I added description about derived store.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||
| import { createRule } from "../utils" | ||||||
| import { extractStoreReferences } from "./reference-helpers/svelte-store" | ||||||
|
|
||||||
| export default createRule("require-store-callbacks-use-set-param", { | ||||||
| meta: { | ||||||
| docs: { | ||||||
| description: "store callbacks must use `set` param", | ||||||
| category: "Possible Errors", | ||||||
| recommended: false, | ||||||
| }, | ||||||
| schema: [], | ||||||
| messages: { | ||||||
| unexpected: "Store callbacks must use `set` param.", | ||||||
| }, | ||||||
| type: "suggestion", | ||||||
| }, | ||||||
| create(context) { | ||||||
| return { | ||||||
| Program() { | ||||||
| for (const { node } of extractStoreReferences(context, [ | ||||||
| "readable", | ||||||
| "writable", | ||||||
| ])) { | ||||||
| const [_, fn] = node.arguments | ||||||
| if (!fn || fn.type !== "ArrowFunctionExpression") { | ||||||
|
||||||
| if (!fn || fn.type !== "ArrowFunctionExpression") { | |
| if (!fn || (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression")) { |
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.
Oops! Sorry I fixed it.
ec2d47d
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| - message: Disallow self-closing on HTML elements. | ||
| line: 3 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Require self-closing on HTML void elements. | ||
| line: 4 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Disallow self-closing on Svelte custom components. | ||
| line: 5 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Require self-closing on Svelte special elements. | ||
| line: 8 | ||
| column: 1 | ||
| suggestions: null |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| - message: Disallow self-closing on HTML elements. | ||
| line: 3 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Disallow self-closing on Svelte custom components. | ||
| line: 4 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Disallow self-closing on HTML void elements. | ||
| line: 5 | ||
| column: 3 | ||
| suggestions: null | ||
| - message: Disallow self-closing on Svelte special elements. | ||
| line: 8 | ||
| column: 1 | ||
| suggestions: null |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| - message: Store callbacks must use `set` param. | ||
| line: 4 | ||
| column: 19 | ||
| suggestions: null | ||
| - message: Store callbacks must use `set` param. | ||
| line: 5 | ||
| column: 19 | ||
| suggestions: null | ||
| - message: Store callbacks must use `set` param. | ||
| line: 7 | ||
| column: 19 | ||
| suggestions: null | ||
| - message: Store callbacks must use `set` param. | ||
| line: 8 | ||
| column: 19 | ||
| suggestions: null |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <script> | ||
| import { readable, writable } from "svelte/store" | ||
|
|
||
| readable(false, () => true) | ||
| readable(false, (foo) => true) | ||
|
|
||
| writable(false, () => true) | ||
| writable(false, (foo) => true) | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <script> | ||
| import { readable, writable, derived } from "svelte/store" | ||
|
|
||
| readable(false, (set) => set(true)) | ||
| readable(false, (set) => true) | ||
|
|
||
| writable(false, (set) => set(true)) | ||
| writable(false, (set) => true) | ||
|
|
||
| derived(a, ($a) => $a * 2) | ||
| derived( | ||
| a, | ||
| ($a, set) => { | ||
| setTimeout(() => set($a), 1000) | ||
| }, | ||
| "one moment...", | ||
| ) | ||
| </script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { RuleTester } from "eslint" | ||
| import rule from "../../../src/rules/require-store-callbacks-use-set-param" | ||
| import { loadTestCases } from "../../utils/utils" | ||
|
|
||
| const tester = new RuleTester({ | ||
| parserOptions: { | ||
| ecmaVersion: 2020, | ||
| sourceType: "module", | ||
| }, | ||
| }) | ||
|
|
||
| tester.run( | ||
| "require-store-callbacks-use-set-param", | ||
| rule as any, | ||
| loadTestCases("require-store-callbacks-use-set-param"), | ||
| ) |
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.
As far as I know it should return the stop function.
A rule doesn't have to check for it, but a GOOD example would be better if it was more correct.