Skip to content

Commit 081d3fe

Browse files
authored
fix: resolve value to a proper node instead of an array (#278)
1 parent 631c884 commit 081d3fe

File tree

7 files changed

+52
-39
lines changed

7 files changed

+52
-39
lines changed

src/handlers/__tests__/propTypeHandler-test.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,8 @@ describe('propTypeHandler', () => {
4949

5050
propTypeHandler(documentation, definition);
5151

52-
expect(getPropTypeMock.mock.calls[0][0].node).toEqualASTNode(
53-
fooPath.node,
54-
);
55-
expect(getPropTypeMock.mock.calls[1][0].node).toEqualASTNode(
56-
xyzPath.node,
57-
);
52+
expect(getPropTypeMock.mock.calls[0][0]).toEqualASTNode(fooPath);
53+
expect(getPropTypeMock.mock.calls[1][0]).toEqualASTNode(xyzPath);
5854
});
5955

6056
it('finds definitions via React.PropTypes', () => {

src/utils/__tests__/getMemberExpressionRoot-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ import getMemberExpressionRoot from '../getMemberExpressionRoot';
1717
describe('getMemberExpressionRoot', () => {
1818
it('returns the root of a member expression', () => {
1919
const root = getMemberExpressionRoot(expression('foo.bar.baz'));
20-
expect(root.node).toEqualASTNode(expression('foo').node);
20+
expect(root).toEqualASTNode(expression('foo'));
2121
});
2222
});

src/utils/__tests__/resolveHOC-test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ describe('resolveHOC', () => {
3131

3232
it('resolves simple hoc', () => {
3333
const path = parse(['hoc(42);'].join('\n'));
34-
expect(resolveHOC(path).node).toEqualASTNode(builders.literal(42));
34+
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
3535
});
3636

3737
it('resolves simple hoc w/ multiple args', () => {
3838
const path = parse(['hoc1(arg1a, arg1b)(42);'].join('\n'));
39-
expect(resolveHOC(path).node).toEqualASTNode(builders.literal(42));
39+
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
4040
});
4141

4242
it('resolves nested hocs', () => {
4343
const path = parse(
4444
['hoc2(arg2b, arg2b)(', ' hoc1(arg1a, arg2a)(42)', ');'].join('\n'),
4545
);
46-
expect(resolveHOC(path).node).toEqualASTNode(builders.literal(42));
46+
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
4747
});
4848

4949
it('resolves really nested hocs', () => {
@@ -56,6 +56,6 @@ describe('resolveHOC', () => {
5656
');',
5757
].join('\n'),
5858
);
59-
expect(resolveHOC(path).node).toEqualASTNode(builders.literal(42));
59+
expect(resolveHOC(path)).toEqualASTNode(builders.literal(42));
6060
});
6161
});

src/utils/__tests__/resolveObjectKeysToArray-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('resolveObjectKeysToArray', () => {
2727
['var foo = { bar: 1, foo: 2 };', 'Object.keys(foo);'].join('\n'),
2828
);
2929

30-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
30+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
3131
builders.arrayExpression([
3232
builders.literal('bar'),
3333
builders.literal('foo'),
@@ -40,7 +40,7 @@ describe('resolveObjectKeysToArray', () => {
4040
['var foo = { "bar": 1, 5: 2 };', 'Object.keys(foo);'].join('\n'),
4141
);
4242

43-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
43+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
4444
builders.arrayExpression([
4545
builders.literal('bar'),
4646
builders.literal('5'),
@@ -53,7 +53,7 @@ describe('resolveObjectKeysToArray', () => {
5353
['var foo = { ["bar"]: 1, [5]: 2};', 'Object.keys(foo);'].join('\n'),
5454
);
5555

56-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
56+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
5757
builders.arrayExpression([
5858
builders.literal('bar'),
5959
builders.literal('5'),
@@ -70,7 +70,7 @@ describe('resolveObjectKeysToArray', () => {
7070
].join('\n'),
7171
);
7272

73-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
73+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
7474
builders.arrayExpression([
7575
builders.literal('boo'),
7676
builders.literal('foo'),
@@ -86,7 +86,7 @@ describe('resolveObjectKeysToArray', () => {
8686
),
8787
);
8888

89-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
89+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
9090
builders.arrayExpression([
9191
builders.literal('boo'),
9292
builders.literal('foo'),
@@ -103,7 +103,7 @@ describe('resolveObjectKeysToArray', () => {
103103
].join('\n'),
104104
);
105105

106-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
106+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
107107
builders.arrayExpression([
108108
builders.literal('boo'),
109109
builders.literal('foo'),
@@ -121,7 +121,7 @@ describe('resolveObjectKeysToArray', () => {
121121
].join('\n'),
122122
);
123123

124-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
124+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
125125
builders.arrayExpression([
126126
builders.literal('boo'),
127127
builders.literal('foo'),
@@ -137,7 +137,7 @@ describe('resolveObjectKeysToArray', () => {
137137
),
138138
);
139139

140-
expect(resolveObjectKeysToArray(path).node).toEqualASTNode(
140+
expect(resolveObjectKeysToArray(path)).toEqualASTNode(
141141
builders.arrayExpression([builders.literal('x')]),
142142
);
143143
});

src/utils/__tests__/resolveToValue-test.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ describe('resolveToValue', () => {
2424

2525
it('resolves simple variable declarations', () => {
2626
const path = parse(['var foo = 42;', 'foo;'].join('\n'));
27-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(42));
27+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(42));
2828
});
2929

3030
it('resolves object destructuring', () => {
3131
const path = parse(['var {foo: {bar: baz}} = bar;', 'baz;'].join('\n'));
3232

3333
// Node should be equal to bar.foo.bar
34-
expect(resolveToValue(path).node).toEqualASTNode(
34+
expect(resolveToValue(path)).toEqualASTNode(
3535
builders.memberExpression(
3636
builders.memberExpression(
3737
builders.identifier('bar'),
@@ -45,19 +45,19 @@ describe('resolveToValue', () => {
4545
it('handles SpreadElements properly', () => {
4646
const path = parse(['var {foo: {bar}, ...baz} = bar;', 'baz;'].join('\n'));
4747

48-
expect(resolveToValue(path).node).toEqualASTNode(path.node);
48+
expect(resolveToValue(path)).toEqualASTNode(path);
4949
});
5050

5151
it('returns the original path if it cannot be resolved', () => {
5252
const path = parse(['function foo() {}', 'foo()'].join('\n'));
5353

54-
expect(resolveToValue(path).node).toEqualASTNode(path.node);
54+
expect(resolveToValue(path)).toEqualASTNode(path);
5555
});
5656

5757
it('resolves variable declarators to their init value', () => {
5858
const path = utils.parse('var foo = 42;').get('body', 0, 'declarations', 0);
5959

60-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(42));
60+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(42));
6161
});
6262

6363
it('resolves to class declarations', () => {
@@ -84,49 +84,57 @@ describe('resolveToValue', () => {
8484
it('resolves to assigned values', () => {
8585
const path = parse(['var foo;', 'foo = 42;', 'foo;'].join('\n'));
8686

87-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(42));
87+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(42));
8888
});
8989
});
9090

9191
describe('ImportDeclaration', () => {
9292
it('resolves default import references to the import declaration', () => {
9393
const path = parse(['import foo from "Foo"', 'foo;'].join('\n'));
94+
const value = resolveToValue(path);
9495

95-
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
96+
expect(Array.isArray(value.value)).toBe(false);
97+
expect(value.node.type).toBe('ImportDeclaration');
9698
});
9799

98100
it('resolves named import references to the import declaration', () => {
99101
const path = parse(['import {foo} from "Foo"', 'foo;'].join('\n'));
102+
const value = resolveToValue(path);
100103

101-
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
104+
expect(Array.isArray(value.value)).toBe(false);
105+
expect(value.node.type).toBe('ImportDeclaration');
102106
});
103107

104108
it('resolves aliased import references to the import declaration', () => {
105109
const path = parse(['import {foo as bar} from "Foo"', 'bar;'].join('\n'));
110+
const value = resolveToValue(path);
106111

107-
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
112+
expect(Array.isArray(value.value)).toBe(false);
113+
expect(value.node.type).toBe('ImportDeclaration');
108114
});
109115

110116
it('resolves namespace import references to the import declaration', () => {
111117
const path = parse(['import * as bar from "Foo"', 'bar;'].join('\n'));
118+
const value = resolveToValue(path);
112119

113-
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
120+
expect(Array.isArray(value.value)).toBe(false);
121+
expect(value.node.type).toBe('ImportDeclaration');
114122
});
115123
});
116124

117125
describe('MemberExpression', () => {
118126
it("resolves a MemberExpression to it's init value", () => {
119127
const path = parse(['var foo = { bar: 1 };', 'foo.bar;'].join('\n'));
120128

121-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(1));
129+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(1));
122130
});
123131

124132
it('resolves a MemberExpression in the scope chain', () => {
125133
const path = parse(
126134
['var foo = 1;', 'var bar = { baz: foo };', 'bar.baz;'].join('\n'),
127135
);
128136

129-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(1));
137+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(1));
130138
});
131139

132140
it('resolves a nested MemberExpression in the scope chain', () => {
@@ -138,7 +146,7 @@ describe('resolveToValue', () => {
138146
].join('\n'),
139147
);
140148

141-
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(1));
149+
expect(resolveToValue(path)).toEqualASTNode(builders.literal(1));
142150
});
143151

144152
it('returns the last resolvable MemberExpression', () => {
@@ -150,7 +158,7 @@ describe('resolveToValue', () => {
150158
].join('\n'),
151159
);
152160

153-
expect(resolveToValue(path).node).toEqualASTNode(
161+
expect(resolveToValue(path)).toEqualASTNode(
154162
builders.memberExpression(
155163
builders.identifier('foo'),
156164
builders.identifier('bar'),
@@ -163,7 +171,7 @@ describe('resolveToValue', () => {
163171
['var foo = {};', 'foo.bar = 1;', 'foo.bar;'].join('\n'),
164172
);
165173

166-
expect(resolveToValue(path).node).toEqualASTNode(path.node);
174+
expect(resolveToValue(path)).toEqualASTNode(path);
167175
});
168176
});
169177
});

src/utils/resolveToValue.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ export default function resolveToValue(path: NodePath): NodePath {
135135
types.ImportNamespaceSpecifier.check(node) ||
136136
types.ImportSpecifier.check(node)
137137
) {
138-
return path.parentPath;
138+
// go up two levels as first level is only the array of specifiers
139+
return path.parentPath.parentPath;
139140
} else if (types.AssignmentExpression.check(node)) {
140141
if (node.operator === '=') {
141142
return resolveToValue(path.get('right'));

tests/setupTestFramework.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ const matchers = {
1717
);
1818
}
1919

20+
// Use value here instead of node, as node has some magic and always returns
21+
// the next Node it finds even if value is an array
22+
const receivedNode = received.value;
23+
let expectedNode = expected;
24+
if (expected instanceof recast.types.NodePath) {
25+
expectedNode = expected.value;
26+
}
27+
2028
return {
21-
pass: recast.types.astNodesAreEquivalent(received, expected),
29+
pass: recast.types.astNodesAreEquivalent(receivedNode, expectedNode),
2230
message: () => {
23-
const diffString = diff(expected, received);
31+
const diffString = diff(expectedNode, receivedNode);
2432

2533
return (
2634
'Expected value to be (using ast-types):\n' +
27-
` ${utils.printExpected(expected)}\n` +
35+
` ${utils.printExpected(expectedNode)}\n` +
2836
'Received:\n' +
29-
` ${utils.printReceived(received)}` +
37+
` ${utils.printReceived(receivedNode)}` +
3038
(diffString ? `\n\nDifference:\n\n${diffString}` : '')
3139
);
3240
},

0 commit comments

Comments
 (0)