From eb55e65bed03bfa9f8cd655a8b908c99dd4c84a7 Mon Sep 17 00:00:00 2001 From: crowlkats Date: Fri, 26 Apr 2024 18:28:38 +0200 Subject: [PATCH] feat(html): variable type literal breakdown --- src/html/pages.rs | 257 +++++++++++++++++++++------------- src/html/symbols/interface.rs | 8 +- src/html/symbols/variable.rs | 64 +++++++-- src/html/util.rs | 37 +++++ 4 files changed, 246 insertions(+), 120 deletions(-) diff --git a/src/html/pages.rs b/src/html/pages.rs index 5291cf05..fefee665 100644 --- a/src/html/pages.rs +++ b/src/html/pages.rs @@ -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::>(); - - 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::>(); - - 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::>(); - - 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::>(); - - 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::>(); - - 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::>(); - - 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::>(); + + 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::>(); + + 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::>(); + + 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::>(); + + 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::>(); + + 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::>(); + + 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::>(); + + 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::>(); + + drilldown_partitions.extend( + super::partition::partition_nodes_by_name(&property_nodes), + ); + } + } + DocNodeKind::Enum => {} + DocNodeKind::Function => {} + DocNodeKind::Import => {} + DocNodeKind::ModuleDoc => {} + DocNodeKind::Namespace => {} } } } diff --git a/src/html/symbols/interface.rs b/src/html/symbols/interface.rs index 93bae837..cb09d1d4 100644 --- a/src/html/symbols/interface.rs +++ b/src/html/symbols/interface.rs @@ -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 { @@ -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 { @@ -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], @@ -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], diff --git a/src/html/symbols/variable.rs b/src/html/symbols/variable.rs index d2aa2060..ddc5e135 100644 --- a/src/html/symbols/variable.rs +++ b/src/html/symbols/variable.rs @@ -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; @@ -10,23 +14,53 @@ pub(crate) fn render_variable( ) -> Vec { 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 } diff --git a/src/html/util.rs b/src/html/util.rs index 1ce5088d..4ff3a37e 100644 --- a/src/html/util.rs +++ b/src/html/util.rs @@ -155,6 +155,43 @@ pub fn compute_namespaced_symbols( )); } } + DocNodeKind::Variable => { + let variable = doc_node.variable_def.as_ref().unwrap(); + + if let Some(type_literal) = variable + .ts_type + .as_ref() + .and_then(|ts_type| ts_type.type_literal.as_ref()) + { + namespaced_symbols.extend(type_literal.methods.iter().map( + |method| { + let mut method_path = current_path.to_vec(); + method_path.extend( + qualify_drilldown_name(doc_node.get_name(), &method.name, true) + .split('.') + .map(|part| part.to_string()), + ); + method_path + }, + )); + + namespaced_symbols.extend(type_literal.properties.iter().map( + |property| { + let mut method_path = current_path.to_vec(); + method_path.extend( + qualify_drilldown_name( + doc_node.get_name(), + &property.name, + true, + ) + .split('.') + .map(|part| part.to_string()), + ); + method_path + }, + )); + } + } _ => {} }