Skip to content

Commit ff33fc8

Browse files
authored
Handle computed properties correctly and do not fail generation (#340)
* Handle computed properties correctly and do not fail generation * Resolve identifiers and correctly use string/number literals * Fix lint
1 parent 8eca876 commit ff33fc8

25 files changed

+824
-89
lines changed

.eslintrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@ module.exports = {
2727
'no-unused-vars': 'off',
2828
},
2929
},
30+
{
31+
files: 'src/**/__tests__/*-test.js',
32+
env: { jest: true },
33+
},
3034
],
3135
};

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,24 @@ Object {
10271027
},
10281028
}
10291029
`;
1030+
1031+
exports[`main fixtures processes component "component_20.js" without errors 1`] = `
1032+
Object {
1033+
"description": "",
1034+
"displayName": "Button",
1035+
"methods": Array [],
1036+
"props": Object {
1037+
"@computed#children": Object {
1038+
"defaultValue": Object {
1039+
"computed": false,
1040+
"value": "\\"default\\"",
1041+
},
1042+
"description": "This is a test",
1043+
"required": false,
1044+
"type": Object {
1045+
"name": "string",
1046+
},
1047+
},
1048+
},
1049+
}
1050+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const Button = () => (
5+
<div></div>
6+
);
7+
8+
Button.propTypes = {
9+
/** This is a test */
10+
[children]: PropTypes.string.isRequired,
11+
};
12+
13+
Button.defaultProps = {
14+
[children]: "default",
15+
};
16+
17+
export default Button;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`componentMethodsHandler should handle and ignore computed methods 1`] = `
4+
Array [
5+
Object {
6+
"docblock": "The foo method",
7+
"modifiers": Array [],
8+
"name": "@computed#foo",
9+
"params": Array [
10+
Object {
11+
"name": "bar",
12+
"optional": undefined,
13+
"type": Object {
14+
"name": "number",
15+
},
16+
},
17+
],
18+
"returns": Object {
19+
"type": Object {
20+
"name": "number",
21+
},
22+
},
23+
},
24+
]
25+
`;
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are imported variables 1`] = `
4+
Object {
5+
"foo": Object {
6+
"defaultValue": Object {
7+
"computed": true,
8+
"value": "ImportedComponent",
9+
},
10+
},
11+
}
12+
`;
13+
14+
exports[`defaultPropsHandler ClassDeclaration with static defaultProps should find prop default values that are literals 1`] = `
15+
Object {
16+
"abc": Object {
17+
"defaultValue": Object {
18+
"computed": false,
19+
"value": "{xyz: abc.def, 123: 42}",
20+
},
21+
},
22+
"bar": Object {
23+
"defaultValue": Object {
24+
"computed": false,
25+
"value": "42",
26+
},
27+
},
28+
"baz": Object {
29+
"defaultValue": Object {
30+
"computed": false,
31+
"value": "[\\"foo\\", \\"bar\\"]",
32+
},
33+
},
34+
"foo": Object {
35+
"defaultValue": Object {
36+
"computed": false,
37+
"value": "\\"bar\\"",
38+
},
39+
},
40+
}
41+
`;
42+
43+
exports[`defaultPropsHandler ClassExpression with static defaultProps should find prop default values that are literals 1`] = `
44+
Object {
45+
"abc": Object {
46+
"defaultValue": Object {
47+
"computed": false,
48+
"value": "{xyz: abc.def, 123: 42}",
49+
},
50+
},
51+
"bar": Object {
52+
"defaultValue": Object {
53+
"computed": false,
54+
"value": "42",
55+
},
56+
},
57+
"baz": Object {
58+
"defaultValue": Object {
59+
"computed": false,
60+
"value": "[\\"foo\\", \\"bar\\"]",
61+
},
62+
},
63+
"foo": Object {
64+
"defaultValue": Object {
65+
"computed": false,
66+
"value": "\\"bar\\"",
67+
},
68+
},
69+
}
70+
`;
71+
72+
exports[`defaultPropsHandler Functional components with default params should find default props that are literals 1`] = `
73+
Object {
74+
"abc": Object {
75+
"defaultValue": Object {
76+
"computed": false,
77+
"value": "{xyz: abc.def, 123: 42}",
78+
},
79+
},
80+
"bar": Object {
81+
"defaultValue": Object {
82+
"computed": false,
83+
"value": "42",
84+
},
85+
},
86+
"baz": Object {
87+
"defaultValue": Object {
88+
"computed": false,
89+
"value": "[\\"foo\\", \\"bar\\"]",
90+
},
91+
},
92+
"foo": Object {
93+
"defaultValue": Object {
94+
"computed": false,
95+
"value": "\\"bar\\"",
96+
},
97+
},
98+
}
99+
`;
100+
101+
exports[`defaultPropsHandler Functional components with default params should find prop default values that are imported variables 1`] = `
102+
Object {
103+
"foo": Object {
104+
"defaultValue": Object {
105+
"computed": true,
106+
"value": "ImportedComponent",
107+
},
108+
},
109+
}
110+
`;
111+
112+
exports[`defaultPropsHandler Functional components with default params should override with defaultProps if available 1`] = `
113+
Object {
114+
"abc": Object {
115+
"defaultValue": Object {
116+
"computed": false,
117+
"value": "{xyz: abc.def, 123: 42}",
118+
},
119+
},
120+
"bar": Object {
121+
"defaultValue": Object {
122+
"computed": false,
123+
"value": "42",
124+
},
125+
},
126+
"baz": Object {
127+
"defaultValue": Object {
128+
"computed": false,
129+
"value": "[\\"foo\\", \\"bar\\"]",
130+
},
131+
},
132+
"foo": Object {
133+
"defaultValue": Object {
134+
"computed": false,
135+
"value": "\\"bar\\"",
136+
},
137+
},
138+
}
139+
`;
140+
141+
exports[`defaultPropsHandler Functional components with default params should work with aliases 1`] = `
142+
Object {
143+
"abc": Object {
144+
"defaultValue": Object {
145+
"computed": false,
146+
"value": "{xyz: abc.def, 123: 42}",
147+
},
148+
},
149+
"bar": Object {
150+
"defaultValue": Object {
151+
"computed": false,
152+
"value": "42",
153+
},
154+
},
155+
"baz": Object {
156+
"defaultValue": Object {
157+
"computed": false,
158+
"value": "[\\"foo\\", \\"bar\\"]",
159+
},
160+
},
161+
"foo": Object {
162+
"defaultValue": Object {
163+
"computed": false,
164+
"value": "\\"bar\\"",
165+
},
166+
},
167+
}
168+
`;
169+
170+
exports[`defaultPropsHandler Functional components with default params should work with no defaults 1`] = `Object {}`;
171+
172+
exports[`defaultPropsHandler ObjectExpression handles computed properties 1`] = `
173+
Object {
174+
"@computed#bar": Object {
175+
"defaultValue": Object {
176+
"computed": false,
177+
"value": "42",
178+
},
179+
},
180+
"foo": Object {
181+
"defaultValue": Object {
182+
"computed": false,
183+
"value": "\\"bar\\"",
184+
},
185+
},
186+
}
187+
`;
188+
189+
exports[`defaultPropsHandler ObjectExpression ignores complex computed properties 1`] = `
190+
Object {
191+
"foo": Object {
192+
"defaultValue": Object {
193+
"computed": false,
194+
"value": "\\"bar\\"",
195+
},
196+
},
197+
}
198+
`;
199+
200+
exports[`defaultPropsHandler ObjectExpression should find prop default values that are literals 1`] = `
201+
Object {
202+
"abc": Object {
203+
"defaultValue": Object {
204+
"computed": false,
205+
"value": "{xyz: abc.def, 123: 42}",
206+
},
207+
},
208+
"bar": Object {
209+
"defaultValue": Object {
210+
"computed": false,
211+
"value": "42",
212+
},
213+
},
214+
"baz": Object {
215+
"defaultValue": Object {
216+
"computed": false,
217+
"value": "[\\"foo\\", \\"bar\\"]",
218+
},
219+
},
220+
"foo": Object {
221+
"defaultValue": Object {
222+
"computed": false,
223+
"value": "\\"bar\\"",
224+
},
225+
},
226+
}
227+
`;
228+
229+
exports[`defaultPropsHandler should only consider Property nodes, not e.g. spread properties 1`] = `
230+
Object {
231+
"bar": Object {
232+
"defaultValue": Object {
233+
"computed": false,
234+
"value": "42",
235+
},
236+
},
237+
}
238+
`;

src/handlers/__tests__/__snapshots__/flowTypeHandler-test.js.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`flowTypeHandler TypeAlias class definition for flow <0.53 ignores hash map entry 1`] = `
4+
Object {
5+
"bar": Object {
6+
"description": "",
7+
"flowType": Object {},
8+
"required": false,
9+
},
10+
}
11+
`;
12+
13+
exports[`flowTypeHandler TypeAlias class definition for flow >=0.53 with State ignores hash map entry 1`] = `
14+
Object {
15+
"bar": Object {
16+
"description": "",
17+
"flowType": Object {},
18+
"required": false,
19+
},
20+
}
21+
`;
22+
23+
exports[`flowTypeHandler TypeAlias class definition for flow >=0.53 without State ignores hash map entry 1`] = `
24+
Object {
25+
"bar": Object {
26+
"description": "",
27+
"flowType": Object {},
28+
"required": false,
29+
},
30+
}
31+
`;
32+
33+
exports[`flowTypeHandler TypeAlias class definition with inline props ignores hash map entry 1`] = `
34+
Object {
35+
"bar": Object {
36+
"description": "",
37+
"flowType": Object {},
38+
"required": false,
39+
},
40+
}
41+
`;
42+
43+
exports[`flowTypeHandler TypeAlias stateless component ignores hash map entry 1`] = `
44+
Object {
45+
"bar": Object {
46+
"description": "",
47+
"flowType": Object {},
48+
"required": false,
49+
},
50+
}
51+
`;
52+
353
exports[`flowTypeHandler does support utility types inline 1`] = `
454
Object {
555
"foo": Object {

0 commit comments

Comments
 (0)