|
| 1 | +import type * as ESTree from "estree" |
| 2 | +import { createRule } from "../utils" |
| 3 | +import type { RuleContext } from "../types" |
| 4 | +import { extractStoreReferences } from "./reference-helpers/svelte-store" |
| 5 | + |
| 6 | +export default createRule("derived-has-same-inputs-outputs", { |
| 7 | + meta: { |
| 8 | + docs: { |
| 9 | + description: "", |
| 10 | + category: "Best Practices", |
| 11 | + recommended: false, |
| 12 | + }, |
| 13 | + schema: [], |
| 14 | + messages: { |
| 15 | + unexpected: "The argument name should be '{{name}}'.", |
| 16 | + }, |
| 17 | + type: "suggestion", |
| 18 | + }, |
| 19 | + create(context) { |
| 20 | + /** check node type */ |
| 21 | + function isIdentifierOrArrayExpression( |
| 22 | + node: ESTree.SpreadElement | ESTree.Expression, |
| 23 | + ): node is ESTree.Identifier | ESTree.ArrayExpression { |
| 24 | + return ["Identifier", "ArrayExpression"].includes(node.type) |
| 25 | + } |
| 26 | + |
| 27 | + type ArrowFunctionExpressionOrFunctionExpression = |
| 28 | + | ESTree.ArrowFunctionExpression |
| 29 | + | ESTree.FunctionExpression |
| 30 | + |
| 31 | + /** check node type */ |
| 32 | + function isFunctionExpression( |
| 33 | + node: ESTree.SpreadElement | ESTree.Expression, |
| 34 | + ): node is ArrowFunctionExpressionOrFunctionExpression { |
| 35 | + return ["ArrowFunctionExpression", "FunctionExpression"].includes( |
| 36 | + node.type, |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Check for identifier type. |
| 42 | + * e.g. derived(a, ($a) => {}); |
| 43 | + */ |
| 44 | + function checkIdentifier( |
| 45 | + context: RuleContext, |
| 46 | + args: ESTree.Identifier, |
| 47 | + fn: ArrowFunctionExpressionOrFunctionExpression, |
| 48 | + ) { |
| 49 | + const fnParam = fn.params[0] |
| 50 | + if (fnParam.type !== "Identifier") return |
| 51 | + const expectedName = `$${args.name}` |
| 52 | + if (expectedName !== fnParam.name) { |
| 53 | + context.report({ |
| 54 | + node: fn, |
| 55 | + loc: { |
| 56 | + start: fnParam.loc?.start ?? { line: 1, column: 0 }, |
| 57 | + end: fnParam.loc?.end ?? { line: 1, column: 0 }, |
| 58 | + }, |
| 59 | + messageId: "unexpected", |
| 60 | + data: { name: expectedName }, |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check for array type. |
| 67 | + * e.g. derived([ a, b ], ([ $a, $b ]) => {}) |
| 68 | + */ |
| 69 | + function checkArrayExpression( |
| 70 | + context: RuleContext, |
| 71 | + args: ESTree.ArrayExpression, |
| 72 | + fn: ArrowFunctionExpressionOrFunctionExpression, |
| 73 | + ) { |
| 74 | + const fnParam = fn.params[0] |
| 75 | + if (fnParam.type !== "ArrayPattern") return |
| 76 | + const argNames = args.elements.map((element) => { |
| 77 | + return element && element.type === "Identifier" ? element.name : null |
| 78 | + }) |
| 79 | + fnParam.elements.forEach((element, index) => { |
| 80 | + if (element && element.type === "Identifier") { |
| 81 | + const expectedName = `$${argNames[index]}` |
| 82 | + if (expectedName !== element.name) { |
| 83 | + context.report({ |
| 84 | + node: fn, |
| 85 | + loc: { |
| 86 | + start: element.loc?.start ?? { line: 1, column: 0 }, |
| 87 | + end: element.loc?.end ?? { line: 1, column: 0 }, |
| 88 | + }, |
| 89 | + messageId: "unexpected", |
| 90 | + data: { name: expectedName }, |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + Program() { |
| 99 | + for (const { node } of extractStoreReferences(context, ["derived"])) { |
| 100 | + const [args, fn] = node.arguments |
| 101 | + if (!args || !isIdentifierOrArrayExpression(args)) continue |
| 102 | + if (!fn || !isFunctionExpression(fn)) continue |
| 103 | + if (!fn.params || fn.params.length === 0) continue |
| 104 | + if (args.type === "Identifier") checkIdentifier(context, args, fn) |
| 105 | + else checkArrayExpression(context, args, fn) |
| 106 | + } |
| 107 | + }, |
| 108 | + } |
| 109 | + }, |
| 110 | +}) |
0 commit comments