Skip to content

Commit aa54200

Browse files
authored
Detect components wrapped in HOC with all resolver (#344)
1 parent c462692 commit aa54200

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/resolver/__tests__/findAllComponentDefinitions-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,4 +227,30 @@ describe('findAllComponentDefinitions', () => {
227227
expect(result[0].value.type).toEqual('CallExpression');
228228
});
229229
});
230+
231+
describe('regressions', () => {
232+
it('finds component wrapped in HOC', () => {
233+
const source = `
234+
/**
235+
* @flow
236+
*/
237+
import * as React from 'react';
238+
239+
type Props = $ReadOnly<{|
240+
tabs: $ReadOnlyArray<string>,
241+
|}>;
242+
243+
const TetraAdminTabs = React.memo<Props>((props: Props) => (
244+
<div></div>
245+
));
246+
247+
export default TetraAdminTabs;
248+
`;
249+
250+
const result = parse(source);
251+
expect(Array.isArray(result)).toBe(true);
252+
expect(result.length).toBe(1);
253+
expect(result[0].value.type).toEqual('ArrowFunctionExpression');
254+
});
255+
});
230256
});

src/resolver/findAllComponentDefinitions.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,22 @@ export default function findAllReactCreateClassCalls(
5454
const inner = resolveToValue(path.get('arguments', 0));
5555
definitions.delete(inner);
5656
definitions.add(path);
57+
58+
// Do not traverse into arguments
59+
return false;
5760
} else if (isReactCreateClassCall(path)) {
5861
const resolvedPath = resolveToValue(path.get('arguments', 0));
5962
if (t.ObjectExpression.check(resolvedPath.node)) {
6063
definitions.add(resolvedPath);
6164
}
65+
66+
// Do not traverse into arguments
67+
return false;
6268
}
63-
return false;
69+
70+
// If it is neither of the above cases we need to traverse further
71+
// as this call expression could be a HOC
72+
this.traverse(path);
6473
},
6574
});
6675

0 commit comments

Comments
 (0)