(context)
| 21 | } |
| 22 | }, |
| 23 | create(context) { |
| 24 | return { |
| 25 | MemberExpression(member) { |
| 26 | // Look for member expressions that look like refs (i.e. `ref.current`). |
| 27 | if ( |
| 28 | member.object.type !== 'Identifier' || |
| 29 | member.computed || |
| 30 | member.property.type !== 'Identifier' || |
| 31 | member.property.name !== 'current' |
| 32 | ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // Find the parent function of this node, as well as any if statement matching against the ref value |
| 37 | // (i.e. lazy init pattern shown in React docs). |
| 38 | let node = member; |
| 39 | let fn; |
| 40 | let conditional; |
| 41 | while (node) { |
| 42 | if ( |
| 43 | node.type === 'FunctionDeclaration' || |
| 44 | node.type === 'FunctionExpression' || |
| 45 | node.type === 'ArrowFunctionExpression' |
| 46 | ) { |
| 47 | fn = node; |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | if ( |
| 52 | node.type === 'IfStatement' && |
| 53 | node.test.type === 'BinaryExpression' && |
| 54 | (node.test.operator === '==' || node.test.operator === '===') && |
| 55 | (isMemberExpressionEqual(node.test.left, member) || |
| 56 | isMemberExpressionEqual(node.test.right, member)) |
| 57 | ) { |
| 58 | conditional = node.test; |
| 59 | } |
| 60 | |
| 61 | node = node.parent; |
| 62 | } |
| 63 | |
| 64 | if (!fn) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // Find the variable definition for the object. |
| 69 | const variable = getVariable(context.sourceCode.getScope(node), member.object.name); |
| 70 | if (!variable) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Find the initialization of the variable and see if it's a call to useRef. |
| 75 | const refDefinition = variable.defs.find(def => { |
| 76 | const init = def.node.init; |
| 77 | if (!init) { |
| 78 | return false; |
| 79 | } |
| 80 |
nothing calls this directly
no outgoing calls
no test coverage detected