Skip to content

Commit

Permalink
Detect components wrapped in HOC with all resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Apr 13, 2019
1 parent 5582f91 commit 6069a99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/resolver/__tests__/findAllComponentDefinitions-test.js
Expand Up @@ -227,4 +227,30 @@ describe('findAllComponentDefinitions', () => {
expect(result[0].value.type).toEqual('CallExpression');
});
});

describe('regressions', () => {
it('finds component wrapped in HOC', () => {
const source = `
/**
* @flow
*/
import * as React from 'react';
type Props = $ReadOnly<{|
tabs: $ReadOnlyArray<string>,
|}>;
const TetraAdminTabs = React.memo<Props>((props: Props) => (
<div></div>
));
export default TetraAdminTabs;
`;

const result = parse(source);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(1);
expect(result[0].value.type).toEqual('ArrowFunctionExpression');
});
});
});
11 changes: 10 additions & 1 deletion src/resolver/findAllComponentDefinitions.js
Expand Up @@ -53,13 +53,22 @@ export default function findAllReactCreateClassCalls(
const inner = resolveToValue(path.get('arguments', 0));
definitions.delete(inner);
definitions.add(path);

// Do not traverse into arguments
return false;
} else if (isReactCreateClassCall(path)) {
const resolvedPath = resolveToValue(path.get('arguments', 0));
if (types.ObjectExpression.check(resolvedPath.node)) {
definitions.add(resolvedPath);
}

// Do not traverse into arguments
return false;
}
return false;

// If it is neither of the above cases we need to traverse further
// as this call expression could be a HOC
this.traverse(path);
},
});

Expand Down

0 comments on commit 6069a99

Please sign in to comment.