|
| 1 | +import type { Rule } from 'eslint'; |
| 2 | +import { ReferenceTracker, findVariable } from '@eslint-community/eslint-utils'; |
| 3 | +import path from 'node:path'; |
| 4 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 5 | +import type { Variable } from '@typescript-eslint/scope-manager'; |
| 6 | + |
| 7 | +export default { |
| 8 | + meta: { |
| 9 | + docs: { |
| 10 | + description: 'enforce to use findVariableSafe() to avoid infinite recursion', |
| 11 | + category: 'Best Practices', |
| 12 | + recommended: false, |
| 13 | + conflictWithPrettier: false, |
| 14 | + url: 'https://github.com/sveltejs/eslint-plugin-svelte/blob/v3.12.3/docs/rules/prefer-find-variable-safe.md' |
| 15 | + }, |
| 16 | + messages: { |
| 17 | + preferFindVariableSafe: 'Prefer to use findVariableSafe() to avoid infinite recursion.' |
| 18 | + }, |
| 19 | + schema: [], |
| 20 | + type: 'suggestion' |
| 21 | + }, |
| 22 | + create(context: Rule.RuleContext): Rule.RuleListener { |
| 23 | + const referenceTracker = new ReferenceTracker( |
| 24 | + context.sourceCode.scopeManager.globalScope as never |
| 25 | + ); |
| 26 | + let astUtilsPath = path.relative( |
| 27 | + path.dirname(context.physicalFilename), |
| 28 | + path.join(import.meta.dirname, '..', 'src', 'utils', 'ast-utils') |
| 29 | + ); |
| 30 | + if (!astUtilsPath.startsWith('.')) { |
| 31 | + astUtilsPath = `./${astUtilsPath}`; |
| 32 | + } |
| 33 | + const findVariableCalls = [ |
| 34 | + ...referenceTracker.iterateEsmReferences({ |
| 35 | + [astUtilsPath]: { |
| 36 | + [ReferenceTracker.ESM]: true, |
| 37 | + findVariable: { |
| 38 | + [ReferenceTracker.CALL]: true |
| 39 | + } |
| 40 | + }, |
| 41 | + [`${astUtilsPath}.js`]: { |
| 42 | + [ReferenceTracker.ESM]: true, |
| 43 | + findVariable: { |
| 44 | + [ReferenceTracker.CALL]: true |
| 45 | + } |
| 46 | + }, |
| 47 | + [`${astUtilsPath}.ts`]: { |
| 48 | + [ReferenceTracker.ESM]: true, |
| 49 | + findVariable: { |
| 50 | + [ReferenceTracker.CALL]: true |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + ]; |
| 55 | + type FunctionContext = { |
| 56 | + node: |
| 57 | + | TSESTree.FunctionDeclaration |
| 58 | + | TSESTree.FunctionExpression |
| 59 | + | TSESTree.ArrowFunctionExpression; |
| 60 | + identifier: TSESTree.Identifier | null; |
| 61 | + findVariableCall?: TSESTree.CallExpression; |
| 62 | + calls: Set<TSESTree.Identifier>; |
| 63 | + upper: FunctionContext | null; |
| 64 | + }; |
| 65 | + let functionStack: FunctionContext | null = null; |
| 66 | + const functionContexts: FunctionContext[] = []; |
| 67 | + |
| 68 | + function getFunctionVariableName( |
| 69 | + node: |
| 70 | + | TSESTree.FunctionDeclaration |
| 71 | + | TSESTree.FunctionExpression |
| 72 | + | TSESTree.ArrowFunctionExpression |
| 73 | + ) { |
| 74 | + if (node.type === 'FunctionDeclaration') { |
| 75 | + return node.id; |
| 76 | + } |
| 77 | + if (node.parent?.type === 'VariableDeclarator' && node.parent.id.type === 'Identifier') { |
| 78 | + return node.parent.id; |
| 79 | + } |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + function* iterateVariables(node: TSESTree.Identifier) { |
| 84 | + const visitedNodes = new Set<TSESTree.Identifier>(); |
| 85 | + let currentNode: TSESTree.Identifier | null = node; |
| 86 | + while (currentNode) { |
| 87 | + if (visitedNodes.has(currentNode)) break; |
| 88 | + const variable = findVariable( |
| 89 | + context.sourceCode.getScope(currentNode), |
| 90 | + currentNode |
| 91 | + ) as Variable | null; |
| 92 | + if (!variable) break; |
| 93 | + yield variable; |
| 94 | + const def = variable.defs[0]; |
| 95 | + if (!def) break; |
| 96 | + if (def.type !== 'Variable' || !def.node.init) break; |
| 97 | + if (def.node.init.type !== 'Identifier') break; |
| 98 | + currentNode = def.node.init; |
| 99 | + visitedNodes.add(currentNode); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Verify a function context to report if necessary. |
| 105 | + * Reports when a function contains a call to findVariable and the function is recursive. |
| 106 | + */ |
| 107 | + function verifyFunctionContext(functionContext: FunctionContext) { |
| 108 | + if (!functionContext.findVariableCall) return; |
| 109 | + if (!hasRecursive(functionContext)) return; |
| 110 | + context.report({ |
| 111 | + node: functionContext.findVariableCall, |
| 112 | + messageId: 'preferFindVariableSafe' |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + function hasRecursive(functionContext: FunctionContext) { |
| 117 | + const buffer = [functionContext]; |
| 118 | + const visitedContext = new Set<FunctionContext>(); |
| 119 | + let current; |
| 120 | + while ((current = buffer.shift())) { |
| 121 | + if (visitedContext.has(current)) continue; |
| 122 | + visitedContext.add(current); |
| 123 | + if (!current.identifier) continue; |
| 124 | + for (const variable of iterateVariables(current.identifier)) { |
| 125 | + for (const { identifier } of variable.references) { |
| 126 | + if (identifier.type !== 'Identifier') continue; |
| 127 | + if (functionContext.calls.has(identifier)) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + buffer.push(...functionContexts.filter((ctx) => ctx.calls.has(identifier))); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return { |
| 138 | + ':function'( |
| 139 | + node: |
| 140 | + | TSESTree.FunctionDeclaration |
| 141 | + | TSESTree.FunctionExpression |
| 142 | + | TSESTree.ArrowFunctionExpression |
| 143 | + ) { |
| 144 | + functionStack = { |
| 145 | + node, |
| 146 | + identifier: getFunctionVariableName(node), |
| 147 | + calls: new Set(), |
| 148 | + upper: functionStack |
| 149 | + }; |
| 150 | + functionContexts.push(functionStack); |
| 151 | + }, |
| 152 | + ':function:exit'() { |
| 153 | + functionStack = functionStack?.upper || null; |
| 154 | + }, |
| 155 | + CallExpression(node) { |
| 156 | + if (!functionStack) return; |
| 157 | + if (findVariableCalls.some((call) => call.node === node)) { |
| 158 | + functionStack.findVariableCall = node; |
| 159 | + } |
| 160 | + if (node.callee.type === 'Identifier') { |
| 161 | + functionStack.calls.add(node.callee); |
| 162 | + } |
| 163 | + }, |
| 164 | + 'Program:exit'() { |
| 165 | + for (const functionContext of functionContexts) { |
| 166 | + verifyFunctionContext(functionContext); |
| 167 | + } |
| 168 | + } |
| 169 | + }; |
| 170 | + } |
| 171 | +}; |
0 commit comments