Skip to content

Commit

Permalink
feat: support Location for ts literals, render ts literals as interfa…
Browse files Browse the repository at this point in the history
…ces (#562)
  • Loading branch information
crowlKats committed Apr 26, 2024
1 parent 6ea914f commit 915782f
Show file tree
Hide file tree
Showing 34 changed files with 2,165 additions and 429 deletions.
32 changes: 3 additions & 29 deletions src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::params::prop_name_to_string;
use crate::params::ts_fn_param_to_param_def;
use crate::ts_type::infer_ts_type_from_expr;
use crate::ts_type::maybe_type_param_instantiation_to_type_defs;
use crate::ts_type::IndexSignatureDef;
use crate::ts_type::TsTypeDef;
use crate::ts_type_param::maybe_type_param_decl_to_type_param_defs;
use crate::ts_type_param::TsTypeParamDef;
Expand Down Expand Up @@ -159,33 +160,6 @@ impl Display for ClassPropertyDef {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClassIndexSignatureDef {
#[serde(skip_serializing_if = "JsDoc::is_empty", default)]
pub js_doc: JsDoc,
pub readonly: bool,
pub params: Vec<ParamDef>,
pub ts_type: Option<TsTypeDef>,
pub location: Location,
}

#[cfg(feature = "rust")]
impl Display for ClassIndexSignatureDef {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(
f,
"{}[{}]",
display_readonly(self.readonly),
SliceDisplayer::new(&self.params, ", ", false)
)?;
if let Some(ts_type) = &self.ts_type {
write!(f, ": {}", ts_type)?;
}
Ok(())
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ClassMethodDef {
Expand Down Expand Up @@ -248,7 +222,7 @@ pub struct ClassDef {
pub is_abstract: bool,
pub constructors: Vec<ClassConstructorDef>,
pub properties: Vec<ClassPropertyDef>,
pub index_signatures: Vec<ClassIndexSignatureDef>,
pub index_signatures: Vec<IndexSignatureDef>,
pub methods: Vec<ClassMethodDef>,
pub extends: Option<String>,
pub implements: Vec<TsTypeDef>,
Expand Down Expand Up @@ -434,7 +408,7 @@ pub fn class_to_class_def(
.as_ref()
.map(|rt| TsTypeDef::new(parsed_source, &rt.type_ann));

let index_sig_def = ClassIndexSignatureDef {
let index_sig_def = IndexSignatureDef {
location: get_location(parsed_source, ts_index_sig.start()),
js_doc,
readonly: ts_index_sig.readonly,
Expand Down
32 changes: 32 additions & 0 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,38 @@ impl DocNodeWithContext {
inner: doc_node,
}
}

pub fn create_child_method(
&self,
mut method_doc_node: DocNode,
parent_name: &str,
is_static: bool,
) -> Self {
method_doc_node.name =
qualify_drilldown_name(parent_name, &method_doc_node.name, is_static);
method_doc_node.declaration_kind = self.declaration_kind;

let mut new_node = self.create_child(Arc::new(method_doc_node));
new_node.drilldown_parent_kind = Some(self.kind);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
new_node
}

pub fn create_child_property(
&self,
mut property_doc_node: DocNode,
parent_name: &str,
is_static: bool,
) -> Self {
property_doc_node.name =
qualify_drilldown_name(parent_name, &property_doc_node.name, is_static);
property_doc_node.declaration_kind = self.declaration_kind;

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

impl core::ops::Deref for DocNodeWithContext {
Expand Down
116 changes: 56 additions & 60 deletions src/html/pages.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use super::sidepanels;
use super::sidepanels::SidepanelCtx;
use super::symbols::SymbolContentCtx;
use super::util::qualify_drilldown_name;
use super::util::BreadcrumbsCtx;
use super::util::SectionHeaderCtx;
use super::DocNodeKindWithDrilldown;
use super::DocNodeWithContext;
use super::FileMode;
use super::GenerateCtx;
use super::RenderContext;
use super::ShortPath;
use super::SymbolGroupCtx;
use super::UrlResolveKind;
use std::sync::Arc;

use super::FUSE_FILENAME;
use super::PAGE_STYLESHEET_FILENAME;
Expand All @@ -21,9 +18,7 @@ use super::SEARCH_FILENAME;
use super::SEARCH_INDEX_FILENAME;
use super::STYLESHEET_FILENAME;

use crate::function::FunctionDef;
use crate::html::partition::Partition;
use crate::variable::VariableDef;
use crate::DocNode;
use crate::DocNodeKind;
use indexmap::IndexMap;
Expand Down Expand Up @@ -233,17 +228,17 @@ pub fn generate_symbol_pages_for_module(
.methods
.iter()
.map(|method| {
let mut new_node =
doc_nodes[0].create_child(Arc::new(DocNode::function(
qualify_drilldown_name(name, &method.name, method.is_static),
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(),
)));
new_node.drilldown_parent_kind = Some(DocNodeKind::Class);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
new_node
),
name,
method.is_static,
)
})
.collect::<Vec<_>>();

Expand All @@ -254,20 +249,11 @@ pub fn generate_symbol_pages_for_module(
.properties
.iter()
.map(|property| {
let mut new_node =
doc_nodes[0].create_child(Arc::new(DocNode::variable(
qualify_drilldown_name(name, &property.name, property.is_static),
property.location.clone(),
doc_nodes[0].declaration_kind,
property.js_doc.clone(),
VariableDef {
ts_type: property.ts_type.clone(),
kind: deno_ast::swc::ast::VarDeclKind::Const,
},
)));
new_node.drilldown_parent_kind = Some(DocNodeKind::Class);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
new_node
doc_nodes[0].create_child_property(
DocNode::from(property.clone()),
name,
property.is_static,
)
})
.collect::<Vec<_>>();

Expand All @@ -279,26 +265,11 @@ pub fn generate_symbol_pages_for_module(
.methods
.iter()
.map(|method| {
let mut new_node =
doc_nodes[0].create_child(Arc::new(DocNode::function(
qualify_drilldown_name(name, &method.name, true),
method.location.clone(),
doc_nodes[0].declaration_kind,
method.js_doc.clone(),
FunctionDef {
def_name: None,
params: method.params.clone(),
return_type: method.return_type.clone(),
has_body: false,
is_async: false,
is_generator: false,
type_params: method.type_params.clone(),
decorators: vec![],
},
)));
new_node.drilldown_parent_kind = Some(DocNodeKind::Interface);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Method;
new_node
doc_nodes[0].create_child_method(
DocNode::from(method.clone()),
name,
true,
)
})
.collect::<Vec<_>>();

Expand All @@ -309,25 +280,50 @@ pub fn generate_symbol_pages_for_module(
.properties
.iter()
.map(|property| {
let mut new_node =
doc_nodes[0].create_child(Arc::new(DocNode::variable(
qualify_drilldown_name(name, &property.name, true),
property.location.clone(),
doc_nodes[0].declaration_kind,
property.js_doc.clone(),
VariableDef {
ts_type: property.ts_type.clone(),
kind: deno_ast::swc::ast::VarDeclKind::Const,
},
)));
new_node.drilldown_parent_kind = Some(DocNodeKind::Interface);
new_node.kind_with_drilldown = DocNodeKindWithDrilldown::Property;
new_node
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));
}
}
}
name_partitions.extend(drilldown_partitions);
Expand Down
40 changes: 1 addition & 39 deletions src/html/symbols/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn render_class(
}

if let Some(index_signatures) =
render_index_signatures(ctx, &class_def.index_signatures)
super::interface::render_index_signatures(ctx, &class_def.index_signatures)
{
sections.push(index_signatures);
}
Expand Down Expand Up @@ -154,44 +154,6 @@ pub struct IndexSignatureCtx {
pub source_href: Option<String>,
}

fn render_index_signatures(
ctx: &RenderContext,
index_signatures: &[crate::class::ClassIndexSignatureDef],
) -> Option<SectionCtx> {
if index_signatures.is_empty() {
return None;
}

let mut items = Vec::with_capacity(index_signatures.len());

for (i, index_signature) in index_signatures.iter().enumerate() {
let id = name_to_id("index_signature", &i.to_string());

let ts_type = index_signature
.ts_type
.as_ref()
.map(|ts_type| render_type_def_colon(ctx, ts_type))
.unwrap_or_default();

items.push(IndexSignatureCtx {
id: id.clone(),
anchor: AnchorCtx { id },
readonly: index_signature.readonly,
params: render_params(ctx, &index_signature.params),
ts_type,
source_href: ctx
.ctx
.href_resolver
.resolve_source(&index_signature.location),
});
}

Some(SectionCtx::new(
"Index Signatures",
SectionContentCtx::IndexSignature(items),
))
}

enum PropertyOrMethod {
Property(ClassPropertyDef),
Method(ClassMethodDef),
Expand Down
16 changes: 8 additions & 8 deletions src/html/symbols/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ pub(crate) fn render_interface(
sections
}

fn render_index_signatures(
pub fn render_index_signatures(
ctx: &RenderContext,
index_signatures: &[crate::interface::InterfaceIndexSignatureDef],
index_signatures: &[crate::ts_type::IndexSignatureDef],
) -> Option<SectionCtx> {
if index_signatures.is_empty() {
return None;
Expand Down Expand Up @@ -95,9 +95,9 @@ fn render_index_signatures(
))
}

fn render_call_signatures(
pub fn render_call_signatures(
ctx: &RenderContext,
call_signatures: &[crate::interface::InterfaceCallSignatureDef],
call_signatures: &[crate::ts_type::CallSignatureDef],
) -> Option<SectionCtx> {
if call_signatures.is_empty() {
return None;
Expand Down Expand Up @@ -140,10 +140,10 @@ fn render_call_signatures(
))
}

fn render_properties(
pub fn render_properties(
ctx: &RenderContext,
interface_name: &str,
properties: &[crate::interface::InterfacePropertyDef],
properties: &[crate::ts_type::PropertyDef],
) -> Option<SectionCtx> {
if properties.is_empty() {
return None;
Expand Down Expand Up @@ -210,10 +210,10 @@ fn render_properties(
))
}

fn render_methods(
pub fn render_methods(
ctx: &RenderContext,
interface_name: &str,
methods: &[crate::interface::InterfaceMethodDef],
methods: &[crate::ts_type::MethodDef],
) -> Option<SectionCtx> {
if methods.is_empty() {
return None;
Expand Down
Loading

0 comments on commit 915782f

Please sign in to comment.