Skip to content

Commit 7df84e8

Browse files
author
Gerard Brull
committed
WIP
1 parent 669a917 commit 7df84e8

File tree

4 files changed

+143
-27
lines changed

4 files changed

+143
-27
lines changed

package-lock.json

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"clean-webpack-plugin": "^3.0.0",
3838
"clipboard-copy": "^3.1.0",
3939
"clsx": "^1.0.4",
40+
"comment-parser": "^0.7.6",
4041
"common-dir": "^3.0.0",
4142
"copy-webpack-plugin": "^6.1.0",
4243
"core-js": "^3.6.4",
@@ -54,6 +55,7 @@
5455
"hash-sum": "^2.0.0",
5556
"is-directory": "^0.3.1",
5657
"javascript-stringify": "^2.0.0",
58+
"jsdoc-parse": "^5.0.0",
5759
"jss": "^10.0.0",
5860
"jss-plugin-camel-case": "^10.0.0",
5961
"jss-plugin-compose": "^10.0.0",

src/client/rsg-components/Argument/ArgumentRenderer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Markdown from 'rsg-components/Markdown';
55
import Name from 'rsg-components/Name';
66
import Type from 'rsg-components/Type';
77
import Group from 'react-group';
8-
import doctrine from 'doctrine';
8+
import doctrine from 'comment-parser';
99
import * as Rsg from '../../../typings';
1010

1111
export const styles = ({ space }: Rsg.Theme) => ({

src/loaders/utils/getProps.ts

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from 'path';
22
import fs from 'fs';
33
import { TagProps, TagParamObject, DocumentationObject, utils, TagObject } from 'react-docgen';
44
import _ from 'lodash';
5-
import doctrine, { Annotation } from 'doctrine';
5+
import parse, { Tag } from 'comment-parser';
6+
67
import createLogger from 'glogg';
78
import highlightCodeInMarkdown from './highlightCodeInMarkdown';
89
import removeDoclets from './removeDoclets';
@@ -27,8 +28,20 @@ const JS_DOC_ALL_SYNONYMS: (keyof TagProps)[] = [
2728
// and https://github.com/styleguidist/react-styleguidist/issues/298
2829
const getDocletsObject = (str?: string) => ({ ...utils.docblock.getDoclets(str) });
2930

30-
const getDoctrineTags = (documentation: Annotation) => {
31-
return _.groupBy(documentation.tags, 'title');
31+
const getDoctrineTags = (documentation: parse.Comment) => {
32+
console.log(documentation);
33+
return _.groupBy(
34+
documentation.tags
35+
.filter((tag) => tag.tag !== 'return')
36+
.reduce((acc, tag) => {
37+
acc[tag.tag] = {
38+
description: tag.description || null,
39+
title: tag.tag,
40+
};
41+
return acc;
42+
}, {}),
43+
'title'
44+
);
3245
};
3346

3447
const doesExternalExampleFileExist = (componentPath: string, exampleFile: string) => {
@@ -59,41 +72,34 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
5972
const outDocs: Rsg.TempPropsObject = { doclets: {}, displayName: '', ...doc, methods: undefined };
6073

6174
// Keep only public methods
62-
outDocs.methods = (doc.methods || []).filter(method => {
75+
outDocs.methods = (doc.methods || []).filter((method) => {
6376
const doclets = method.docblock && utils.docblock.getDoclets(method.docblock);
6477
return doclets && doclets.public;
6578
}) as Rsg.MethodWithDocblock[];
6679

6780
// Parse the docblock of the remaining methods with doctrine to retrieve
6881
// the JSDoc tags
6982
// if a method is visible it must have a docblock
70-
outDocs.methods = outDocs.methods.map(method => {
71-
const allTags = getDoctrineTags(
72-
doctrine.parse(method.docblock, { sloppy: true, unwrap: true })
73-
);
83+
outDocs.methods = outDocs.methods.map((method) => {
84+
const allTags = getDoctrineTags(parse(`/** ${method.docblock} */`)[0]);
7485

7586
// Merge with react-docgen information about arguments and return value
7687
// with information from JSDoc
7788

78-
const paramTags = getMergedTag(
79-
allTags as TagProps,
80-
JS_DOC_METHOD_PARAM_TAG_SYNONYMS
81-
) as TagParamObject[];
89+
const paramTags = getMergedTag(allTags, JS_DOC_METHOD_PARAM_TAG_SYNONYMS) as TagParamObject[];
8290
const params =
8391
method.params &&
84-
method.params.map(param => ({
92+
method.params.map((param) => ({
8593
...param,
86-
...paramTags.find(tagParam => tagParam.name === param.name),
94+
...paramTags.find((tagParam) => tagParam.name === param.name),
8795
}));
8896

8997
if (params) {
9098
method.params = params;
9199
}
92100

93-
const returnTags = getMergedTag(
94-
allTags as TagProps,
95-
JS_DOC_METHOD_RETURN_TAG_SYNONYMS
96-
) as TagParamObject[];
101+
const returnTags = getMergedTag(allTags, JS_DOC_METHOD_RETURN_TAG_SYNONYMS) as TagParamObject[];
102+
console.log({ returnTags });
97103
const returns = method.returns
98104
? {
99105
...method.returns,
@@ -102,7 +108,16 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
102108
...method.returns.type,
103109
},
104110
}
105-
: returnTags[0];
111+
: returnTags[0]
112+
? {
113+
description: returnTags[0].name,
114+
title: returnTags[0].tag,
115+
type: {
116+
name: returnTags[0].type,
117+
type: 'NameExpression',
118+
},
119+
}
120+
: undefined;
106121

107122
if (returns) {
108123
method.returns = returns;
@@ -118,8 +133,8 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
118133
// Read doclets from the description and remove them
119134
outDocs.doclets = getDocletsObject(doc.description);
120135

121-
const documentation = doctrine.parse(doc.description);
122-
outDocs.tags = getDoctrineTags(documentation) as TagProps;
136+
const documentation = parse(doc.description);
137+
outDocs.tags = getDoctrineTags(documentation[0]);
123138

124139
outDocs.description = highlightCodeInMarkdown(removeDoclets(doc.description));
125140

@@ -142,19 +157,18 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
142157

143158
if (doc.props) {
144159
// Read doclets of props
145-
Object.keys(doc.props).forEach(propName => {
160+
Object.keys(doc.props).forEach((propName) => {
146161
if (!doc.props) {
147162
return;
148163
}
149164
const prop = doc.props[propName];
150165
const doclets = getDocletsObject(prop.description);
151-
152166
// When a prop is listed in defaultProps but not in props the prop.description is undefined
153-
const documentation = doctrine.parse(prop.description || '');
167+
const documentation = parse(`/** ${prop.description} */` || '');
154168

155169
// documentation.description is the description without tags
156-
prop.description = documentation.description;
157-
prop.tags = getDoctrineTags(documentation) as TagProps;
170+
prop.description = documentation.length ? documentation[0].description : '';
171+
prop.tags = getDoctrineTags(documentation[0]);
158172

159173
// Remove ignored props
160174
if (doclets && doclets.ignore && outDocs.props) {

0 commit comments

Comments
 (0)