Skip to content

Commit

Permalink
Add story and fix for #14377
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Mar 30, 2021
1 parent 7c274e1 commit 1ab18cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -184,6 +186,13 @@ PropsWriter.__docgenInfo = {
\\"required\\": false,
\\"description\\": \\"\\"
},
\\"stringNoPropType\\": {
\\"defaultValue\\": {
\\"value\\": \\"'stringNoPropType'\\",
\\"computed\\": false
},
\\"required\\": false
},
\\"numberRequired\\": {
\\"type\\": {
\\"name\\": \\"number\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
40 changes: 28 additions & 12 deletions addons/docs/src/lib/docgen/createPropDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 1ab18cf

Please sign in to comment.