23
23
Pathname ,
24
24
Return ,
25
25
TopLevel ,
26
+ Type ,
26
27
TypeParam ,
28
+ TypeXRef ,
27
29
)
28
30
from .jsdoc import Analyzer as JsAnalyzer
29
31
from .parsers import PathVisitor
@@ -205,16 +207,51 @@ def _formal_params(self, obj: Function | Class) -> str:
205
207
206
208
return "({})" .format (", " .join (formals ))
207
209
210
+ def format_type (self , type : Type , escape : bool = False ) -> str :
211
+ if not type :
212
+ return ""
213
+ if isinstance (type , str ):
214
+ return self .format_type ([type ])
215
+ it = iter (type )
216
+
217
+ def strs () -> Iterator [str ]:
218
+ for elem in it :
219
+ if isinstance (elem , str ):
220
+ yield elem
221
+ else :
222
+ xref .append (elem )
223
+ return
224
+
225
+ res = []
226
+ while True :
227
+ xref : list [TypeXRef ] = []
228
+ s = "" .join (strs ())
229
+ if escape :
230
+ s = rst .escape (s )
231
+ res .append (s )
232
+ if not xref :
233
+ break
234
+ res .append (self .render_xref (xref [0 ], escape ))
235
+
236
+ return "" .join (res )
237
+
238
+ def render_xref (self , s : TypeXRef , escape : bool = False ) -> str :
239
+ if escape :
240
+ return rst .escape (s .name )
241
+ return s .name
242
+
208
243
def _return_formatter (self , return_ : Return ) -> tuple [list [str ], str ]:
209
244
"""Derive heads and tail from ``@returns`` blocks."""
210
- tail = ("**%s** -- " % rst .escape (return_ .type )) if return_ .type else ""
245
+ tail = ""
246
+ if return_ .type :
247
+ tail += "**%s** -- " % self .format_type (return_ .type , escape = True )
211
248
tail += return_ .description
212
249
return ["returns" ], tail
213
250
214
251
def _type_param_formatter (self , tparam : TypeParam ) -> tuple [list [str ], str ] | None :
215
252
v = tparam .name
216
253
if tparam .extends :
217
- v += f " extends { tparam .extends } "
254
+ v += " extends " + self . format_type ( tparam .extends )
218
255
heads = ["typeparam" , v ]
219
256
return heads , tparam .description
220
257
@@ -225,8 +262,9 @@ def _param_formatter(self, param: Param) -> tuple[list[str], str] | None:
225
262
return None
226
263
heads = ["param" ]
227
264
if param .type :
228
- heads .append (param .type )
265
+ heads .append (self . format_type ( param .type ) )
229
266
heads .append (param .name )
267
+
230
268
tail = param .description
231
269
return heads , tail
232
270
@@ -235,14 +273,14 @@ def _param_type_formatter(self, param: Param) -> tuple[list[str], str] | None:
235
273
if not param .type :
236
274
return None
237
275
heads = ["type" , param .name ]
238
- tail = rst . escape (param .type )
276
+ tail = self . format_type (param .type )
239
277
return heads , tail
240
278
241
279
def _exception_formatter (self , exception : Exc ) -> tuple [list [str ], str ]:
242
280
"""Derive heads and tail from ``@throws`` blocks."""
243
281
heads = ["throws" ]
244
282
if exception .type :
245
- heads .append (exception .type )
283
+ heads .append (self . format_type ( exception .type ) )
246
284
tail = exception .description
247
285
return heads , tail
248
286
@@ -453,7 +491,7 @@ def _template_vars(self, name: str, obj: Attribute) -> dict[str, Any]: # type:
453
491
is_optional = obj .is_optional ,
454
492
see_also = obj .see_alsos ,
455
493
examples = obj .examples ,
456
- type = obj .type ,
494
+ type = self . format_type ( obj .type ) ,
457
495
content = "\n " .join (self ._content ),
458
496
)
459
497
0 commit comments