|
| 1 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 2 | + |
| 3 | +import { createRule } from '../utils/index.js'; |
| 4 | + |
| 5 | +type ReactiveFunction = '$props' | '$derived' | '$derived.by' | '$state' | '$state.raw'; |
| 6 | +const DEFAULT_FUNCTIONS: ReactiveFunction[] = [ |
| 7 | + '$props', |
| 8 | + '$derived', |
| 9 | + '$derived.by', |
| 10 | + '$state', |
| 11 | + '$state.raw' |
| 12 | +]; |
| 13 | + |
| 14 | +function getReactiveFunction(callExpr: TSESTree.CallExpression, validNames: string[]) { |
| 15 | + if (callExpr.callee.type === 'Identifier') { |
| 16 | + if (validNames.includes(callExpr.callee.name)) { |
| 17 | + return callExpr.callee.name as ReactiveFunction; |
| 18 | + } |
| 19 | + } else if ( |
| 20 | + callExpr.callee.type === 'MemberExpression' && |
| 21 | + callExpr.callee.object.type === 'Identifier' && |
| 22 | + callExpr.callee.property.type === 'Identifier' |
| 23 | + ) { |
| 24 | + const fullName = `${callExpr.callee.object.name}.${callExpr.callee.property.name}`; |
| 25 | + |
| 26 | + if (validNames.includes(fullName)) { |
| 27 | + return fullName as ReactiveFunction; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | +} |
| 33 | + |
| 34 | +export default createRule('prefer-let', { |
| 35 | + meta: { |
| 36 | + docs: { |
| 37 | + description: 'Prefer `let` over `const` for Svelte 5 reactive variable declarations.', |
| 38 | + category: 'Best Practices', |
| 39 | + recommended: false |
| 40 | + }, |
| 41 | + schema: [ |
| 42 | + { |
| 43 | + type: 'object', |
| 44 | + properties: { |
| 45 | + exclude: { |
| 46 | + type: 'array', |
| 47 | + items: { |
| 48 | + enum: ['$props', '$derived', '$derived.by', '$state', '$state.raw'] |
| 49 | + }, |
| 50 | + uniqueItems: true |
| 51 | + } |
| 52 | + }, |
| 53 | + additionalProperties: false |
| 54 | + } |
| 55 | + ], |
| 56 | + messages: { |
| 57 | + 'use-let': "'const' is used for a reactive declaration from {{rune}}. Use 'let' instead." |
| 58 | + }, |
| 59 | + type: 'suggestion', |
| 60 | + fixable: 'code' |
| 61 | + }, |
| 62 | + create(context) { |
| 63 | + const exclude = context.options[0]?.exclude ?? []; |
| 64 | + const allowedNames = DEFAULT_FUNCTIONS.filter((name) => !exclude.includes(name)); |
| 65 | + |
| 66 | + return { |
| 67 | + VariableDeclaration(node: TSESTree.VariableDeclaration) { |
| 68 | + if (node.kind === 'const') { |
| 69 | + node.declarations.forEach((declarator) => { |
| 70 | + const init = declarator.init; |
| 71 | + |
| 72 | + if (!init || init.type !== 'CallExpression') { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const rune = getReactiveFunction(init, allowedNames); |
| 77 | + if (rune) { |
| 78 | + context.report({ |
| 79 | + node, |
| 80 | + messageId: 'use-let', |
| 81 | + data: { rune }, |
| 82 | + fix: (fixer) => fixer.replaceTextRange([node.range[0], node.range[0] + 5], 'let') |
| 83 | + }); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + }; |
| 89 | + } |
| 90 | +}); |
0 commit comments