|
| 1 | +import { createRule } from '../utils/index.js'; |
| 2 | +import { getSourceCode } from '../utils/compat.js'; |
| 3 | +import { ReferenceTracker } from '@eslint-community/eslint-utils'; |
| 4 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 5 | + |
| 6 | +const REACTIVE_CLASSES = [ |
| 7 | + 'SvelteSet', |
| 8 | + 'SvelteMap', |
| 9 | + 'SvelteURL', |
| 10 | + 'SvelteURLSearchParams', |
| 11 | + 'SvelteDate', |
| 12 | + 'MediaQuery' |
| 13 | +]; |
| 14 | + |
| 15 | +export default createRule('no-unnecessary-state-wrap', { |
| 16 | + meta: { |
| 17 | + docs: { |
| 18 | + description: 'Disallow unnecessary $state wrapping of reactive classes', |
| 19 | + category: 'Best Practices', |
| 20 | + recommended: true |
| 21 | + }, |
| 22 | + schema: [ |
| 23 | + { |
| 24 | + type: 'object', |
| 25 | + properties: { |
| 26 | + additionalReactiveClasses: { |
| 27 | + type: 'array', |
| 28 | + items: { |
| 29 | + type: 'string' |
| 30 | + }, |
| 31 | + uniqueItems: true |
| 32 | + }, |
| 33 | + allowReassign: { |
| 34 | + type: 'boolean' |
| 35 | + } |
| 36 | + }, |
| 37 | + additionalProperties: false |
| 38 | + } |
| 39 | + ], |
| 40 | + messages: { |
| 41 | + unnecessaryStateWrap: '{{className}} is already reactive, $state wrapping is unnecessary.', |
| 42 | + suggestRemoveStateWrap: 'Remove unnecessary $state wrapping' |
| 43 | + }, |
| 44 | + type: 'suggestion', |
| 45 | + hasSuggestions: true, |
| 46 | + conditions: [ |
| 47 | + { |
| 48 | + svelteVersions: ['5'], |
| 49 | + runes: [true, 'undetermined'] |
| 50 | + } |
| 51 | + ] |
| 52 | + }, |
| 53 | + create(context) { |
| 54 | + const options = context.options[0] ?? {}; |
| 55 | + const additionalReactiveClasses = options.additionalReactiveClasses ?? []; |
| 56 | + const allowReassign = options.allowReassign ?? false; |
| 57 | + |
| 58 | + const referenceTracker = new ReferenceTracker(getSourceCode(context).scopeManager.globalScope!); |
| 59 | + const traceMap: Record<string, Record<string, boolean>> = {}; |
| 60 | + for (const reactiveClass of REACTIVE_CLASSES) { |
| 61 | + traceMap[reactiveClass] = { |
| 62 | + [ReferenceTracker.CALL]: true, |
| 63 | + [ReferenceTracker.CONSTRUCT]: true |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + // Track all reactive class imports and their aliases |
| 68 | + const references = referenceTracker.iterateEsmReferences({ |
| 69 | + 'svelte/reactivity': { |
| 70 | + [ReferenceTracker.ESM]: true, |
| 71 | + ...traceMap |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + const referenceNodeAndNames = Array.from(references).map(({ node, path }) => { |
| 76 | + return { |
| 77 | + node, |
| 78 | + name: path[path.length - 1] |
| 79 | + }; |
| 80 | + }); |
| 81 | + |
| 82 | + function isReassigned(identifier: TSESTree.Identifier): boolean { |
| 83 | + const variable = getSourceCode(context).scopeManager.getDeclaredVariables( |
| 84 | + identifier.parent |
| 85 | + )[0]; |
| 86 | + return variable.references.some((ref) => { |
| 87 | + return ref.isWrite() && ref.identifier !== identifier; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + function reportUnnecessaryStateWrap( |
| 92 | + stateNode: TSESTree.Node, |
| 93 | + targetNode: TSESTree.Node, |
| 94 | + className: string, |
| 95 | + identifier?: TSESTree.Identifier |
| 96 | + ) { |
| 97 | + if (allowReassign && identifier && isReassigned(identifier)) { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + context.report({ |
| 102 | + node: targetNode, |
| 103 | + messageId: 'unnecessaryStateWrap', |
| 104 | + data: { |
| 105 | + className |
| 106 | + }, |
| 107 | + suggest: [ |
| 108 | + { |
| 109 | + messageId: 'suggestRemoveStateWrap', |
| 110 | + fix(fixer) { |
| 111 | + return fixer.replaceText(stateNode, getSourceCode(context).getText(targetNode)); |
| 112 | + } |
| 113 | + } |
| 114 | + ] |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + CallExpression(node: TSESTree.CallExpression) { |
| 120 | + if (node.callee.type !== 'Identifier' || node.callee.name !== '$state') { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + for (const arg of node.arguments) { |
| 125 | + if ( |
| 126 | + (arg.type === 'NewExpression' || arg.type === 'CallExpression') && |
| 127 | + arg.callee.type === 'Identifier' |
| 128 | + ) { |
| 129 | + const name = arg.callee.name; |
| 130 | + if (additionalReactiveClasses.includes(name)) { |
| 131 | + const parent = node.parent; |
| 132 | + if (parent?.type === 'VariableDeclarator' && parent.id.type === 'Identifier') { |
| 133 | + reportUnnecessaryStateWrap(node, arg, name, parent.id); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + |
| 140 | + 'Program:exit': () => { |
| 141 | + for (const { node, name } of referenceNodeAndNames) { |
| 142 | + if ( |
| 143 | + node.parent?.type === 'CallExpression' && |
| 144 | + node.parent.callee.type === 'Identifier' && |
| 145 | + node.parent.callee.name === '$state' |
| 146 | + ) { |
| 147 | + const parent = node.parent.parent; |
| 148 | + if (parent?.type === 'VariableDeclarator' && parent.id.type === 'Identifier') { |
| 149 | + reportUnnecessaryStateWrap(node.parent, node, name, parent.id); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + }; |
| 155 | + } |
| 156 | +}); |
0 commit comments