Skip to content

Commit

Permalink
enum DocNode
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jul 22, 2024
1 parent ca6fee4 commit 911ee10
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 195 deletions.
17 changes: 9 additions & 8 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,9 @@ impl<'a, 'b> DiagnosticDocNodeVisitor<'a, 'b> {
}

if let Some(last_node) = last_node {
if doc_node.name == last_node.name && last_node.function_def.is_some() {
if let Some(current_fn) = &doc_node.function_def {
if doc_node.name == last_node.name && last_node.function_def().is_some()
{
if let Some(current_fn) = &doc_node.function_def() {
if current_fn.has_body {
continue; // it's an overload. Ignore it
}
Expand Down Expand Up @@ -402,29 +403,29 @@ impl<'a, 'b> DiagnosticDocNodeVisitor<'a, 'b> {
return; // skip, we don't do these diagnostics above private nodes
}

if is_js_docable_kind(&doc_node.kind) {
if is_js_docable_kind(&doc_node.kind()) {
self
.diagnostics
.check_missing_js_doc(&doc_node.js_doc, &doc_node.location);
}

if let Some(def) = &doc_node.class_def {
if let Some(def) = &doc_node.class_def() {
self.visit_class_def(def);
}

if let Some(def) = &doc_node.function_def {
if let Some(def) = &doc_node.function_def() {
self.visit_function_def(doc_node, def);
}

if let Some(def) = &doc_node.interface_def {
if let Some(def) = &doc_node.interface_def() {
self.visit_interface_def(def);
}

if let Some(def) = &doc_node.namespace_def {
if let Some(def) = &doc_node.namespace_def() {
self.visit_namespace_def(def);
}

if let Some(def) = &doc_node.variable_def {
if let Some(def) = &doc_node.variable_def() {
self.visit_variable_def(doc_node, def);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ impl ModuleDocCtx {

let (deprecated, html) = if let Some(node) = module_doc_nodes
.iter()
.find(|n| n.kind == DocNodeKind::ModuleDoc)
.find(|n| n.kind() == DocNodeKind::ModuleDoc)
{
let deprecated = node.js_doc.tags.iter().find_map(|tag| {
if let JsDocTag::Deprecated { doc } = tag {
Expand Down
23 changes: 11 additions & 12 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;

use crate::node::DocNodeDef;
use crate::DocNode;

pub mod comrak_adapters;
Expand Down Expand Up @@ -312,20 +313,18 @@ impl GenerateCtx {
}

let node = if node
.variable_def
.variable_def()
.as_ref()
.and_then(|def| def.ts_type.as_ref())
.and_then(|ts_type| ts_type.kind.as_ref())
.is_some_and(|kind| {
kind == &crate::ts_type::TsTypeDefKind::FnOrConstructor
}) {
let fn_or_constructor = node
.variable_def
.unwrap()
.ts_type
.unwrap()
.fn_or_constructor
.unwrap();
let DocNodeDef::Variable { variable_def } = node.def else {
unreachable!()
};
let fn_or_constructor =
variable_def.ts_type.unwrap().fn_or_constructor.unwrap();

let mut new_node = DocNode::function(
node.name,
Expand Down Expand Up @@ -354,7 +353,7 @@ impl GenerateCtx {
origin: short_path.clone(),
ns_qualifiers: Rc::new([]),
drilldown_parent_kind: None,
kind_with_drilldown: DocNodeKindWithDrilldown::Other(node.kind),
kind_with_drilldown: DocNodeKindWithDrilldown::Other(node.kind()),
inner: Rc::new(node),
parent: None,
}
Expand Down Expand Up @@ -540,7 +539,7 @@ impl DocNodeWithContext {
origin: self.origin.clone(),
ns_qualifiers: self.ns_qualifiers.clone(),
drilldown_parent_kind: None,
kind_with_drilldown: DocNodeKindWithDrilldown::Other(doc_node.kind),
kind_with_drilldown: DocNodeKindWithDrilldown::Other(doc_node.kind()),
inner: doc_node,
parent: Some(Box::new(self.clone())),
}
Expand All @@ -567,7 +566,7 @@ impl DocNodeWithContext {
method_doc_node.declaration_kind = self.declaration_kind;

let mut new_node = self.create_child(Rc::new(method_doc_node));
new_node.drilldown_parent_kind = Some(self.kind);
new_node.drilldown_parent_kind = Some(self.kind());
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
new_node
}
Expand All @@ -586,7 +585,7 @@ impl DocNodeWithContext {
property_doc_node.declaration_kind = self.declaration_kind;

let mut new_node = self.create_child(Rc::new(property_doc_node));
new_node.drilldown_parent_kind = Some(self.kind);
new_node.drilldown_parent_kind = Some(self.kind());
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
new_node
}
Expand Down
19 changes: 10 additions & 9 deletions src/html/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl IndexCtx {
.map(|(short_path, nodes)| {
let doc = nodes
.iter()
.find(|node| node.kind == DocNodeKind::ModuleDoc)
.find(|node| node.kind() == DocNodeKind::ModuleDoc)
.and_then(|node| {
crate::html::jsdoc::jsdoc_body_to_html(
&render_ctx,
Expand Down Expand Up @@ -484,12 +484,13 @@ pub fn generate_symbol_pages_for_module(

let mut drilldown_partitions = IndexMap::new();
for doc_nodes in name_partitions.values() {
let has_class =
doc_nodes.iter().any(|node| node.kind == DocNodeKind::Class);
let has_class = doc_nodes
.iter()
.any(|node| node.kind() == DocNodeKind::Class);
for doc_node in doc_nodes {
match doc_node.kind {
match doc_node.kind() {
DocNodeKind::Class => {
let class = doc_node.class_def.as_ref().unwrap();
let class = doc_node.class_def().unwrap();

let method_nodes = class
.methods
Expand Down Expand Up @@ -528,7 +529,7 @@ pub fn generate_symbol_pages_for_module(
);
}
DocNodeKind::Interface => {
let interface = doc_node.interface_def.as_ref().unwrap();
let interface = doc_node.interface_def().unwrap();
let method_nodes = interface
.methods
.iter()
Expand Down Expand Up @@ -567,7 +568,7 @@ pub fn generate_symbol_pages_for_module(
.extend(partition::partition_nodes_by_name(&property_nodes, false));
}
DocNodeKind::TypeAlias => {
let type_alias = doc_node.type_alias_def.as_ref().unwrap();
let type_alias = doc_node.type_alias_def().unwrap();

if let Some(ts_type_literal) =
type_alias.ts_type.type_literal.as_ref()
Expand Down Expand Up @@ -612,7 +613,7 @@ pub fn generate_symbol_pages_for_module(
}
}
DocNodeKind::Variable => {
let variable = doc_node.variable_def.as_ref().unwrap();
let variable = doc_node.variable_def().unwrap();

if let Some(ts_type_literal) = variable
.ts_type
Expand Down Expand Up @@ -670,7 +671,7 @@ pub fn generate_symbol_pages_for_module(

if doc_nodes
.iter()
.any(|doc_node| doc_node.kind == DocNodeKind::Class)
.any(|doc_node| doc_node.kind() == DocNodeKind::Class)
{
let prototype_name = format!("{name}.prototype");
generated_pages.push(SymbolPage::Redirect {
Expand Down
12 changes: 6 additions & 6 deletions src/html/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ where
F: Fn(&mut IndexMap<T, Vec<DocNodeWithContext>>, &DocNodeWithContext),
{
for node in doc_nodes {
if matches!(node.kind, DocNodeKind::ModuleDoc | DocNodeKind::Import) {
if matches!(node.kind(), DocNodeKind::ModuleDoc | DocNodeKind::Import) {
continue;
}

if flatten_namespaces && node.kind == DocNodeKind::Namespace {
let namespace_def = node.namespace_def.as_ref().unwrap();
if flatten_namespaces && node.kind() == DocNodeKind::Namespace {
let namespace_def = node.namespace_def().unwrap();
let ns_qualifiers: Rc<[String]> = node.sub_qualifier().into();

partitioner_inner(
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn partition_nodes_by_name(
});

for val in partitions.values_mut() {
val.sort_by_key(|n| n.kind);
val.sort_by_key(|n| n.kind());
}

partitions.sort_keys();
Expand Down Expand Up @@ -138,7 +138,7 @@ pub fn partition_nodes_by_category(

if !entry.iter().any(|n| {
n.get_qualified_name() == node.get_qualified_name()
&& n.kind == node.kind
&& n.kind() == node.kind()
}) {
entry.push(node.clone());
}
Expand Down Expand Up @@ -207,5 +207,5 @@ fn compare_node(
.cmp(&node2.get_qualified_name().to_ascii_lowercase())
})
.then_with(|| node1.get_qualified_name().cmp(&node2.get_qualified_name()))
.then_with(|| node1.kind.cmp(&node2.kind))
.then_with(|| node1.kind().cmp(&node2.kind()))
}
22 changes: 8 additions & 14 deletions src/html/render_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ fn get_current_imports(
let mut imports = HashMap::new();

for doc_node in doc_nodes {
if doc_node.kind == DocNodeKind::Import {
let import_def = doc_node.import_def.as_ref().unwrap();
if doc_node.kind() == DocNodeKind::Import {
let import_def = doc_node.import_def().unwrap();
// TODO: handle import aliasing
if import_def.imported.as_deref() == Some(doc_node.get_name()) {
imports.insert(doc_node.get_name().to_string(), import_def.src.clone());
Expand Down Expand Up @@ -405,7 +405,6 @@ mod test {
let doc_nodes_by_url = indexmap::IndexMap::from([(
ModuleSpecifier::parse("file:///mod.ts").unwrap(),
vec![DocNode {
kind: DocNodeKind::Import,
name: "foo".into(),
is_default: None,
location: Location {
Expand All @@ -416,17 +415,12 @@ mod test {
},
declaration_kind: DeclarationKind::Private,
js_doc: Default::default(),
function_def: None,
variable_def: None,
enum_def: None,
class_def: None,
type_alias_def: None,
namespace_def: None,
interface_def: None,
import_def: Some(ImportDef {
src: "b".to_string(),
imported: Some("foo".to_string()),
}),
def: crate::node::DocNodeDef::Import {
import_def: ImportDef {
src: "b".to_string(),
imported: Some("foo".to_string()),
},
},
}],
)]);

Expand Down
2 changes: 1 addition & 1 deletion src/html/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn doc_nodes_into_search_index_node(
doc_nodes: Vec<DocNodeWithContext>,
name: String,
) -> SearchIndexNode {
let kinds = doc_nodes.iter().map(|node| node.kind).collect();
let kinds = doc_nodes.iter().map(|node| node.kind()).collect();
let deprecated =
super::util::all_deprecated(&doc_nodes.iter().collect::<Vec<_>>());

Expand Down
2 changes: 1 addition & 1 deletion src/html/symbols/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub(crate) fn render_class(
doc_node: &DocNodeWithContext,
name: &str,
) -> Vec<SectionCtx> {
let class_def = doc_node.class_def.as_ref().unwrap();
let class_def = doc_node.class_def().unwrap();

let current_type_params = class_def
.type_params
Expand Down
2 changes: 1 addition & 1 deletion src/html/symbols/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) fn render_enum(
render_ctx: &RenderContext,
doc_node: &DocNodeWithContext,
) -> Vec<SectionCtx> {
let mut members = doc_node.enum_def.as_ref().unwrap().members.clone();
let mut members = doc_node.enum_def().unwrap().members.clone();

members.sort_by(|a, b| a.name.cmp(&b.name));

Expand Down
6 changes: 3 additions & 3 deletions src/html/symbols/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ impl FunctionCtx {
.iter()
.enumerate()
.filter(|(i, doc_node)| {
let function_def = doc_node.function_def.as_ref().unwrap();
let function_def = doc_node.function_def().unwrap();

!(function_def.has_body && *i != 0)
})
.count();

for (i, doc_node) in doc_nodes.into_iter().enumerate() {
let function_def = doc_node.function_def.as_ref().unwrap();
let function_def = doc_node.function_def().unwrap();

if function_def.has_body && i != 0 {
continue;
Expand Down Expand Up @@ -116,7 +116,7 @@ fn render_single_function(
doc_node: &DocNodeWithContext,
overload_id: &str,
) -> SymbolContentCtx {
let function_def = doc_node.function_def.as_ref().unwrap();
let function_def = doc_node.function_def().unwrap();

let current_type_params = function_def
.type_params
Expand Down
2 changes: 1 addition & 1 deletion src/html/symbols/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) fn render_interface(
doc_node: &DocNodeWithContext,
name: &str,
) -> Vec<SectionCtx> {
let interface_def = doc_node.interface_def.as_ref().unwrap();
let interface_def = doc_node.interface_def().unwrap();

let current_type_params = interface_def
.type_params
Expand Down
Loading

0 comments on commit 911ee10

Please sign in to comment.