aboutsummaryrefslogtreecommitdiffstats
path: root/packages/react-docs/src/utils/typedoc_utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/react-docs/src/utils/typedoc_utils.ts')
-rw-r--r--packages/react-docs/src/utils/typedoc_utils.ts71
1 files changed, 63 insertions, 8 deletions
diff --git a/packages/react-docs/src/utils/typedoc_utils.ts b/packages/react-docs/src/utils/typedoc_utils.ts
index e4cea1e40..02f5b4049 100644
--- a/packages/react-docs/src/utils/typedoc_utils.ts
+++ b/packages/react-docs/src/utils/typedoc_utils.ts
@@ -15,6 +15,7 @@ import {
TypeDocNode,
TypeDocType,
TypeParameter,
+ TypescriptFunction,
TypescriptMethod,
} from '../types';
import { utils } from '../utils/utils';
@@ -81,17 +82,22 @@ export const typeDocUtils = {
}
}
- // Since the `types.ts` file is the only file that does not export a module/class but
- // instead has each type export itself, we do not need to go down two levels of nesting
- // for it.
let entities;
let packageComment = '';
- if (sectionName === docsInfo.sections.types) {
- entities = packageDefinitionWithMergedChildren.children;
- } else {
+ // HACK: We assume 1 exported class per file
+ const classChildren = _.filter(packageDefinitionWithMergedChildren.children, (child: TypeDocNode) => {
+ return child.kindString === KindString.Class;
+ });
+ if (classChildren.length > 1 && sectionName !== 'types') {
+ throw new Error('`react-docs` only supports projects with 1 exported class per file');
+ }
+ const isClassExport = packageDefinitionWithMergedChildren.children[0].kindString === KindString.Class;
+ if (isClassExport) {
entities = packageDefinitionWithMergedChildren.children[0].children;
const commentObj = packageDefinitionWithMergedChildren.children[0].comment;
packageComment = !_.isUndefined(commentObj) ? commentObj.shortText : packageComment;
+ } else {
+ entities = packageDefinitionWithMergedChildren.children;
}
const docSection = typeDocUtils._convertEntitiesToDocSection(entities, docsInfo, sectionName);
@@ -105,6 +111,7 @@ export const typeDocUtils = {
comment: '',
constructors: [],
methods: [],
+ functions: [],
properties: [],
types: [],
};
@@ -124,6 +131,13 @@ export const typeDocUtils = {
docSection.constructors.push(constructor);
break;
+ case KindString.Function:
+ if (entity.flags.isExported) {
+ const func = typeDocUtils._convertFunction(entity, docsInfo.sections, sectionName, docsInfo.id);
+ docSection.functions.push(func);
+ }
+ break;
+
case KindString.Method:
if (entity.flags.isPublic) {
isConstructor = false;
@@ -151,7 +165,6 @@ export const typeDocUtils = {
break;
case KindString.Interface:
- case KindString.Function:
case KindString.Variable:
case KindString.Enumeration:
case KindString.TypeAlias:
@@ -162,10 +175,20 @@ export const typeDocUtils = {
sectionName,
docsInfo.id,
);
- docSection.types.push(customType);
+ const seenTypeNames = _.map(docSection.types, t => t.name);
+ const isUnseen = !_.includes(seenTypeNames, customType.name);
+ if (isUnseen) {
+ docSection.types.push(customType);
+ }
}
break;
+ case KindString.Class:
+ // We currently do not support more then a single class per file
+ // except for the types section, where we ignore classes since we
+ // only want to render type definitions.
+ break;
+
default:
throw utils.spawnSwitchErr('kindString', entity.kindString);
}
@@ -303,6 +326,38 @@ export const typeDocUtils = {
};
return method;
},
+ _convertFunction(
+ entity: TypeDocNode,
+ sections: SectionsMap,
+ sectionName: string,
+ docId: string,
+ ): TypescriptFunction {
+ const signature = entity.signatures[0];
+ const source = entity.sources[0];
+ const hasComment = !_.isUndefined(signature.comment);
+
+ const parameters = _.map(signature.parameters, param => {
+ return typeDocUtils._convertParameter(param, sections, sectionName, docId);
+ });
+ const returnType = typeDocUtils._convertType(signature.type, sections, sectionName, docId);
+ const typeParameter = _.isUndefined(signature.typeParameter)
+ ? undefined
+ : typeDocUtils._convertTypeParameter(signature.typeParameter[0], sections, sectionName, docId);
+
+ const func = {
+ name: signature.name,
+ comment: hasComment ? signature.comment.shortText : undefined,
+ returnComment: hasComment && signature.comment.returns ? signature.comment.returns : undefined,
+ source: {
+ fileName: source.fileName,
+ line: source.line,
+ },
+ parameters,
+ returnType,
+ typeParameter,
+ };
+ return func;
+ },
_convertTypeParameter(
entity: TypeDocNode,
sections: SectionsMap,