|
| 1 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 2 | +import { createRule } from '../utils/index.js'; |
| 3 | +import { getScope } from 'src/utils/ast-utils.js'; |
| 4 | +import { getSourceCode } from 'src/utils/compat.js'; |
| 5 | + |
| 6 | +function isEffectOrEffectPre(node: TSESTree.CallExpression) { |
| 7 | + if (node.callee.type === 'Identifier') { |
| 8 | + return node.callee.name === '$effect'; |
| 9 | + } |
| 10 | + if (node.callee.type === 'MemberExpression') { |
| 11 | + return ( |
| 12 | + node.callee.object.type === 'Identifier' && |
| 13 | + node.callee.object.name === '$effect' && |
| 14 | + node.callee.property.type === 'Identifier' && |
| 15 | + node.callee.property.name === 'pre' |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + return false; |
| 20 | +} |
| 21 | + |
| 22 | +export default createRule('prefer-writable-derived', { |
| 23 | + meta: { |
| 24 | + docs: { |
| 25 | + description: 'Prefer using writable $derived instead of $state and $effect', |
| 26 | + category: 'Best Practices', |
| 27 | + recommended: true |
| 28 | + }, |
| 29 | + schema: [], |
| 30 | + messages: { |
| 31 | + unexpected: 'Prefer using writable $derived instead of $state and $effect' |
| 32 | + }, |
| 33 | + type: 'suggestion', |
| 34 | + conditions: [], |
| 35 | + fixable: 'code' |
| 36 | + }, |
| 37 | + create(context) { |
| 38 | + return { |
| 39 | + CallExpression: (node: TSESTree.CallExpression) => { |
| 40 | + if (!isEffectOrEffectPre(node)) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (node.arguments.length !== 1) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const argument = node.arguments[0]; |
| 49 | + if (argument.type !== 'FunctionExpression' && argument.type !== 'ArrowFunctionExpression') { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (argument.params.length !== 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if (argument.body.type !== 'BlockStatement') { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const body = argument.body.body; |
| 62 | + if (body.length !== 1) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const statement = body[0]; |
| 67 | + if (statement.type !== 'ExpressionStatement') { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const expression = statement.expression; |
| 72 | + if (expression.type !== 'AssignmentExpression') { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const { left, right, operator } = expression; |
| 77 | + if (operator !== '=' || left.type !== 'Identifier') { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const scope = getScope(context, statement); |
| 82 | + const reference = scope.references.find((reference) => { |
| 83 | + return ( |
| 84 | + reference.identifier.type === 'Identifier' && reference.identifier.name === left.name |
| 85 | + ); |
| 86 | + }); |
| 87 | + const defs = reference?.resolved?.defs; |
| 88 | + if (defs == null || defs.length !== 1) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const def = defs[0]; |
| 93 | + if (def.type !== 'Variable' || def.node.type !== 'VariableDeclarator') { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + const init = def.node.init; |
| 98 | + if (init == null || init.type !== 'CallExpression') { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (init.callee.type !== 'Identifier' || init.callee.name !== '$state') { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + context.report({ |
| 107 | + node: def.node, |
| 108 | + messageId: 'unexpected', |
| 109 | + fix: (fixer) => { |
| 110 | + const rightCode = getSourceCode(context).getText(right); |
| 111 | + return [fixer.replaceText(init, `$derived(${rightCode})`), fixer.remove(node)]; |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + }; |
| 116 | + } |
| 117 | +}); |
0 commit comments