@@ -2,7 +2,8 @@ import path from 'path';
2
2
import fs from 'fs' ;
3
3
import { TagProps , TagParamObject , DocumentationObject , utils , TagObject } from 'react-docgen' ;
4
4
import _ from 'lodash' ;
5
- import doctrine , { Annotation } from 'doctrine' ;
5
+ import parse , { Tag } from 'comment-parser' ;
6
+
6
7
import createLogger from 'glogg' ;
7
8
import highlightCodeInMarkdown from './highlightCodeInMarkdown' ;
8
9
import removeDoclets from './removeDoclets' ;
@@ -27,8 +28,20 @@ const JS_DOC_ALL_SYNONYMS: (keyof TagProps)[] = [
27
28
// and https://github.com/styleguidist/react-styleguidist/issues/298
28
29
const getDocletsObject = ( str ?: string ) => ( { ...utils . docblock . getDoclets ( str ) } ) ;
29
30
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
+ ) ;
32
45
} ;
33
46
34
47
const doesExternalExampleFileExist = ( componentPath : string , exampleFile : string ) => {
@@ -59,41 +72,34 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
59
72
const outDocs : Rsg . TempPropsObject = { doclets : { } , displayName : '' , ...doc , methods : undefined } ;
60
73
61
74
// Keep only public methods
62
- outDocs . methods = ( doc . methods || [ ] ) . filter ( method => {
75
+ outDocs . methods = ( doc . methods || [ ] ) . filter ( ( method ) => {
63
76
const doclets = method . docblock && utils . docblock . getDoclets ( method . docblock ) ;
64
77
return doclets && doclets . public ;
65
78
} ) as Rsg . MethodWithDocblock [ ] ;
66
79
67
80
// Parse the docblock of the remaining methods with doctrine to retrieve
68
81
// the JSDoc tags
69
82
// 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 ] ) ;
74
85
75
86
// Merge with react-docgen information about arguments and return value
76
87
// with information from JSDoc
77
88
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 [ ] ;
82
90
const params =
83
91
method . params &&
84
- method . params . map ( param => ( {
92
+ method . params . map ( ( param ) => ( {
85
93
...param ,
86
- ...paramTags . find ( tagParam => tagParam . name === param . name ) ,
94
+ ...paramTags . find ( ( tagParam ) => tagParam . name === param . name ) ,
87
95
} ) ) ;
88
96
89
97
if ( params ) {
90
98
method . params = params ;
91
99
}
92
100
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 } ) ;
97
103
const returns = method . returns
98
104
? {
99
105
...method . returns ,
@@ -102,7 +108,16 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
102
108
...method . returns . type ,
103
109
} ,
104
110
}
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 ;
106
121
107
122
if ( returns ) {
108
123
method . returns = returns ;
@@ -118,8 +133,8 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
118
133
// Read doclets from the description and remove them
119
134
outDocs . doclets = getDocletsObject ( doc . description ) ;
120
135
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 ] ) ;
123
138
124
139
outDocs . description = highlightCodeInMarkdown ( removeDoclets ( doc . description ) ) ;
125
140
@@ -142,19 +157,18 @@ export default function getProps(doc: DocumentationObject, filepath?: string): R
142
157
143
158
if ( doc . props ) {
144
159
// Read doclets of props
145
- Object . keys ( doc . props ) . forEach ( propName => {
160
+ Object . keys ( doc . props ) . forEach ( ( propName ) => {
146
161
if ( ! doc . props ) {
147
162
return ;
148
163
}
149
164
const prop = doc . props [ propName ] ;
150
165
const doclets = getDocletsObject ( prop . description ) ;
151
-
152
166
// 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 } */` || '' ) ;
154
168
155
169
// 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 ] ) ;
158
172
159
173
// Remove ignored props
160
174
if ( doclets && doclets . ignore && outDocs . props ) {
0 commit comments