Skip to content

Commit

Permalink
refactor: remove DocNodeKind (#696)
Browse files Browse the repository at this point in the history
This doesnt affect JSON output, only the rust API.
This is done in favour of just operating of DocNodeDef variants instead of having a special type that mirrors all the variants

Also changes NamespaceDef elements to be stored as Arc instead of Rc.
  • Loading branch information
crowlKats authored Jan 30, 2025
1 parent 90c8f00 commit 6d6209b
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 237 deletions.
8 changes: 5 additions & 3 deletions examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
use clap::App;
use clap::Arg;
use deno_doc::find_nodes_by_name_recursively;
use deno_doc::html::GenerateCtx;
use deno_doc::html::HrefResolver;
use deno_doc::html::UrlResolveKind;
use deno_doc::html::UsageComposer;
use deno_doc::html::UsageComposerEntry;
use deno_doc::html::{GenerateCtx, HrefResolver};
use deno_doc::DocNodeKind;
use deno_doc::node::DocNodeDef;
use deno_doc::DocParser;
use deno_doc::DocParserOptions;
use deno_doc::DocPrinter;
Expand Down Expand Up @@ -151,7 +152,8 @@ async fn run() -> anyhow::Result<()> {
let mut doc_nodes =
parser.parse()?.into_values().flatten().collect::<Vec<_>>();

doc_nodes.retain(|doc_node| doc_node.kind() != DocNodeKind::Import);
doc_nodes
.retain(|doc_node| !matches!(doc_node.def, DocNodeDef::Import { .. }));
if let Some(filter) = maybe_filter {
doc_nodes = find_nodes_by_name_recursively(doc_nodes, filter);
}
Expand Down
28 changes: 14 additions & 14 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
use crate::js_doc::JsDoc;
use crate::node::DeclarationKind;
use crate::node::DocNode;
use crate::node::DocNodeDef;
use crate::node::NamespaceDef;
use crate::ts_type::TsTypeDef;
use crate::util::swc::get_text_info_location;
use crate::util::swc::has_ignorable_js_doc_tag;
use crate::util::symbol::symbol_has_ignorable_js_doc_tag;
use crate::variable::VariableDef;
use crate::DocNodeKind;
use crate::Location;

use deno_ast::diagnostics::Diagnostic;
Expand Down Expand Up @@ -394,26 +394,26 @@ impl DiagnosticDocNodeVisitor<'_, '_> {
}

fn visit_doc_node(&mut self, doc_node: &DocNode) {
fn is_js_docable_kind(kind: &DocNodeKind) -> bool {
match kind {
DocNodeKind::Class
| DocNodeKind::Enum
| DocNodeKind::Function
| DocNodeKind::Interface
| DocNodeKind::Namespace
| DocNodeKind::TypeAlias
| DocNodeKind::Variable => true,
DocNodeKind::Import
| DocNodeKind::ModuleDoc
| DocNodeKind::Reference => false,
fn is_js_docable_kind(node: &DocNode) -> bool {
match node.def {
DocNodeDef::Class { .. }
| DocNodeDef::Enum { .. }
| DocNodeDef::Function { .. }
| DocNodeDef::Interface { .. }
| DocNodeDef::Namespace { .. }
| DocNodeDef::TypeAlias { .. }
| DocNodeDef::Variable { .. } => true,
DocNodeDef::Import { .. }
| DocNodeDef::ModuleDoc { .. }
| DocNodeDef::Reference { .. } => false,
}
}

if doc_node.declaration_kind == DeclarationKind::Private {
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) {
self
.diagnostics
.check_missing_js_doc(&doc_node.js_doc, &doc_node.location);
Expand Down
4 changes: 2 additions & 2 deletions src/html/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::util::*;
use crate::html::ShortPath;
use crate::js_doc::JsDoc;
use crate::js_doc::JsDocTag;
use crate::DocNodeKind;
use crate::node::DocNodeDef;
use serde::Serialize;
use std::borrow::Cow;
use std::rc::Rc;
Expand Down Expand Up @@ -330,7 +330,7 @@ impl ModuleDocCtx {

let (deprecated, html) = if let Some(node) = module_doc_nodes
.iter()
.find(|n| n.kind() == DocNodeKind::ModuleDoc)
.find(|n| matches!(n.def, DocNodeDef::ModuleDoc))
{
let deprecated = node.js_doc.tags.iter().find_map(|tag| {
if let JsDocTag::Deprecated { doc } = tag {
Expand Down
139 changes: 77 additions & 62 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::node::DocNodeDef;
use crate::DocNode;
use deno_ast::swc::ast::MethodKind;
use deno_ast::ModuleSpecifier;
use handlebars::handlebars_helper;
use handlebars::Handlebars;
Expand All @@ -12,6 +11,7 @@ use std::cmp::Ordering;
use std::collections::HashMap;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;

pub mod jsdoc;
pub mod pages;
Expand Down Expand Up @@ -353,8 +353,8 @@ impl GenerateCtx {
DocNodeWithContext {
origin: short_path.clone(),
ns_qualifiers: Rc::new([]),
kind_with_drilldown: DocNodeKindWithDrilldown::Other(node.kind()),
inner: Rc::new(node),
kind: DocNodeKind::from_node(&node),
inner: Arc::new(node),
drilldown_name: None,
parent: None,
namespace_children: None,
Expand Down Expand Up @@ -499,7 +499,7 @@ impl GenerateCtx {
return Box::new(std::iter::once(node));
}

if node.kind() == crate::DocNodeKind::Namespace {
if matches!(node.def, DocNodeDef::Namespace { .. }) {
if let Some(children) = &node.namespace_children {
return Box::new(
children
Expand Down Expand Up @@ -656,58 +656,74 @@ impl PartialOrd for ShortPath {
}
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
pub enum DocNodeKindWithDrilldown {
Property,
Method(MethodKind),
Other(crate::DocNodeKind),
#[derive(
Debug,
PartialEq,
Eq,
Hash,
Clone,
Copy,
Serialize,
Deserialize,
Ord,
PartialOrd,
)]
pub enum MethodKind {
Method,
Getter,
Setter,
}

impl PartialOrd for DocNodeKindWithDrilldown {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
impl From<deno_ast::swc::ast::MethodKind> for MethodKind {
fn from(value: deno_ast::swc::ast::MethodKind) -> Self {
match value {
deno_ast::swc::ast::MethodKind::Method => Self::Method,
deno_ast::swc::ast::MethodKind::Getter => Self::Getter,
deno_ast::swc::ast::MethodKind::Setter => Self::Setter,
}
}
}

impl Ord for DocNodeKindWithDrilldown {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(
DocNodeKindWithDrilldown::Other(other1),
DocNodeKindWithDrilldown::Other(other2),
) => other1.cmp(other2),
(
DocNodeKindWithDrilldown::Property,
DocNodeKindWithDrilldown::Other(_),
) => Ordering::Less,
(
DocNodeKindWithDrilldown::Property,
DocNodeKindWithDrilldown::Method(_),
) => Ordering::Less,
(
DocNodeKindWithDrilldown::Property,
DocNodeKindWithDrilldown::Property,
) => Ordering::Equal,
(
DocNodeKindWithDrilldown::Method(_),
DocNodeKindWithDrilldown::Other(_),
) => Ordering::Less,
(
DocNodeKindWithDrilldown::Method(_),
DocNodeKindWithDrilldown::Method(_),
) => Ordering::Equal,
(
DocNodeKindWithDrilldown::Method(_),
DocNodeKindWithDrilldown::Property,
) => Ordering::Greater,
(
DocNodeKindWithDrilldown::Other(_),
DocNodeKindWithDrilldown::Property,
) => Ordering::Greater,
(
DocNodeKindWithDrilldown::Other(_),
DocNodeKindWithDrilldown::Method(_),
) => Ordering::Greater,
#[derive(
Debug,
PartialEq,
Eq,
Hash,
Clone,
Copy,
Serialize,
Deserialize,
Ord,
PartialOrd,
)]
pub enum DocNodeKind {
Property,
Method(MethodKind),
Class,
Enum,
Function,
Import,
Interface,
ModuleDoc,
Namespace,
Reference,
TypeAlias,
Variable,
}

impl DocNodeKind {
fn from_node(node: &DocNode) -> Self {
match node.def {
DocNodeDef::Function { .. } => Self::Function,
DocNodeDef::Variable { .. } => Self::Variable,
DocNodeDef::Enum { .. } => Self::Enum,
DocNodeDef::Class { .. } => Self::Class,
DocNodeDef::TypeAlias { .. } => Self::TypeAlias,
DocNodeDef::Namespace { .. } => Self::Namespace,
DocNodeDef::Interface { .. } => Self::Interface,
DocNodeDef::Import { .. } => Self::Import,
DocNodeDef::ModuleDoc => Self::ModuleDoc,
DocNodeDef::Reference { .. } => Self::Reference,
}
}
}
Expand All @@ -720,19 +736,19 @@ impl Ord for DocNodeKindWithDrilldown {
pub struct DocNodeWithContext {
pub origin: Rc<ShortPath>,
pub ns_qualifiers: Rc<[String]>,
pub kind_with_drilldown: DocNodeKindWithDrilldown,
pub inner: Rc<DocNode>,
pub kind: DocNodeKind,
pub inner: Arc<DocNode>,
pub drilldown_name: Option<Box<str>>,
pub parent: Option<Box<DocNodeWithContext>>,
pub namespace_children: Option<Vec<DocNodeWithContext>>,
}

impl DocNodeWithContext {
pub fn create_child(&self, doc_node: Rc<DocNode>) -> Self {
pub fn create_child(&self, doc_node: Arc<DocNode>) -> Self {
DocNodeWithContext {
origin: self.origin.clone(),
ns_qualifiers: self.ns_qualifiers.clone(),
kind_with_drilldown: DocNodeKindWithDrilldown::Other(doc_node.kind()),
kind: DocNodeKind::from_node(&doc_node),
inner: doc_node,
drilldown_name: None,
parent: Some(Box::new(self.clone())),
Expand All @@ -742,7 +758,7 @@ impl DocNodeWithContext {

pub fn create_namespace_child(
&self,
doc_node: Rc<DocNode>,
doc_node: Arc<DocNode>,
qualifiers: Rc<[String]>,
) -> Self {
let mut child = self.create_child(doc_node);
Expand All @@ -754,18 +770,17 @@ impl DocNodeWithContext {
&self,
mut method_doc_node: DocNode,
is_static: bool,
method_kind: MethodKind,
method_kind: deno_ast::swc::ast::MethodKind,
) -> Self {
let original_name = method_doc_node.name.clone();
method_doc_node.name =
qualify_drilldown_name(self.get_name(), &method_doc_node.name, is_static)
.into_boxed_str();
method_doc_node.declaration_kind = self.declaration_kind;

let mut new_node = self.create_child(Rc::new(method_doc_node));
let mut new_node = self.create_child(Arc::new(method_doc_node));
new_node.drilldown_name = Some(original_name);
new_node.kind_with_drilldown =
DocNodeKindWithDrilldown::Method(method_kind);
new_node.kind = DocNodeKind::Method(method_kind.into());
new_node
}

Expand All @@ -783,9 +798,9 @@ impl DocNodeWithContext {
.into_boxed_str();
property_doc_node.declaration_kind = self.declaration_kind;

let mut new_node = self.create_child(Rc::new(property_doc_node));
let mut new_node = self.create_child(Arc::new(property_doc_node));
new_node.drilldown_name = Some(original_name);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
new_node.kind = DocNodeKind::Property;
new_node
}

Expand Down
6 changes: 3 additions & 3 deletions src/html/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::STYLESHEET_FILENAME;

use crate::html::usage::UsagesCtx;
use crate::js_doc::JsDocTag;
use crate::DocNodeKind;
use crate::node::DocNodeDef;
use indexmap::IndexMap;
use serde::Serialize;

Expand Down Expand Up @@ -260,7 +260,7 @@ impl IndexCtx {
.map(|(short_path, nodes)| {
let doc = nodes
.iter()
.find(|node| node.kind() == DocNodeKind::ModuleDoc)
.find(|node| matches!(node.def, DocNodeDef::ModuleDoc))
.and_then(|node| {
crate::html::jsdoc::jsdoc_body_to_html(
&render_ctx,
Expand Down Expand Up @@ -552,7 +552,7 @@ pub fn generate_symbol_pages_for_module(

if doc_nodes
.iter()
.any(|doc_node| doc_node.kind() == DocNodeKind::Class)
.any(|doc_node| matches!(doc_node.def, DocNodeDef::Class { .. }))
{
let prototype_name = format!("{name}.prototype");
generated_pages.push(SymbolPage::Redirect {
Expand Down
Loading

0 comments on commit 6d6209b

Please sign in to comment.