Skip to content

Commit ac12401

Browse files
gwyneplaineNoviny
authored andcommitted
[pretty-proptypes]: resolve thrown error on null values in members array (\w tests) (#96)
* resolve thrown error on null values in members array, add tests * add console.error to make the error trackable * add changeset
1 parent 57b4dbd commit ac12401

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "releases": [{ "name": "pretty-proptypes", "type": "patch" }], "dependents": [] }

.changeset/wet-masks-taste/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Work around added in object converter to not throw on null value in members list

packages/pretty-proptypes/src/PrettyConvert/converters.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ export const converters: { [string]: ?Function } = {
104104
if (type.members.length === 0) {
105105
return <components.Type>Object</components.Type>;
106106
}
107-
let simpleObj = type.members.filter(mem => !SIMPLE_TYPES.includes(mem.kind)).length === 0;
107+
let simpleObj = type.members.filter(mem => {
108+
if (mem === null) {
109+
/** if the member is null, error out */
110+
console.error(`null property in members of ${type.referenceIdName} of kind ${type.kind} `)
111+
return false;
112+
}
113+
return !SIMPLE_TYPES.includes(mem.kind)
114+
}).length === 0;
108115

109116
if (simpleObj) {
110117
return <components.Type>{convert(type)}</components.Type>;
@@ -114,7 +121,7 @@ export const converters: { [string]: ?Function } = {
114121
<span>
115122
<AddBrackets BracketStyler={components.TypeMeta} openBracket="{" closeBracket="}">
116123
<components.Indent>
117-
{type.members.map(prop => {
124+
{type.members.filter(p => p).map(prop => {
118125
if (prop.kind === 'spread') {
119126
const nestedObj = resolveFromGeneric(prop.value);
120127
// Spreads almost always resolve to an object, but they can
@@ -237,4 +244,5 @@ const prettyConvert = (type: K.AnyKind, components: Components, depth: number =
237244
return converter(type, components, depth);
238245
};
239246

247+
240248
export default prettyConvert;

packages/pretty-proptypes/src/PrettyConvert/converters.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,35 @@ test('resolve generic of array', () => {
122122
expect(wrapper.html().includes('string')).toBeTruthy();
123123
});
124124

125+
test('objects with null members do not throw', () => {
126+
const type = {
127+
kind: "object",
128+
members: [
129+
{
130+
kind: "property",
131+
optional: false,
132+
key: {
133+
kind: "id",
134+
name: "children"
135+
},
136+
value: {
137+
kind: "generic",
138+
value: {
139+
kind: "id",
140+
name: "React.ReactNode"
141+
}
142+
}
143+
},
144+
null
145+
],
146+
referenceIdName: "LabelTextProps"
147+
}
148+
expect(() => prettyConvert(type, components)).not.toThrow();
149+
})
150+
125151
test.skip('object with spread object', () => {
126152
let values = getSingleDefault(`{ ...something, c: 'something' }`);
127-
let wrapper = shallow(prettyConvert(values, components));
153+
let wrapper = shallow(prettyConvert(`{ ...something, c: 'something' }`, components));
128154
expect(wrapper).toBe('something');
129155
});
130156
test.skip('resolve generic of array with complex type', () => {

0 commit comments

Comments
 (0)