Skip to content

Commit

Permalink
feat(html): variable type literal breakdown
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats committed Apr 26, 2024
1 parent 915782f commit eb55e65
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 120 deletions.
257 changes: 156 additions & 101 deletions src/html/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,107 +222,162 @@ pub fn generate_symbol_pages_for_module(

let mut drilldown_partitions = IndexMap::new();
for (name, doc_nodes) in &name_partitions {
if doc_nodes[0].kind == DocNodeKind::Class {
let class = doc_nodes[0].class_def.as_ref().unwrap();
let method_nodes = class
.methods
.iter()
.map(|method| {
doc_nodes[0].create_child_method(
DocNode::function(
method.name.clone(),
method.location.clone(),
doc_nodes[0].declaration_kind,
method.js_doc.clone(),
method.function_def.clone(),
),
name,
method.is_static,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = class
.properties
.iter()
.map(|property| {
doc_nodes[0].create_child_property(
DocNode::from(property.clone()),
name,
property.is_static,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&property_nodes));
} else if doc_nodes[0].kind == DocNodeKind::Interface {
let interface = doc_nodes[0].interface_def.as_ref().unwrap();
let method_nodes = interface
.methods
.iter()
.map(|method| {
doc_nodes[0].create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = interface
.properties
.iter()
.map(|property| {
doc_nodes[0].create_child_property(
DocNode::from(property.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&property_nodes));
} else if doc_nodes[0].kind == DocNodeKind::TypeAlias {
let type_alias = doc_nodes[0].type_alias_def.as_ref().unwrap();

if let Some(ts_type_literal) = type_alias.ts_type.type_literal.as_ref() {
let method_nodes = ts_type_literal
.methods
.iter()
.map(|method| {
doc_nodes[0].create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = ts_type_literal
.properties
.iter()
.map(|property| {
doc_nodes[0].create_child_property(
DocNode::from(property.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&property_nodes));
for doc_node in doc_nodes {
match doc_node.kind {
DocNodeKind::Class => {
let class = doc_node.class_def.as_ref().unwrap();

let method_nodes = class
.methods
.iter()
.map(|method| {
doc_node.create_child_method(
DocNode::function(
method.name.clone(),
method.location.clone(),
doc_node.declaration_kind,
method.js_doc.clone(),
method.function_def.clone(),
),
name,
method.is_static,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = class
.properties
.iter()
.map(|property| {
doc_node.create_child_property(
DocNode::from(property.clone()),
name,
property.is_static,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&property_nodes));
}
DocNodeKind::Interface => {
let interface = doc_node.interface_def.as_ref().unwrap();
let method_nodes = interface
.methods
.iter()
.map(|method| {
doc_node.create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = interface
.properties
.iter()
.map(|property| {
doc_node.create_child_property(
DocNode::from(property.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&property_nodes));
}
DocNodeKind::TypeAlias => {
let type_alias = doc_node.type_alias_def.as_ref().unwrap();

if let Some(ts_type_literal) =
type_alias.ts_type.type_literal.as_ref()
{
let method_nodes = ts_type_literal
.methods
.iter()
.map(|method| {
doc_node.create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = ts_type_literal
.properties
.iter()
.map(|property| {
doc_node.create_child_property(
DocNode::from(property.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions.extend(
super::partition::partition_nodes_by_name(&property_nodes),
);
}
}
DocNodeKind::Variable => {
let variable = doc_node.variable_def.as_ref().unwrap();

if let Some(ts_type_literal) = variable
.ts_type
.as_ref()
.and_then(|ts_type| ts_type.type_literal.as_ref())
{
let method_nodes = ts_type_literal
.methods
.iter()
.map(|method| {
doc_node.create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions
.extend(super::partition::partition_nodes_by_name(&method_nodes));

let property_nodes = ts_type_literal
.properties
.iter()
.map(|property| {
doc_node.create_child_property(
DocNode::from(property.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

drilldown_partitions.extend(
super::partition::partition_nodes_by_name(&property_nodes),
);
}
}
DocNodeKind::Enum => {}
DocNodeKind::Function => {}
DocNodeKind::Import => {}
DocNodeKind::ModuleDoc => {}
DocNodeKind::Namespace => {}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/html/symbols/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(crate) fn render_interface(
sections
}

pub fn render_index_signatures(
pub(crate) fn render_index_signatures(
ctx: &RenderContext,
index_signatures: &[crate::ts_type::IndexSignatureDef],
) -> Option<SectionCtx> {
Expand Down Expand Up @@ -95,7 +95,7 @@ pub fn render_index_signatures(
))
}

pub fn render_call_signatures(
pub(crate) fn render_call_signatures(
ctx: &RenderContext,
call_signatures: &[crate::ts_type::CallSignatureDef],
) -> Option<SectionCtx> {
Expand Down Expand Up @@ -140,7 +140,7 @@ pub fn render_call_signatures(
))
}

pub fn render_properties(
pub(crate) fn render_properties(
ctx: &RenderContext,
interface_name: &str,
properties: &[crate::ts_type::PropertyDef],
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn render_properties(
))
}

pub fn render_methods(
pub(crate) fn render_methods(
ctx: &RenderContext,
interface_name: &str,
methods: &[crate::ts_type::MethodDef],
Expand Down
64 changes: 49 additions & 15 deletions src/html/symbols/variable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::html::render_context::RenderContext;
use crate::html::symbols::interface::render_call_signatures;
use crate::html::symbols::interface::render_index_signatures;
use crate::html::symbols::interface::render_methods;
use crate::html::symbols::interface::render_properties;
use crate::html::types::render_type_def;
use crate::html::util::*;
use crate::html::DocNodeWithContext;
Expand All @@ -10,23 +14,53 @@ pub(crate) fn render_variable(
) -> Vec<SectionCtx> {
let variable_def = doc_node.variable_def.as_ref().unwrap();

if variable_def.ts_type.is_none() {
let Some(ts_type) = &variable_def.ts_type else {
return vec![];
}
};

let id = name_to_id("variable", doc_node.get_name());

vec![SectionCtx::new(
"Type",
SectionContentCtx::DocEntry(vec![DocEntryCtx::new(
ctx,
&id,
"",
None,
&render_type_def(ctx, variable_def.ts_type.as_ref().unwrap()),
HashSet::new(),
None,
&doc_node.location,
)]),
)]
let mut sections = vec![];

if let Some(ts_type_literal) = &ts_type.type_literal {
if let Some(index_signatures) =
render_index_signatures(ctx, &ts_type_literal.index_signatures)
{
sections.push(index_signatures);
}

if let Some(call_signatures) =
render_call_signatures(ctx, &ts_type_literal.call_signatures)
{
sections.push(call_signatures);
}

if let Some(properties) =
render_properties(ctx, doc_node.get_name(), &ts_type_literal.properties)
{
sections.push(properties);
}

if let Some(methods) =
render_methods(ctx, doc_node.get_name(), &ts_type_literal.methods)
{
sections.push(methods);
}
} else {
sections.push(SectionCtx::new(
"Type",
SectionContentCtx::DocEntry(vec![DocEntryCtx::new(
ctx,
&id,
"",
None,
&render_type_def(ctx, ts_type),
HashSet::new(),
None,
&doc_node.location,
)]),
));
}

sections
}

0 comments on commit eb55e65

Please sign in to comment.