|
| 1 | +import _ from 'lodash'; |
| 2 | +import CONST from './CONST.js'; |
| 3 | + |
| 4 | +const meta = { |
| 5 | + type: 'problem', |
| 6 | + docs: { |
| 7 | + description: |
| 8 | + 'Disallow deep equality checks in React.memo comparison functions', |
| 9 | + recommended: 'error', |
| 10 | + }, |
| 11 | + schema: [], |
| 12 | + messages: { |
| 13 | + noDeepEqualInMemo: CONST.MESSAGE.NO_DEEP_EQUAL_IN_MEMO, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +const DEEP_EQUAL_FUNCTIONS = new Set(['deepEqual', 'isEqual', 'isDeepEqual']); |
| 18 | + |
| 19 | +/** |
| 20 | + * Check if a node is a React.memo or memo call |
| 21 | + * @param {Object} node |
| 22 | + * @returns {Boolean} |
| 23 | + */ |
| 24 | +function isMemoCall(node) { |
| 25 | + if (node.type !== 'CallExpression') { |
| 26 | + return false; |
| 27 | + } |
| 28 | + |
| 29 | + const callee = node.callee; |
| 30 | + |
| 31 | + // Check for React.memo() |
| 32 | + if (callee.type === 'MemberExpression') { |
| 33 | + const object = _.get(callee, 'object.name'); |
| 34 | + const property = _.get(callee, 'property.name'); |
| 35 | + return object === 'React' && property === 'memo'; |
| 36 | + } |
| 37 | + |
| 38 | + // Check for memo() |
| 39 | + if (callee.type === 'Identifier') { |
| 40 | + return callee.name === 'memo'; |
| 41 | + } |
| 42 | + |
| 43 | + return false; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Check if a node is a deep equality function call |
| 48 | + * @param {Object} node |
| 49 | + * @returns {Boolean} |
| 50 | + */ |
| 51 | +function isDeepEqualCall(node) { |
| 52 | + if (node.type !== 'CallExpression') { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + const callee = node.callee; |
| 57 | + |
| 58 | + // Check for direct function calls like deepEqual(), isEqual(), etc. |
| 59 | + if (callee.type === 'Identifier') { |
| 60 | + return DEEP_EQUAL_FUNCTIONS.has(callee.name); |
| 61 | + } |
| 62 | + |
| 63 | + // Check for method calls like _.isEqual(), lodash.isEqual(), etc. |
| 64 | + if (callee.type === 'MemberExpression') { |
| 65 | + const methodName = _.get(callee, 'property.name'); |
| 66 | + return DEEP_EQUAL_FUNCTIONS.has(methodName); |
| 67 | + } |
| 68 | + |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Check if a node is a JSON.stringify comparison |
| 74 | + * @param {Object} node |
| 75 | + * @returns {Boolean} |
| 76 | + */ |
| 77 | +function isJSONStringifyComparison(node) { |
| 78 | + if (node.type !== 'BinaryExpression') { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + const {left, right, operator} = node; |
| 83 | + |
| 84 | + // Check if operator is === or !== |
| 85 | + if (operator !== '===' && operator !== '!==') { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + // Check if either side is JSON.stringify() |
| 90 | + const isLeftStringify = left.type === 'CallExpression' |
| 91 | + && left.callee.type === 'MemberExpression' |
| 92 | + && _.get(left, 'callee.object.name') === 'JSON' |
| 93 | + && _.get(left, 'callee.property.name') === 'stringify'; |
| 94 | + |
| 95 | + const isRightStringify = right.type === 'CallExpression' |
| 96 | + && right.callee.type === 'MemberExpression' |
| 97 | + && _.get(right, 'callee.object.name') === 'JSON' |
| 98 | + && _.get(right, 'callee.property.name') === 'stringify'; |
| 99 | + |
| 100 | + return isLeftStringify || isRightStringify; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Recursively check if a node or its descendants contain deep equality checks |
| 105 | + * @param {Object} node |
| 106 | + * @param {Object} context |
| 107 | + */ |
| 108 | +function checkForDeepEqual(node, context) { |
| 109 | + if (!node) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Check if current node is a deep equal call |
| 114 | + if (isDeepEqualCall(node)) { |
| 115 | + context.report({ |
| 116 | + node, |
| 117 | + messageId: 'noDeepEqualInMemo', |
| 118 | + }); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + // Check if current node is a JSON.stringify comparison |
| 123 | + if (isJSONStringifyComparison(node)) { |
| 124 | + context.report({ |
| 125 | + node, |
| 126 | + messageId: 'noDeepEqualInMemo', |
| 127 | + }); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + // Recursively check child nodes |
| 132 | + _.keys(node).forEach((key) => { |
| 133 | + if (key === 'parent' || key === 'loc' || key === 'range') { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + const child = node[key]; |
| 138 | + |
| 139 | + if (_.isArray(child)) { |
| 140 | + child.forEach(item => checkForDeepEqual(item, context)); |
| 141 | + } else if (child && typeof child === 'object' && child.type) { |
| 142 | + checkForDeepEqual(child, context); |
| 143 | + } |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +function create(context) { |
| 148 | + return { |
| 149 | + CallExpression(node) { |
| 150 | + // Check if this is a React.memo() or memo() call |
| 151 | + if (!isMemoCall(node)) { |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + // Check if there's a second argument (comparison function) |
| 156 | + if (node.arguments.length < 2) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + const comparisonFunction = node.arguments[1]; |
| 161 | + |
| 162 | + // Only check inline function expressions or arrow functions |
| 163 | + // Note: We don't check variable references (e.g., memo(Component, myCompareFunc)) |
| 164 | + // because tracking variable definitions across scopes is complex and would require |
| 165 | + // significant scope analysis. Users should inline their comparison functions for this |
| 166 | + // rule to work effectively. |
| 167 | + if ( |
| 168 | + comparisonFunction.type !== 'FunctionExpression' |
| 169 | + && comparisonFunction.type !== 'ArrowFunctionExpression' |
| 170 | + ) { |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + // Check the body of the comparison function for deep equality checks |
| 175 | + checkForDeepEqual(comparisonFunction.body, context); |
| 176 | + }, |
| 177 | + }; |
| 178 | +} |
| 179 | + |
| 180 | +export {meta, create}; |
0 commit comments