Skip to content

Commit 9a579ec

Browse files
authored
feat: parse typescript types from jsdoc comments (#783)
1 parent 6eb1fa4 commit 9a579ec

13 files changed

Lines changed: 835 additions & 571 deletions

File tree

js/types.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,36 +329,36 @@ export interface JsDocTagValued extends JsDocTagBase {
329329

330330
export interface JsDocTagTyped extends JsDocTagBase {
331331
kind: "enum" | "extends" | "this" | "type";
332-
type: string;
332+
ts_type: TsTypeDef;
333333
doc?: string;
334334
}
335335

336336
export interface JsDocTagNamedTyped extends JsDocTagBase {
337337
kind: "property" | "typedef";
338338
name: string;
339-
type: string;
339+
ts_type: TsTypeDef;
340340
doc?: string;
341341
}
342342

343343
export interface JsDocTagParam extends JsDocTagBase {
344344
kind: "param";
345345
name: string;
346-
type?: string;
346+
ts_type?: TsTypeDef;
347347
optional?: true;
348348
default?: string;
349349
doc?: string;
350350
}
351351

352352
export interface JsDocTagReturn extends JsDocTagBase {
353353
kind: "return";
354-
type?: string;
354+
ts_type?: TsTypeDef;
355355
doc?: string;
356356
}
357357

358358
export interface JsDocTagThrows extends JsDocTagBase {
359359
kind: "throws";
360-
type?: string;
361-
doc: string;
360+
ts_type?: TsTypeDef;
361+
doc?: string;
362362
}
363363

364364
export interface JsDocTagModule extends JsDocTagBase {

src/diff/js_doc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ fn tags_same_kind(a: &JsDocTag, b: &JsDocTag) -> bool {
106106
(Constructor, Constructor) => true,
107107
(Default { value: v1, .. }, Default { value: v2, .. }) => v1 == v2,
108108
(Deprecated { .. }, Deprecated { .. }) => true,
109-
(Enum { type_ref: t1, .. }, Enum { type_ref: t2, .. }) => t1 == t2,
109+
(Enum { ts_type: t1, .. }, Enum { ts_type: t2, .. }) => t1 == t2,
110110
(Example { .. }, Example { .. }) => true,
111111
(Experimental, Experimental) => true,
112-
(Extends { type_ref: t1, .. }, Extends { type_ref: t2, .. }) => t1 == t2,
112+
(Extends { ts_type: t1, .. }, Extends { ts_type: t2, .. }) => t1 == t2,
113113
(Ignore, Ignore) => true,
114114
(Internal, Internal) => true,
115115
(Module { .. }, Module { .. }) => true,
@@ -122,10 +122,10 @@ fn tags_same_kind(a: &JsDocTag, b: &JsDocTag) -> bool {
122122
(Return { .. }, Return { .. }) => true,
123123
(Tags { .. }, Tags { .. }) => true,
124124
(Template { name: n1, .. }, Template { name: n2, .. }) => n1 == n2,
125-
(This { type_ref: t1, .. }, This { type_ref: t2, .. }) => t1 == t2,
126-
(Throws { type_ref: t1, .. }, Throws { type_ref: t2, .. }) => t1 == t2,
125+
(This { ts_type: t1, .. }, This { ts_type: t2, .. }) => t1 == t2,
126+
(Throws { ts_type: t1, .. }, Throws { ts_type: t2, .. }) => t1 == t2,
127127
(TypeDef { name: n1, .. }, TypeDef { name: n2, .. }) => n1 == n2,
128-
(TypeRef { type_ref: t1, .. }, TypeRef { type_ref: t2, .. }) => t1 == t2,
128+
(TypeRef { ts_type: t1, .. }, TypeRef { ts_type: t2, .. }) => t1 == t2,
129129
(See { .. }, See { .. }) => true,
130130
(Since { .. }, Since { .. }) => true,
131131
(Priority { .. }, Priority { .. }) => true,

src/html/symbols/function.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::html::types::type_params_summary;
1212
use crate::html::util::*;
1313
use crate::js_doc::JsDocTag;
1414
use crate::params::ParamPatternDef;
15+
use crate::ts_type::TsTypeDef;
1516
use indexmap::IndexSet;
1617
use serde::Deserialize;
1718
use serde::Serialize;
@@ -419,20 +420,20 @@ fn render_single_function(
419420
.tags
420421
.iter()
421422
.filter_map(|tag| {
422-
if let JsDocTag::Throws { type_ref, doc } = tag
423-
&& (type_ref.is_some() || doc.is_some())
423+
if let JsDocTag::Throws { ts_type, doc, .. } = tag
424+
&& (ts_type.is_some() || doc.is_some())
424425
{
425-
return Some((type_ref, doc));
426+
return Some((ts_type, doc));
426427
}
427428

428429
None
429430
})
430431
.enumerate()
431-
.map(|(i, (type_ref, doc))| {
432+
.map(|(i, (ts_type, doc))| {
432433
render_function_throws(
433434
ctx,
434435
decl,
435-
type_ref,
436+
ts_type,
436437
doc,
437438
overload_id.clone(),
438439
i,
@@ -444,8 +445,8 @@ fn render_single_function(
444445
// Inject removed throws entries
445446
if let Some(td) = &throws_tags_diff {
446447
for removed_tag in &td.removed {
447-
if let JsDocTag::Throws { type_ref, doc } = removed_tag
448-
&& (type_ref.is_some() || doc.is_some())
448+
if let JsDocTag::Throws { ts_type, doc, .. } = removed_tag
449+
&& (ts_type.is_some() || doc.is_some())
449450
{
450451
let id = IdBuilder::new_with_parent(ctx, &overload_id)
451452
.kind(IdKind::Throws)
@@ -457,7 +458,10 @@ fn render_single_function(
457458
id,
458459
None,
459460
None,
460-
type_ref.as_ref().map(|t| t.as_ref()).unwrap_or_default(),
461+
ts_type
462+
.as_ref()
463+
.map(|t| t.repr.as_str())
464+
.unwrap_or_default(),
461465
IndexSet::new(),
462466
doc.as_ref().map(|d| d.as_ref()),
463467
&decl.location,
@@ -660,38 +664,37 @@ fn inject_removed_params(
660664
fn render_function_throws(
661665
render_ctx: &RenderContext,
662666
decl: &Declaration,
663-
type_ref: &Option<Box<str>>,
667+
ts_type: &Option<TsTypeDef>,
664668
doc: &Option<Box<str>>,
665669
overload_id: Id,
666670
throws_id: usize,
667671
diff: &Option<crate::diff::TagsDiff>,
668672
) -> DocEntryCtx {
669-
let (diff_status, old_content) = if let Some(td) = diff {
670-
if let Some(tag_diff) = td.modified.iter().find(|m| {
671-
matches!(&m.new, JsDocTag::Throws { type_ref: t, .. } if t == type_ref)
673+
let (diff_status, old_content) =
674+
if let Some(td) = diff {
675+
if let Some(tag_diff) = td.modified.iter().find(|m| {
676+
matches!(&m.new, JsDocTag::Throws { ts_type: t, .. } if t == ts_type)
672677
}) {
673678
let old = if let JsDocTag::Throws {
674-
type_ref: old_type_ref,
679+
ts_type: old_ts_type,
675680
..
676681
} = &tag_diff.old
677682
{
678-
old_type_ref
679-
.as_ref()
680-
.map(|t| t.to_string())
683+
old_ts_type.as_ref().map(|t| t.repr.clone())
681684
} else {
682685
None
683686
};
684687
(Some(DiffStatus::Modified), old)
685688
} else if td.added.iter().any(|a| {
686-
matches!(a, JsDocTag::Throws { type_ref: t, .. } if t == type_ref)
689+
matches!(a, JsDocTag::Throws { ts_type: t, .. } if t == ts_type)
687690
}) {
688691
(Some(DiffStatus::Added), None)
689692
} else {
690693
(None, None)
691694
}
692-
} else {
693-
(None, None)
694-
};
695+
} else {
696+
(None, None)
697+
};
695698

696699
let id = IdBuilder::new_with_parent(render_ctx, &overload_id)
697700
.kind(IdKind::Throws)
@@ -703,9 +706,9 @@ fn render_function_throws(
703706
id,
704707
None,
705708
None,
706-
type_ref
709+
ts_type
707710
.as_ref()
708-
.map(|doc| doc.as_ref())
711+
.map(|t| t.repr.as_str())
709712
.unwrap_or_default(),
710713
IndexSet::new(),
711714
doc.as_ref().map(|doc| doc.as_ref()),

src/html/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,9 @@ pub(crate) fn render_type_def(
459459
html_escape::encode_text(&import_type.specifier),
460460
)
461461
}
462+
TsTypeDefKind::Unsupported => {
463+
html_escape::encode_text(&def.repr).into_owned()
464+
}
462465
}
463466
}
464467

0 commit comments

Comments
 (0)