From 1ab18cf7c230822c409be5f8a96c989614006013 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 30 Mar 2021 12:45:21 +1100 Subject: [PATCH] Add story and fix for #14377 --- .../js-class-component/docgen.snapshot | 11 ++++- .../js-class-component/input.js | 2 + addons/docs/src/lib/docgen/createPropDef.ts | 40 +++++++++++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/docgen.snapshot b/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/docgen.snapshot index 21c0ef51b307..1db7dceec9b5 100644 --- a/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/docgen.snapshot +++ b/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/docgen.snapshot @@ -50,7 +50,9 @@ PropsWriter.defaultProps = { localReference: local, importedReference: imported, globalReference: Date, - stringGlobalName: 'top' + stringGlobalName: 'top', + // eslint-disable-next-line react/default-props-match-prop-types + stringNoPropType: 'stringNoPropType' }; export const component = PropsWriter; PropsWriter.__docgenInfo = { @@ -184,6 +186,13 @@ PropsWriter.__docgenInfo = { \\"required\\": false, \\"description\\": \\"\\" }, + \\"stringNoPropType\\": { + \\"defaultValue\\": { + \\"value\\": \\"'stringNoPropType'\\", + \\"computed\\": false + }, + \\"required\\": false + }, \\"numberRequired\\": { \\"type\\": { \\"name\\": \\"number\\" diff --git a/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/input.js b/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/input.js index 55bc100c820e..2a20097cf217 100644 --- a/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/input.js +++ b/addons/docs/src/frameworks/react/__testfixtures__/js-class-component/input.js @@ -48,6 +48,8 @@ PropsWriter.defaultProps = { importedReference: imported, globalReference: Date, stringGlobalName: 'top', + // eslint-disable-next-line react/default-props-match-prop-types + stringNoPropType: 'stringNoPropType', }; export const component = PropsWriter; diff --git a/addons/docs/src/lib/docgen/createPropDef.ts b/addons/docs/src/lib/docgen/createPropDef.ts index d663b2d668a3..3916a3aeff35 100644 --- a/addons/docs/src/lib/docgen/createPropDef.ts +++ b/addons/docs/src/lib/docgen/createPropDef.ts @@ -18,27 +18,43 @@ function createType(type: DocgenType) { return type != null ? createSummaryValue(type.name) : null; } +// A heuristic to tell if a defaultValue comes from RDT +function isReactDocgenTypescript(defaultValue: DocgenPropDefaultValue) { + const { computed, func } = defaultValue; + return typeof computed === 'undefined' && typeof func === 'undefined'; +} + +function isStringValued(type?: DocgenType) { + if (!type) { + return false; + } + + if (type.name === 'string') { + return true; + } + + if (type.name === 'enum') { + return ( + Array.isArray(type.value) && + type.value.every( + ({ value: tv }) => typeof tv === 'string' && tv[0] === '"' && tv[tv.length - 1] === '"' + ) + ); + } + return false; +} + function createDefaultValue( defaultValue: DocgenPropDefaultValue, type: DocgenType ): PropDefaultValue { if (defaultValue != null) { - const { value, computed, func } = defaultValue; + const { value } = defaultValue; if (!isDefaultValueBlacklisted(value)) { - const isReactDocgenTypescript = - typeof computed === 'undefined' && typeof func === 'undefined'; - const isStringValued = - type.name === 'string' || - (type.name === 'enum' && - Array.isArray(type.value) && - type.value.every( - ({ value: tv }) => typeof tv === 'string' && tv[0] === '"' && tv[tv.length - 1] === '"' - )); - // Work around a bug in `react-docgen-typescript-loader`, which returns 'string' for a string // default, instead of "'string'" -- which is incorrect - if (isReactDocgenTypescript && isStringValued) { + if (isReactDocgenTypescript(defaultValue) && isStringValued(type)) { return createSummaryValue(JSON.stringify(value)); }