Skip to content

Commit 915782f

Browse files
authored
feat: support Location for ts literals, render ts literals as interfaces (#562)
1 parent 6ea914f commit 915782f

34 files changed

+2165
-429
lines changed

src/class.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::params::prop_name_to_string;
1818
use crate::params::ts_fn_param_to_param_def;
1919
use crate::ts_type::infer_ts_type_from_expr;
2020
use crate::ts_type::maybe_type_param_instantiation_to_type_defs;
21+
use crate::ts_type::IndexSignatureDef;
2122
use crate::ts_type::TsTypeDef;
2223
use crate::ts_type_param::maybe_type_param_decl_to_type_param_defs;
2324
use crate::ts_type_param::TsTypeParamDef;
@@ -159,33 +160,6 @@ impl Display for ClassPropertyDef {
159160
}
160161
}
161162

162-
#[derive(Debug, Serialize, Deserialize, Clone)]
163-
#[serde(rename_all = "camelCase")]
164-
pub struct ClassIndexSignatureDef {
165-
#[serde(skip_serializing_if = "JsDoc::is_empty", default)]
166-
pub js_doc: JsDoc,
167-
pub readonly: bool,
168-
pub params: Vec<ParamDef>,
169-
pub ts_type: Option<TsTypeDef>,
170-
pub location: Location,
171-
}
172-
173-
#[cfg(feature = "rust")]
174-
impl Display for ClassIndexSignatureDef {
175-
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
176-
write!(
177-
f,
178-
"{}[{}]",
179-
display_readonly(self.readonly),
180-
SliceDisplayer::new(&self.params, ", ", false)
181-
)?;
182-
if let Some(ts_type) = &self.ts_type {
183-
write!(f, ": {}", ts_type)?;
184-
}
185-
Ok(())
186-
}
187-
}
188-
189163
#[derive(Debug, Serialize, Deserialize, Clone)]
190164
#[serde(rename_all = "camelCase")]
191165
pub struct ClassMethodDef {
@@ -248,7 +222,7 @@ pub struct ClassDef {
248222
pub is_abstract: bool,
249223
pub constructors: Vec<ClassConstructorDef>,
250224
pub properties: Vec<ClassPropertyDef>,
251-
pub index_signatures: Vec<ClassIndexSignatureDef>,
225+
pub index_signatures: Vec<IndexSignatureDef>,
252226
pub methods: Vec<ClassMethodDef>,
253227
pub extends: Option<String>,
254228
pub implements: Vec<TsTypeDef>,
@@ -434,7 +408,7 @@ pub fn class_to_class_def(
434408
.as_ref()
435409
.map(|rt| TsTypeDef::new(parsed_source, &rt.type_ann));
436410

437-
let index_sig_def = ClassIndexSignatureDef {
411+
let index_sig_def = IndexSignatureDef {
438412
location: get_location(parsed_source, ts_index_sig.start()),
439413
js_doc,
440414
readonly: ts_index_sig.readonly,

src/html/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,38 @@ impl DocNodeWithContext {
289289
inner: doc_node,
290290
}
291291
}
292+
293+
pub fn create_child_method(
294+
&self,
295+
mut method_doc_node: DocNode,
296+
parent_name: &str,
297+
is_static: bool,
298+
) -> Self {
299+
method_doc_node.name =
300+
qualify_drilldown_name(parent_name, &method_doc_node.name, is_static);
301+
method_doc_node.declaration_kind = self.declaration_kind;
302+
303+
let mut new_node = self.create_child(Arc::new(method_doc_node));
304+
new_node.drilldown_parent_kind = Some(self.kind);
305+
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
306+
new_node
307+
}
308+
309+
pub fn create_child_property(
310+
&self,
311+
mut property_doc_node: DocNode,
312+
parent_name: &str,
313+
is_static: bool,
314+
) -> Self {
315+
property_doc_node.name =
316+
qualify_drilldown_name(parent_name, &property_doc_node.name, is_static);
317+
property_doc_node.declaration_kind = self.declaration_kind;
318+
319+
let mut new_node = self.create_child(Arc::new(property_doc_node));
320+
new_node.drilldown_parent_kind = Some(self.kind);
321+
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
322+
new_node
323+
}
292324
}
293325

294326
impl core::ops::Deref for DocNodeWithContext {

src/html/pages.rs

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
use super::sidepanels;
22
use super::sidepanels::SidepanelCtx;
33
use super::symbols::SymbolContentCtx;
4-
use super::util::qualify_drilldown_name;
54
use super::util::BreadcrumbsCtx;
65
use super::util::SectionHeaderCtx;
7-
use super::DocNodeKindWithDrilldown;
86
use super::DocNodeWithContext;
97
use super::FileMode;
108
use super::GenerateCtx;
119
use super::RenderContext;
1210
use super::ShortPath;
1311
use super::SymbolGroupCtx;
1412
use super::UrlResolveKind;
15-
use std::sync::Arc;
1613

1714
use super::FUSE_FILENAME;
1815
use super::PAGE_STYLESHEET_FILENAME;
@@ -21,9 +18,7 @@ use super::SEARCH_FILENAME;
2118
use super::SEARCH_INDEX_FILENAME;
2219
use super::STYLESHEET_FILENAME;
2320

24-
use crate::function::FunctionDef;
2521
use crate::html::partition::Partition;
26-
use crate::variable::VariableDef;
2722
use crate::DocNode;
2823
use crate::DocNodeKind;
2924
use indexmap::IndexMap;
@@ -233,17 +228,17 @@ pub fn generate_symbol_pages_for_module(
233228
.methods
234229
.iter()
235230
.map(|method| {
236-
let mut new_node =
237-
doc_nodes[0].create_child(Arc::new(DocNode::function(
238-
qualify_drilldown_name(name, &method.name, method.is_static),
231+
doc_nodes[0].create_child_method(
232+
DocNode::function(
233+
method.name.clone(),
239234
method.location.clone(),
240235
doc_nodes[0].declaration_kind,
241236
method.js_doc.clone(),
242237
method.function_def.clone(),
243-
)));
244-
new_node.drilldown_parent_kind = Some(DocNodeKind::Class);
245-
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
246-
new_node
238+
),
239+
name,
240+
method.is_static,
241+
)
247242
})
248243
.collect::<Vec<_>>();
249244

@@ -254,20 +249,11 @@ pub fn generate_symbol_pages_for_module(
254249
.properties
255250
.iter()
256251
.map(|property| {
257-
let mut new_node =
258-
doc_nodes[0].create_child(Arc::new(DocNode::variable(
259-
qualify_drilldown_name(name, &property.name, property.is_static),
260-
property.location.clone(),
261-
doc_nodes[0].declaration_kind,
262-
property.js_doc.clone(),
263-
VariableDef {
264-
ts_type: property.ts_type.clone(),
265-
kind: deno_ast::swc::ast::VarDeclKind::Const,
266-
},
267-
)));
268-
new_node.drilldown_parent_kind = Some(DocNodeKind::Class);
269-
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
270-
new_node
252+
doc_nodes[0].create_child_property(
253+
DocNode::from(property.clone()),
254+
name,
255+
property.is_static,
256+
)
271257
})
272258
.collect::<Vec<_>>();
273259

@@ -279,26 +265,11 @@ pub fn generate_symbol_pages_for_module(
279265
.methods
280266
.iter()
281267
.map(|method| {
282-
let mut new_node =
283-
doc_nodes[0].create_child(Arc::new(DocNode::function(
284-
qualify_drilldown_name(name, &method.name, true),
285-
method.location.clone(),
286-
doc_nodes[0].declaration_kind,
287-
method.js_doc.clone(),
288-
FunctionDef {
289-
def_name: None,
290-
params: method.params.clone(),
291-
return_type: method.return_type.clone(),
292-
has_body: false,
293-
is_async: false,
294-
is_generator: false,
295-
type_params: method.type_params.clone(),
296-
decorators: vec![],
297-
},
298-
)));
299-
new_node.drilldown_parent_kind = Some(DocNodeKind::Interface);
300-
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
301-
new_node
268+
doc_nodes[0].create_child_method(
269+
DocNode::from(method.clone()),
270+
name,
271+
true,
272+
)
302273
})
303274
.collect::<Vec<_>>();
304275

@@ -309,25 +280,50 @@ pub fn generate_symbol_pages_for_module(
309280
.properties
310281
.iter()
311282
.map(|property| {
312-
let mut new_node =
313-
doc_nodes[0].create_child(Arc::new(DocNode::variable(
314-
qualify_drilldown_name(name, &property.name, true),
315-
property.location.clone(),
316-
doc_nodes[0].declaration_kind,
317-
property.js_doc.clone(),
318-
VariableDef {
319-
ts_type: property.ts_type.clone(),
320-
kind: deno_ast::swc::ast::VarDeclKind::Const,
321-
},
322-
)));
323-
new_node.drilldown_parent_kind = Some(DocNodeKind::Interface);
324-
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
325-
new_node
283+
doc_nodes[0].create_child_property(
284+
DocNode::from(property.clone()),
285+
name,
286+
true,
287+
)
326288
})
327289
.collect::<Vec<_>>();
328290

329291
drilldown_partitions
330292
.extend(super::partition::partition_nodes_by_name(&property_nodes));
293+
} else if doc_nodes[0].kind == DocNodeKind::TypeAlias {
294+
let type_alias = doc_nodes[0].type_alias_def.as_ref().unwrap();
295+
296+
if let Some(ts_type_literal) = type_alias.ts_type.type_literal.as_ref() {
297+
let method_nodes = ts_type_literal
298+
.methods
299+
.iter()
300+
.map(|method| {
301+
doc_nodes[0].create_child_method(
302+
DocNode::from(method.clone()),
303+
name,
304+
true,
305+
)
306+
})
307+
.collect::<Vec<_>>();
308+
309+
drilldown_partitions
310+
.extend(super::partition::partition_nodes_by_name(&method_nodes));
311+
312+
let property_nodes = ts_type_literal
313+
.properties
314+
.iter()
315+
.map(|property| {
316+
doc_nodes[0].create_child_property(
317+
DocNode::from(property.clone()),
318+
name,
319+
true,
320+
)
321+
})
322+
.collect::<Vec<_>>();
323+
324+
drilldown_partitions
325+
.extend(super::partition::partition_nodes_by_name(&property_nodes));
326+
}
331327
}
332328
}
333329
name_partitions.extend(drilldown_partitions);

src/html/symbols/class.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) fn render_class(
5050
}
5151

5252
if let Some(index_signatures) =
53-
render_index_signatures(ctx, &class_def.index_signatures)
53+
super::interface::render_index_signatures(ctx, &class_def.index_signatures)
5454
{
5555
sections.push(index_signatures);
5656
}
@@ -154,44 +154,6 @@ pub struct IndexSignatureCtx {
154154
pub source_href: Option<String>,
155155
}
156156

157-
fn render_index_signatures(
158-
ctx: &RenderContext,
159-
index_signatures: &[crate::class::ClassIndexSignatureDef],
160-
) -> Option<SectionCtx> {
161-
if index_signatures.is_empty() {
162-
return None;
163-
}
164-
165-
let mut items = Vec::with_capacity(index_signatures.len());
166-
167-
for (i, index_signature) in index_signatures.iter().enumerate() {
168-
let id = name_to_id("index_signature", &i.to_string());
169-
170-
let ts_type = index_signature
171-
.ts_type
172-
.as_ref()
173-
.map(|ts_type| render_type_def_colon(ctx, ts_type))
174-
.unwrap_or_default();
175-
176-
items.push(IndexSignatureCtx {
177-
id: id.clone(),
178-
anchor: AnchorCtx { id },
179-
readonly: index_signature.readonly,
180-
params: render_params(ctx, &index_signature.params),
181-
ts_type,
182-
source_href: ctx
183-
.ctx
184-
.href_resolver
185-
.resolve_source(&index_signature.location),
186-
});
187-
}
188-
189-
Some(SectionCtx::new(
190-
"Index Signatures",
191-
SectionContentCtx::IndexSignature(items),
192-
))
193-
}
194-
195157
enum PropertyOrMethod {
196158
Property(ClassPropertyDef),
197159
Method(ClassMethodDef),

src/html/symbols/interface.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ pub(crate) fn render_interface(
5757
sections
5858
}
5959

60-
fn render_index_signatures(
60+
pub fn render_index_signatures(
6161
ctx: &RenderContext,
62-
index_signatures: &[crate::interface::InterfaceIndexSignatureDef],
62+
index_signatures: &[crate::ts_type::IndexSignatureDef],
6363
) -> Option<SectionCtx> {
6464
if index_signatures.is_empty() {
6565
return None;
@@ -95,9 +95,9 @@ fn render_index_signatures(
9595
))
9696
}
9797

98-
fn render_call_signatures(
98+
pub fn render_call_signatures(
9999
ctx: &RenderContext,
100-
call_signatures: &[crate::interface::InterfaceCallSignatureDef],
100+
call_signatures: &[crate::ts_type::CallSignatureDef],
101101
) -> Option<SectionCtx> {
102102
if call_signatures.is_empty() {
103103
return None;
@@ -140,10 +140,10 @@ fn render_call_signatures(
140140
))
141141
}
142142

143-
fn render_properties(
143+
pub fn render_properties(
144144
ctx: &RenderContext,
145145
interface_name: &str,
146-
properties: &[crate::interface::InterfacePropertyDef],
146+
properties: &[crate::ts_type::PropertyDef],
147147
) -> Option<SectionCtx> {
148148
if properties.is_empty() {
149149
return None;
@@ -210,10 +210,10 @@ fn render_properties(
210210
))
211211
}
212212

213-
fn render_methods(
213+
pub fn render_methods(
214214
ctx: &RenderContext,
215215
interface_name: &str,
216-
methods: &[crate::interface::InterfaceMethodDef],
216+
methods: &[crate::ts_type::MethodDef],
217217
) -> Option<SectionCtx> {
218218
if methods.is_empty() {
219219
return None;

0 commit comments

Comments
 (0)