Skip to content

Commit 52ae1f4

Browse files
committed
feat: Add support for PropTypes.exact
1 parent e203cfd commit 52ae1f4

File tree

7 files changed

+358
-111
lines changed

7 files changed

+358
-111
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ Object {
7878
"value": "Child.propTypes",
7979
},
8080
},
81+
"childExact": Object {
82+
"description": "",
83+
"required": true,
84+
"type": Object {
85+
"computed": true,
86+
"name": "exact",
87+
"value": "Child.propTypes",
88+
},
89+
},
8190
"extendedChild": Object {
8291
"description": "",
8392
"required": true,
@@ -91,6 +100,19 @@ Object {
91100
},
92101
},
93102
},
103+
"extendedChildExact": Object {
104+
"description": "",
105+
"required": true,
106+
"type": Object {
107+
"name": "exact",
108+
"value": Object {
109+
"adopted": Object {
110+
"name": "bool",
111+
"required": true,
112+
},
113+
},
114+
},
115+
},
94116
"something": Object {
95117
"description": "",
96118
"required": true,
@@ -896,6 +918,24 @@ Object {
896918
"displayName": "Foo",
897919
"methods": Array [],
898920
"props": Object {
921+
"exactProp": Object {
922+
"description": "",
923+
"required": false,
924+
"type": Object {
925+
"name": "exact",
926+
"value": Object {
927+
"c": Object {
928+
"description": "Comment for property c",
929+
"name": "string",
930+
"required": false,
931+
},
932+
"d": Object {
933+
"name": "number",
934+
"required": false,
935+
},
936+
},
937+
},
938+
},
899939
"oneOfTypeProp": Object {
900940
"description": "",
901941
"required": false,

src/__tests__/fixtures/component_17.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Foo.propTypes = {
2525
a: PropTypes.string,
2626
b: PropTypes.number
2727
}),
28+
exactProp: PropTypes.exact({
29+
/** Comment for property c */
30+
c: PropTypes.string,
31+
d: PropTypes.number
32+
}),
2833
oneOfTypeProp: PropTypes.oneOfType([
2934
/** Comment for type string */
3035
PropTypes.string,

src/__tests__/fixtures/component_4.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class Parent extends React.Component {
2323
extendedChild: pt.shape({
2424
...Child.propTypes,
2525
adopted: pt.bool.isRequired
26+
}).isRequired,
27+
childExact: pt.exact(Child.propTypes).isRequired,
28+
extendedChildExact: pt.exact({
29+
...Child.propTypes,
30+
adopted: pt.bool.isRequired
2631
}).isRequired
2732
};
2833
}

src/types.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ export type PropTypeDescriptor = {
2727
| 'symbol'
2828
| 'objectOf'
2929
| 'shape'
30+
| 'exact'
3031
| 'union',
3132
value?: any,
3233
raw?: string,
3334
computed?: boolean,
34-
// These are only needed for shape types.
35+
// These are only needed for shape/exact types.
3536
// Consider consolidating PropTypeDescriptor and PropDescriptor
3637
description?: string,
3738
required?: boolean,
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`getPropType detects custom validation functions for arrow function 1`] = `
4+
Object {
5+
"name": "custom",
6+
"raw": "() => {}",
7+
}
8+
`;
9+
10+
exports[`getPropType detects custom validation functions for function 1`] = `
11+
Object {
12+
"name": "custom",
13+
"raw": "(function() {})",
14+
}
15+
`;
16+
17+
exports[`getPropType detects descriptions on nested types in arrayOf 1`] = `
18+
Object {
19+
"description": "test2",
20+
"name": "arrayOf",
21+
"value": Object {
22+
"name": "string",
23+
},
24+
}
25+
`;
26+
27+
exports[`getPropType detects descriptions on nested types in exacts 1`] = `
28+
Object {
29+
"name": "exact",
30+
"value": Object {
31+
"bar": Object {
32+
"description": "test2",
33+
"name": "bool",
34+
"required": false,
35+
},
36+
"foo": Object {
37+
"description": "test1",
38+
"name": "string",
39+
"required": false,
40+
},
41+
},
42+
}
43+
`;
44+
45+
exports[`getPropType detects descriptions on nested types in objectOf 1`] = `
46+
Object {
47+
"description": "test2",
48+
"name": "objectOf",
49+
"value": Object {
50+
"name": "string",
51+
},
52+
}
53+
`;
54+
55+
exports[`getPropType detects descriptions on nested types in shapes 1`] = `
56+
Object {
57+
"name": "shape",
58+
"value": Object {
59+
"bar": Object {
60+
"description": "test2",
61+
"name": "bool",
62+
"required": false,
63+
},
64+
"foo": Object {
65+
"description": "test1",
66+
"name": "string",
67+
"required": false,
68+
},
69+
},
70+
}
71+
`;
72+
73+
exports[`getPropType detects required notations of nested types in exacts 1`] = `
74+
Object {
75+
"name": "exact",
76+
"value": Object {
77+
"bar": Object {
78+
"name": "bool",
79+
"required": false,
80+
},
81+
"foo": Object {
82+
"name": "string",
83+
"required": true,
84+
},
85+
},
86+
}
87+
`;
88+
89+
exports[`getPropType detects required notations of nested types in shapes 1`] = `
90+
Object {
91+
"name": "shape",
92+
"value": Object {
93+
"bar": Object {
94+
"name": "bool",
95+
"required": false,
96+
},
97+
"foo": Object {
98+
"name": "string",
99+
"required": true,
100+
},
101+
},
102+
}
103+
`;
104+
105+
exports[`getPropType resolve identifier to their values correctly resolves SpreadElements in arrays 1`] = `
106+
Object {
107+
"name": "enum",
108+
"value": Array [
109+
Object {
110+
"computed": false,
111+
"value": "\\"foo\\"",
112+
},
113+
Object {
114+
"computed": false,
115+
"value": "\\"bar\\"",
116+
},
117+
],
118+
}
119+
`;
120+
121+
exports[`getPropType resolve identifier to their values correctly resolves nested SpreadElements in arrays 1`] = `
122+
Object {
123+
"name": "enum",
124+
"value": Array [
125+
Object {
126+
"computed": false,
127+
"value": "\\"foo\\"",
128+
},
129+
Object {
130+
"computed": false,
131+
"value": "\\"bar\\"",
132+
},
133+
],
134+
}
135+
`;
136+
137+
exports[`getPropType resolve identifier to their values does not resolve computed values 1`] = `
138+
Object {
139+
"name": "enum",
140+
"value": Array [
141+
Object {
142+
"computed": false,
143+
"value": "\\"FOO\\"",
144+
},
145+
Object {
146+
"computed": false,
147+
"value": "\\"BAR\\"",
148+
},
149+
],
150+
}
151+
`;
152+
153+
exports[`getPropType resolve identifier to their values does not resolve external values 1`] = `
154+
Object {
155+
"computed": true,
156+
"name": "enum",
157+
"value": "TYPES",
158+
}
159+
`;
160+
161+
exports[`getPropType resolve identifier to their values resolves memberExpressions 1`] = `
162+
Object {
163+
"name": "enum",
164+
"value": Array [
165+
Object {
166+
"computed": false,
167+
"value": "\\"foo\\"",
168+
},
169+
Object {
170+
"computed": false,
171+
"value": "\\"bar\\"",
172+
},
173+
],
174+
}
175+
`;
176+
177+
exports[`getPropType resolve identifier to their values resolves simple identifier to their initialization value 1`] = `
178+
Object {
179+
"name": "enum",
180+
"value": Array [
181+
Object {
182+
"computed": false,
183+
"value": "\\"foo\\"",
184+
},
185+
Object {
186+
"computed": false,
187+
"value": "\\"bar\\"",
188+
},
189+
],
190+
}
191+
`;
192+
193+
exports[`getPropType resolve identifier to their values resolves simple identifier to their initialization value in array 1`] = `
194+
Object {
195+
"name": "enum",
196+
"value": Array [
197+
Object {
198+
"computed": false,
199+
"value": "\\"foo\\"",
200+
},
201+
Object {
202+
"computed": false,
203+
"value": "\\"bar\\"",
204+
},
205+
],
206+
}
207+
`;
208+
209+
exports[`getPropType resolve identifier to their values resolves variables to their values 1`] = `
210+
Object {
211+
"name": "shape",
212+
"value": Object {
213+
"bar": Object {
214+
"name": "string",
215+
"required": false,
216+
},
217+
},
218+
}
219+
`;

0 commit comments

Comments
 (0)