|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Rule to check unused dependencies in React Hooks |
| 5 | + * @author Zheng Song |
| 6 | + */ |
| 7 | + |
| 8 | +const reactNamespace = 'React'; |
| 9 | +const hookNames = ['useEffect', 'useLayoutEffect']; |
| 10 | +const matchHooks = (name, { |
| 11 | + pattern, |
| 12 | + replace |
| 13 | +} = {}) => { |
| 14 | + let match = false; |
| 15 | + if (pattern) { |
| 16 | + match = new RegExp(pattern).test(name); |
| 17 | + if (replace) return match; |
| 18 | + } |
| 19 | + return match || hookNames.includes(name); |
| 20 | +}; |
| 21 | +const rule = { |
| 22 | + meta: { |
| 23 | + type: 'problem', |
| 24 | + schema: [{ |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + effectComment: { |
| 28 | + type: 'string' |
| 29 | + }, |
| 30 | + additionalHooks: { |
| 31 | + type: 'object', |
| 32 | + properties: { |
| 33 | + pattern: { |
| 34 | + type: 'string' |
| 35 | + }, |
| 36 | + replace: { |
| 37 | + type: 'boolean' |
| 38 | + } |
| 39 | + }, |
| 40 | + required: ['pattern'], |
| 41 | + additionalProperties: false |
| 42 | + } |
| 43 | + }, |
| 44 | + additionalProperties: false |
| 45 | + }], |
| 46 | + messages: { |
| 47 | + unused: 'React Hook {{ hook }} has unused dependencies: {{ unusedDeps }}. They might cause the Hook to run unintentionally. Either exclude them or prepend /* {{ effectComment }} */ comments to make the intention explicit.' |
| 48 | + } |
| 49 | + }, |
| 50 | + create(context) { |
| 51 | + var _context$sourceCode; |
| 52 | + const { |
| 53 | + effectComment = 'effect dep', |
| 54 | + additionalHooks |
| 55 | + } = context.options[0] || {}; |
| 56 | + const sourceCode = (_context$sourceCode = context.sourceCode) != null ? _context$sourceCode : context.getSourceCode(); |
| 57 | + const nodeListener = node => { |
| 58 | + const { |
| 59 | + parent |
| 60 | + } = node; |
| 61 | + if (parent.type !== 'CallExpression' || parent.arguments.length < 2 || parent.arguments[1].type !== 'ArrayExpression') { |
| 62 | + return; |
| 63 | + } |
| 64 | + const { |
| 65 | + callee |
| 66 | + } = parent; |
| 67 | + let hookName; |
| 68 | + switch (callee.type) { |
| 69 | + case 'Identifier': |
| 70 | + hookName = callee.name; |
| 71 | + break; |
| 72 | + case 'MemberExpression': |
| 73 | + if (callee.object.type === 'Identifier' && callee.object.name === reactNamespace && callee.property.type === 'Identifier') { |
| 74 | + hookName = callee.property.name; |
| 75 | + } |
| 76 | + break; |
| 77 | + } |
| 78 | + if (!hookName || !matchHooks(hookName, additionalHooks)) return; |
| 79 | + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); |
| 80 | + const through = scope.through.map(r => r.identifier.name); |
| 81 | + const depArray = parent.arguments[1]; |
| 82 | + const deps = depArray.elements.filter(e => (e == null ? void 0 : e.type) === 'Identifier'); |
| 83 | + const unusedDeps = []; |
| 84 | + for (const dep of deps) { |
| 85 | + if (through.includes(dep.name)) continue; |
| 86 | + if (sourceCode.getCommentsBefore(dep).some(({ |
| 87 | + value |
| 88 | + }) => value.includes(effectComment))) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + unusedDeps.push(dep.name); |
| 92 | + } |
| 93 | + if (unusedDeps.length > 0) { |
| 94 | + context.report({ |
| 95 | + node: depArray, |
| 96 | + messageId: 'unused', |
| 97 | + data: { |
| 98 | + hook: hookName, |
| 99 | + unusedDeps: unusedDeps.map(dep => `'${dep}'`).join(', '), |
| 100 | + effectComment |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + }; |
| 105 | + return { |
| 106 | + 'ArrowFunctionExpression,FunctionExpression': nodeListener |
| 107 | + }; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +module.exports = rule; |
0 commit comments