|
| 1 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 2 | +import { createRule } from '../utils/index.js'; |
| 3 | +import { getScope } from '../utils/ast-utils.js'; |
| 4 | +import { VERSION as SVELTE_VERSION } from 'svelte/compiler'; |
| 5 | +import semver from 'semver'; |
| 6 | + |
| 7 | +// Writable derived were introduced in Svelte 5.25.0 |
| 8 | +const shouldRun = semver.satisfies(SVELTE_VERSION, '>=5.25.0'); |
| 9 | + |
| 10 | +type ValidFunctionType = TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression; |
| 11 | +type ValidFunction = ValidFunctionType & { |
| 12 | + body: TSESTree.BlockStatement; |
| 13 | +}; |
| 14 | + |
| 15 | +type ValidAssignmentExpression = TSESTree.AssignmentExpression & { |
| 16 | + operator: '='; |
| 17 | + left: TSESTree.Identifier; |
| 18 | +}; |
| 19 | + |
| 20 | +type ValidExpressionStatement = TSESTree.ExpressionStatement & { |
| 21 | + expression: ValidAssignmentExpression; |
| 22 | +}; |
| 23 | + |
| 24 | +function isEffectOrEffectPre(node: TSESTree.CallExpression) { |
| 25 | + if (node.callee.type === 'Identifier') { |
| 26 | + return node.callee.name === '$effect'; |
| 27 | + } |
| 28 | + if (node.callee.type === 'MemberExpression') { |
| 29 | + return ( |
| 30 | + node.callee.object.type === 'Identifier' && |
| 31 | + node.callee.object.name === '$effect' && |
| 32 | + node.callee.property.type === 'Identifier' && |
| 33 | + node.callee.property.name === 'pre' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | +} |
| 39 | + |
| 40 | +function isValidFunctionArgument(argument: TSESTree.Node): argument is ValidFunction { |
| 41 | + if ( |
| 42 | + (argument.type !== 'FunctionExpression' && argument.type !== 'ArrowFunctionExpression') || |
| 43 | + argument.params.length !== 0 |
| 44 | + ) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + if (argument.body.type !== 'BlockStatement') { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + return argument.body.body.length === 1; |
| 53 | +} |
| 54 | + |
| 55 | +function isValidAssignment(statement: TSESTree.Statement): statement is ValidExpressionStatement { |
| 56 | + if (statement.type !== 'ExpressionStatement') return false; |
| 57 | + |
| 58 | + const { expression } = statement; |
| 59 | + return ( |
| 60 | + expression.type === 'AssignmentExpression' && |
| 61 | + expression.operator === '=' && |
| 62 | + expression.left.type === 'Identifier' |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function isStateVariable(init: TSESTree.Expression | null): init is TSESTree.CallExpression { |
| 67 | + return ( |
| 68 | + init?.type === 'CallExpression' && |
| 69 | + init.callee.type === 'Identifier' && |
| 70 | + init.callee.name === '$state' |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export default createRule('prefer-writable-derived', { |
| 75 | + meta: { |
| 76 | + docs: { |
| 77 | + description: 'Prefer using writable $derived instead of $state and $effect', |
| 78 | + category: 'Best Practices', |
| 79 | + recommended: true |
| 80 | + }, |
| 81 | + schema: [], |
| 82 | + messages: { |
| 83 | + unexpected: 'Prefer using writable $derived instead of $state and $effect', |
| 84 | + suggestRewrite: 'Rewrite $state and $effect to $derived' |
| 85 | + }, |
| 86 | + type: 'suggestion', |
| 87 | + conditions: [ |
| 88 | + { |
| 89 | + svelteVersions: ['5'], |
| 90 | + runes: [true, 'undetermined'] |
| 91 | + } |
| 92 | + ], |
| 93 | + hasSuggestions: true |
| 94 | + }, |
| 95 | + create(context) { |
| 96 | + if (!shouldRun) { |
| 97 | + return {}; |
| 98 | + } |
| 99 | + return { |
| 100 | + CallExpression: (node: TSESTree.CallExpression) => { |
| 101 | + if (!isEffectOrEffectPre(node) || node.arguments.length !== 1) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + const argument = node.arguments[0]; |
| 106 | + if (!isValidFunctionArgument(argument)) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const statement = argument.body.body[0]; |
| 111 | + if (!isValidAssignment(statement)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + const { left, right } = statement.expression; |
| 116 | + const scope = getScope(context, statement); |
| 117 | + const reference = scope.references.find( |
| 118 | + (ref) => ref.identifier.type === 'Identifier' && ref.identifier.name === left.name |
| 119 | + ); |
| 120 | + |
| 121 | + const def = reference?.resolved?.defs?.[0]; |
| 122 | + if (!def || def.type !== 'Variable' || def.node.type !== 'VariableDeclarator') { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + const { init } = def.node; |
| 127 | + if (!isStateVariable(init)) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + context.report({ |
| 132 | + node: def.node, |
| 133 | + messageId: 'unexpected', |
| 134 | + suggest: [ |
| 135 | + { |
| 136 | + messageId: 'suggestRewrite', |
| 137 | + fix: (fixer) => { |
| 138 | + const rightCode = context.sourceCode.getText(right); |
| 139 | + return [fixer.replaceText(init, `$derived(${rightCode})`), fixer.remove(node)]; |
| 140 | + } |
| 141 | + } |
| 142 | + ] |
| 143 | + }); |
| 144 | + } |
| 145 | + }; |
| 146 | + } |
| 147 | +}); |
0 commit comments