Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect components wrapped in HOC with all resolver #344

Merged
merged 1 commit into from Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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