-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: implement derived-has-same-inputs-outputs
#249
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 2 commits
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 `svelte/derived-has-same-inputs-outputs` rule |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/derived-has-same-inputs-outputs" | ||
description: "derived store should use same variable names between values and callback" | ||
--- | ||
|
||
# svelte/derived-has-same-inputs-outputs | ||
|
||
> derived store should use same variable names between values and callback | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports where variable names and callback function's argument names are different. | ||
This is mainly a recommended rule to avoid implementation confusion. | ||
|
||
<ESLintCodeBlock language="javascript"> | ||
|
||
<!--eslint-skip--> | ||
|
||
```js | ||
/* eslint svelte/derived-has-same-inputs-outputs: "error" */ | ||
|
||
import { derived } from "svelte/store" | ||
|
||
/* ✓ GOOD */ | ||
derived(a, ($a) => {}); | ||
derived(a, ($a, set) => {}) | ||
derived([ a, b ], ([ $a, $b ]) => {}) | ||
|
||
/* ✗ BAD */ | ||
derived(a, (b) => {}); | ||
derived(a, (b, set) => {}); | ||
derived([ a, b ], ([ one, two ]) => {}) | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
|
||
## :books: Further Reading | ||
|
||
- [Svelte - Docs > RUN TIME > svelte/store > derived](https://svelte.dev/docs#run-time-svelte-store-derived) | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,110 @@ | ||||||||||||
import type * as ESTree from "estree" | ||||||||||||
import { createRule } from "../utils" | ||||||||||||
import type { RuleContext } from "../types" | ||||||||||||
import { extractStoreReferences } from "./reference-helpers/svelte-store" | ||||||||||||
|
||||||||||||
export default createRule("derived-has-same-inputs-outputs", { | ||||||||||||
meta: { | ||||||||||||
docs: { | ||||||||||||
description: "", | ||||||||||||
category: "Best Practices", | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated! ba6dccc |
||||||||||||
recommended: false, | ||||||||||||
}, | ||||||||||||
schema: [], | ||||||||||||
messages: { | ||||||||||||
unexpected: "The argument name should be '{{name}}'.", | ||||||||||||
}, | ||||||||||||
type: "suggestion", | ||||||||||||
}, | ||||||||||||
create(context) { | ||||||||||||
/** check node type */ | ||||||||||||
function isIdentifierOrArrayExpression( | ||||||||||||
node: ESTree.SpreadElement | ESTree.Expression, | ||||||||||||
): node is ESTree.Identifier | ESTree.ArrayExpression { | ||||||||||||
return ["Identifier", "ArrayExpression"].includes(node.type) | ||||||||||||
} | ||||||||||||
|
||||||||||||
type ArrowFunctionExpressionOrFunctionExpression = | ||||||||||||
| ESTree.ArrowFunctionExpression | ||||||||||||
| ESTree.FunctionExpression | ||||||||||||
|
||||||||||||
/** check node type */ | ||||||||||||
function isFunctionExpression( | ||||||||||||
node: ESTree.SpreadElement | ESTree.Expression, | ||||||||||||
): node is ArrowFunctionExpressionOrFunctionExpression { | ||||||||||||
return ["ArrowFunctionExpression", "FunctionExpression"].includes( | ||||||||||||
node.type, | ||||||||||||
) | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Check for identifier type. | ||||||||||||
* e.g. derived(a, ($a) => {}); | ||||||||||||
*/ | ||||||||||||
function checkIdentifier( | ||||||||||||
context: RuleContext, | ||||||||||||
args: ESTree.Identifier, | ||||||||||||
fn: ArrowFunctionExpressionOrFunctionExpression, | ||||||||||||
) { | ||||||||||||
const fnParam = fn.params[0] | ||||||||||||
if (fnParam.type !== "Identifier") return | ||||||||||||
const expectedName = `$${args.name}` | ||||||||||||
if (expectedName !== fnParam.name) { | ||||||||||||
context.report({ | ||||||||||||
node: fn, | ||||||||||||
loc: { | ||||||||||||
start: fnParam.loc?.start ?? { line: 1, column: 0 }, | ||||||||||||
end: fnParam.loc?.end ?? { line: 1, column: 0 }, | ||||||||||||
}, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nodes handled by ESLint always have a loc property, so non-null expressions can be used.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! ba6dccc |
||||||||||||
messageId: "unexpected", | ||||||||||||
data: { name: expectedName }, | ||||||||||||
}) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Check for array type. | ||||||||||||
* e.g. derived([ a, b ], ([ $a, $b ]) => {}) | ||||||||||||
*/ | ||||||||||||
function checkArrayExpression( | ||||||||||||
context: RuleContext, | ||||||||||||
args: ESTree.ArrayExpression, | ||||||||||||
fn: ArrowFunctionExpressionOrFunctionExpression, | ||||||||||||
) { | ||||||||||||
const fnParam = fn.params[0] | ||||||||||||
if (fnParam.type !== "ArrayPattern") return | ||||||||||||
const argNames = args.elements.map((element) => { | ||||||||||||
return element && element.type === "Identifier" ? element.name : null | ||||||||||||
}) | ||||||||||||
fnParam.elements.forEach((element, index) => { | ||||||||||||
if (element && element.type === "Identifier") { | ||||||||||||
const expectedName = `$${argNames[index]}` | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for that! I updated! ba6dccc |
||||||||||||
if (expectedName !== element.name) { | ||||||||||||
context.report({ | ||||||||||||
node: fn, | ||||||||||||
loc: { | ||||||||||||
start: element.loc?.start ?? { line: 1, column: 0 }, | ||||||||||||
end: element.loc?.end ?? { line: 1, column: 0 }, | ||||||||||||
}, | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed! ba6dccc |
||||||||||||
messageId: "unexpected", | ||||||||||||
data: { name: expectedName }, | ||||||||||||
}) | ||||||||||||
} | ||||||||||||
} | ||||||||||||
}) | ||||||||||||
} | ||||||||||||
|
||||||||||||
return { | ||||||||||||
Program() { | ||||||||||||
for (const { node } of extractStoreReferences(context, ["derived"])) { | ||||||||||||
const [args, fn] = node.arguments | ||||||||||||
if (!args || !isIdentifierOrArrayExpression(args)) continue | ||||||||||||
if (!fn || !isFunctionExpression(fn)) continue | ||||||||||||
if (!fn.params || fn.params.length === 0) continue | ||||||||||||
if (args.type === "Identifier") checkIdentifier(context, args, fn) | ||||||||||||
else checkArrayExpression(context, args, fn) | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
}, | ||||||||||||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
- message: The argument name should be '$a'. | ||
line: 3 | ||
column: 13 | ||
suggestions: null | ||
- message: The argument name should be '$c'. | ||
line: 6 | ||
column: 13 | ||
suggestions: null | ||
- message: The argument name should be '$e'. | ||
line: 9 | ||
column: 19 | ||
suggestions: null | ||
- message: The argument name should be '$f'. | ||
line: 9 | ||
column: 22 | ||
suggestions: null | ||
- message: The argument name should be '$i'. | ||
line: 12 | ||
column: 19 | ||
suggestions: null | ||
- message: The argument name should be '$j'. | ||
line: 12 | ||
column: 22 | ||
suggestions: null |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { derived } from "svelte/store" | ||
|
||
derived(a, (b) => { | ||
/** do nothing */ | ||
}) | ||
derived(c, (d, set) => { | ||
/** do nothing */ | ||
}) | ||
derived([e, f], ([g, h]) => { | ||
/** do nothing */ | ||
}) | ||
derived([i, j], ([k, l], set) => { | ||
/** do nothing */ | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { derived } from "svelte/store" | ||
|
||
derived(a, ($a) => { | ||
/** do nothing */ | ||
}) | ||
derived(c, ($c, set) => { | ||
/** do nothing */ | ||
}) | ||
derived([e, f], ([$e, $f]) => { | ||
/** do nothing */ | ||
}) | ||
derived([i, j], ([$i, $j], set) => { | ||
/** do nothing */ | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/derived-has-same-inputs-outputs" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"derived-has-same-inputs-outputs", | ||
rule as any, | ||
loadTestCases("derived-has-same-inputs-outputs"), | ||
) |
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.
description
must be set.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.
ah sorry!
ba6dccc