Workaround bug in react-docgen-typescript with string values

This commit is contained in:
Tom Coleman 2021-02-17 19:24:48 +11:00
parent 5a61260abc
commit e189c89f0a
2 changed files with 13 additions and 3 deletions

View File

@ -18,11 +18,20 @@ function createType(type: DocgenType) {
return type != null ? createSummaryValue(type.name) : null;
}
function createDefaultValue(defaultValue: DocgenPropDefaultValue): PropDefaultValue {
function createDefaultValue(
defaultValue: DocgenPropDefaultValue,
type: DocgenType
): PropDefaultValue {
if (defaultValue != null) {
const { value } = defaultValue;
const { value, computed } = defaultValue;
if (!isDefaultValueBlacklisted(value)) {
// Work around a bug in `react-docgen-typescript-loader`, which returns 'string' for a string
// default, instead of "'string'" -- which is incorrect (PR to RDT to follow)
if (typeof computed === 'undefined' && type.name === 'string') {
return createSummaryValue(JSON.stringify(value));
}
return createSummaryValue(value);
}
}
@ -38,7 +47,7 @@ function createBasicPropDef(name: string, type: DocgenType, docgenInfo: DocgenIn
type: createType(type),
required,
description,
defaultValue: createDefaultValue(defaultValue),
defaultValue: createDefaultValue(defaultValue, type),
};
}

View File

@ -32,6 +32,7 @@ export interface DocgenTypeScriptType extends DocgenType {}
export interface DocgenPropDefaultValue {
value: string;
computed?: boolean;
}
export interface DocgenInfo {