From a1e86c208983285b3a8fa784b53e4c48ef272a22 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Sun, 7 Jun 2020 00:59:44 -0700 Subject: [PATCH] feat: Add test and implementation to get meta for shape types --- src/utils/__tests__/propTypeInfo.js | 57 +++++++++++++++++++++++++++++ src/utils/propTypeInfo.js | 16 ++++++-- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/propTypeInfo.js b/src/utils/__tests__/propTypeInfo.js index 87f7f866e..f569a4c34 100644 --- a/src/utils/__tests__/propTypeInfo.js +++ b/src/utils/__tests__/propTypeInfo.js @@ -303,4 +303,61 @@ describe('getTypeMeta', () => { params: [{ description: '', name: 'event', type: 'Event' }], }); }); + + test('returns nested prop type info for shape types', () => { + const propType = createPropType('shape', [ + { + pathname: createPropType('string', undefined, { isRequired: true }), + search: createPropType('string'), + hash: createPropType('string'), + }, + ]); + + const component = { + propTypes: { + to: propType, + }, + }; + + expect(getTypeMeta('to', propType, { component })).toEqual({ + types: [ + { + name: 'pathname', + defaultValue: undefined, + description: undefined, + deprecation: null, + isRequired: true, + type: { + meta: null, + raw: 'string', + name: 'string', + }, + }, + { + name: 'search', + defaultValue: undefined, + description: undefined, + deprecation: null, + isRequired: false, + type: { + meta: null, + raw: 'string', + name: 'string', + }, + }, + { + name: 'hash', + defaultValue: undefined, + description: undefined, + deprecation: null, + isRequired: false, + type: { + meta: null, + raw: 'string', + name: 'string', + }, + }, + ], + }); + }); }); diff --git a/src/utils/propTypeInfo.js b/src/utils/propTypeInfo.js index e2151164b..fd5615df0 100644 --- a/src/utils/propTypeInfo.js +++ b/src/utils/propTypeInfo.js @@ -101,6 +101,15 @@ export const getTypeMeta = (name, propType, { component }) => { returnValue: propTypeDocs.tags?.returnValue ?? { type: 'undefined' }, params: propTypeDocs.tags?.param, }; + case 'shape': { + const [shape] = getArgs(propType); + + return { + types: Object.entries(shape).map(([name, propType]) => + getPropTypeDefinition(component, name, propType) + ), + }; + } default: return null; } @@ -113,10 +122,11 @@ export const getPropTypeDefinition = (component, name, propType) => { return { name, defaultValue: getDefaultValue(component, name), - description: propDocs.text, - deprecation: propDocs.tags.deprecated?.[0] ?? null, - isRequired: propMeta.some((item) => item.name === 'isRequired'), + description: propDocs?.text, + deprecation: propDocs?.tags?.deprecated?.[0] ?? null, + isRequired: propMeta?.some((item) => item.name === 'isRequired') ?? false, type: { + meta: getTypeMeta(name, propType, { component }), raw: getRawTypeName(propType), name: getNormalizedTypeName(propType), },