Skip to content

Commit 2873c8d

Browse files
authored
Assume HOC first param is Component not last (#343)
Unless we know for sure it is not a component (literal, array, object, spread)
1 parent a1f9b9a commit 2873c8d

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/utils/__tests__/resolveHOC-test.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,58 @@ describe('resolveHOC', () => {
1919
}
2020

2121
it('resolves simple hoc', () => {
22-
const path = parse(['hoc(42);'].join('\n'));
23-
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
22+
const path = parse(['hoc(Component);'].join('\n'));
23+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
2424
});
2525

2626
it('resolves simple hoc w/ multiple args', () => {
27-
const path = parse(['hoc1(arg1a, arg1b)(42);'].join('\n'));
28-
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
27+
const path = parse(['hoc1(arg1a, arg1b)(Component);'].join('\n'));
28+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
2929
});
3030

3131
it('resolves nested hocs', () => {
3232
const path = parse(
33-
['hoc2(arg2b, arg2b)(', ' hoc1(arg1a, arg2a)(42)', ');'].join('\n'),
33+
`hoc2(arg2b, arg2b)(
34+
hoc1(arg1a, arg2a)(Component)
35+
);`,
3436
);
35-
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
37+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
3638
});
3739

3840
it('resolves really nested hocs', () => {
3941
const path = parse(
40-
[
41-
'hoc3(arg3a, arg3b)(',
42-
' hoc2(arg2b, arg2b)(',
43-
' hoc1(arg1a, arg2a)(42)',
44-
' )',
45-
');',
46-
].join('\n'),
42+
`hoc3(arg3a, arg3b)(
43+
hoc2(arg2b, arg2b)(
44+
hoc1(arg1a, arg2a)(Component)
45+
)
46+
);`,
4747
);
48-
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
48+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
49+
});
50+
51+
it('resolves HOC with additional params', () => {
52+
const path = parse(`hoc3(Component, {})`);
53+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
54+
});
55+
56+
it('resolves HOC as last element if first is literal', () => {
57+
const path = parse(`hoc3(41, Component)`);
58+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
59+
});
60+
61+
it('resolves HOC as last element if first is array', () => {
62+
const path = parse(`hoc3([], Component)`);
63+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
64+
});
65+
66+
it('resolves HOC as last element if first is object', () => {
67+
const path = parse(`hoc3({}, Component)`);
68+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
69+
});
70+
71+
it('resolves HOC as last element if first is spread', () => {
72+
const path = parse(`hoc3(...params, Component)`);
73+
expect(resolveHOC(path)).toEqualASTNode(builders.identifier('Component'));
4974
});
5075

5176
it('resolves intermediate hocs', () => {

src/utils/resolveHOC.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,23 @@ export default function resolveHOC(path: NodePath): NodePath {
2828
!isReactForwardRefCall(path)
2929
) {
3030
if (node.arguments.length) {
31-
return resolveHOC(
32-
resolveToValue(path.get('arguments', node.arguments.length - 1)),
33-
);
31+
const inner = path.get('arguments', 0);
32+
33+
// If the first argument is one of these types then the component might be the last argument
34+
// If there are all identifiers then we cannot figure out exactly and have to assume it is the first
35+
if (
36+
node.arguments.length > 1 &&
37+
(t.Literal.check(inner.node) ||
38+
t.ObjectExpression.check(inner.node) ||
39+
t.ArrayExpression.check(inner.node) ||
40+
t.SpreadElement.check(inner.node))
41+
) {
42+
return resolveHOC(
43+
resolveToValue(path.get('arguments', node.arguments.length - 1)),
44+
);
45+
}
46+
47+
return resolveHOC(resolveToValue(inner));
3448
}
3549
}
3650

0 commit comments

Comments
 (0)