mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 06:09:25 -05:00
refactor(doc): add helper functions for DocNode (#7083)
This commit is contained in:
parent
93e2bfe22e
commit
9f1dcc1a32
4 changed files with 201 additions and 267 deletions
|
@ -3,7 +3,6 @@ use swc_common::Spanned;
|
|||
|
||||
use super::parser::DocParser;
|
||||
use super::DocNode;
|
||||
use super::DocNodeKind;
|
||||
|
||||
pub fn get_doc_node_for_export_decl(
|
||||
doc_parser: &DocParser,
|
||||
|
@ -19,55 +18,16 @@ pub fn get_doc_node_for_export_decl(
|
|||
Decl::Class(class_decl) => {
|
||||
let (name, class_def) =
|
||||
super::class::get_doc_for_class_decl(doc_parser, class_decl);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Class,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
class_def: Some(class_def),
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::class(name, location, js_doc, class_def)
|
||||
}
|
||||
Decl::Fn(fn_decl) => {
|
||||
let (name, function_def) =
|
||||
super::function::get_doc_for_fn_decl(doc_parser, fn_decl);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Function,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: Some(function_def),
|
||||
class_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::function(name, location, js_doc, function_def)
|
||||
}
|
||||
Decl::Var(var_decl) => {
|
||||
let (name, var_def) = super::variable::get_doc_for_var_decl(var_decl);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Variable,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
variable_def: Some(var_def),
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::variable(name, location, js_doc, var_def)
|
||||
}
|
||||
Decl::TsInterface(ts_interface_decl) => {
|
||||
let (name, interface_def) =
|
||||
|
@ -75,20 +35,7 @@ pub fn get_doc_node_for_export_decl(
|
|||
doc_parser,
|
||||
ts_interface_decl,
|
||||
);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Interface,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
interface_def: Some(interface_def),
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::interface(name, location, js_doc, interface_def)
|
||||
}
|
||||
Decl::TsTypeAlias(ts_type_alias) => {
|
||||
let (name, type_alias_def) =
|
||||
|
@ -96,56 +43,17 @@ pub fn get_doc_node_for_export_decl(
|
|||
doc_parser,
|
||||
ts_type_alias,
|
||||
);
|
||||
DocNode {
|
||||
kind: DocNodeKind::TypeAlias,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
type_alias_def: Some(type_alias_def),
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::type_alias(name, location, js_doc, type_alias_def)
|
||||
}
|
||||
Decl::TsEnum(ts_enum) => {
|
||||
let (name, enum_def) =
|
||||
super::r#enum::get_doc_for_ts_enum_decl(doc_parser, ts_enum);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Enum,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
enum_def: Some(enum_def),
|
||||
type_alias_def: None,
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::r#enum(name, location, js_doc, enum_def)
|
||||
}
|
||||
Decl::TsModule(ts_module) => {
|
||||
let (name, namespace_def) =
|
||||
super::namespace::get_doc_for_ts_module(doc_parser, ts_module);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Namespace,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
namespace_def: Some(namespace_def),
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::namespace(name, location, js_doc, namespace_def)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ use serde::Serialize;
|
|||
|
||||
use super::parser::DocParser;
|
||||
use super::DocNode;
|
||||
use super::DocNodeKind;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct NamespaceDef {
|
||||
|
@ -34,20 +33,7 @@ pub fn get_doc_for_ts_namespace_decl(
|
|||
|
||||
let ns_def = NamespaceDef { elements };
|
||||
|
||||
DocNode {
|
||||
kind: DocNodeKind::Namespace,
|
||||
name: namespace_name,
|
||||
location,
|
||||
js_doc,
|
||||
namespace_def: Some(ns_def),
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::namespace(namespace_name, location, js_doc, ns_def)
|
||||
}
|
||||
|
||||
pub fn get_doc_for_ts_module(
|
||||
|
|
178
cli/doc/node.rs
178
cli/doc/node.rs
|
@ -107,3 +107,181 @@ pub struct DocNode {
|
|||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub import_def: Option<ImportDef>,
|
||||
}
|
||||
|
||||
impl DocNode {
|
||||
pub fn function(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
fn_def: super::function::FunctionDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Function,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: Some(fn_def),
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn variable(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
var_def: super::variable::VariableDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Variable,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: Some(var_def),
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#enum(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
enum_def: super::r#enum::EnumDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Enum,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: Some(enum_def),
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn class(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
class_def: super::class::ClassDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Class,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: Some(class_def),
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn type_alias(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
type_alias_def: super::type_alias::TypeAliasDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::TypeAlias,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: Some(type_alias_def),
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn namespace(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
namespace_def: super::namespace::NamespaceDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Namespace,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: Some(namespace_def),
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn interface(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
interface_def: super::interface::InterfaceDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Interface,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
class_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: Some(interface_def),
|
||||
import_def: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn import(
|
||||
name: String,
|
||||
location: Location,
|
||||
js_doc: Option<String>,
|
||||
import_def: ImportDef,
|
||||
) -> Self {
|
||||
Self {
|
||||
kind: DocNodeKind::Import,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
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(import_def),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,20 +240,12 @@ impl DocParser {
|
|||
imported: maybe_imported_name,
|
||||
};
|
||||
|
||||
let doc_node = DocNode {
|
||||
kind: DocNodeKind::Import,
|
||||
let doc_node = DocNode::import(
|
||||
name,
|
||||
location: location.clone(),
|
||||
js_doc: js_doc.clone(),
|
||||
import_def: Some(import_def),
|
||||
class_def: None,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
};
|
||||
location.clone(),
|
||||
js_doc.clone(),
|
||||
import_def,
|
||||
);
|
||||
|
||||
imports.push(doc_node);
|
||||
}
|
||||
|
@ -284,40 +276,14 @@ impl DocParser {
|
|||
DefaultDecl::Class(class_expr) => {
|
||||
let class_def =
|
||||
crate::doc::class::class_to_class_def(self, &class_expr.class);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Class,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
class_def: Some(class_def),
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::class(name, location, js_doc, class_def)
|
||||
}
|
||||
DefaultDecl::Fn(fn_expr) => {
|
||||
let function_def = crate::doc::function::function_to_function_def(
|
||||
self,
|
||||
&fn_expr.function,
|
||||
);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Function,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
class_def: None,
|
||||
function_def: Some(function_def),
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::function(name, location, js_doc, function_def)
|
||||
}
|
||||
DefaultDecl::TsInterfaceDecl(interface_decl) => {
|
||||
let (_, interface_def) =
|
||||
|
@ -325,20 +291,7 @@ impl DocParser {
|
|||
self,
|
||||
interface_decl,
|
||||
);
|
||||
DocNode {
|
||||
kind: DocNodeKind::Interface,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
class_def: None,
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: Some(interface_def),
|
||||
import_def: None,
|
||||
}
|
||||
DocNode::interface(name, location, js_doc, interface_def)
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -371,20 +324,7 @@ impl DocParser {
|
|||
let (name, class_def) =
|
||||
super::class::get_doc_for_class_decl(self, class_decl);
|
||||
let (js_doc, location) = self.details_for_span(class_decl.class.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Class,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
class_def: Some(class_def),
|
||||
function_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::class(name, location, js_doc, class_def))
|
||||
}
|
||||
Decl::Fn(fn_decl) => {
|
||||
if !self.private && !fn_decl.declare {
|
||||
|
@ -393,20 +333,7 @@ impl DocParser {
|
|||
let (name, function_def) =
|
||||
super::function::get_doc_for_fn_decl(self, fn_decl);
|
||||
let (js_doc, location) = self.details_for_span(fn_decl.function.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Function,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
function_def: Some(function_def),
|
||||
class_def: None,
|
||||
variable_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::function(name, location, js_doc, function_def))
|
||||
}
|
||||
Decl::Var(var_decl) => {
|
||||
if !self.private && !var_decl.declare {
|
||||
|
@ -414,20 +341,7 @@ impl DocParser {
|
|||
}
|
||||
let (name, var_def) = super::variable::get_doc_for_var_decl(var_decl);
|
||||
let (js_doc, location) = self.details_for_span(var_decl.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Variable,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
variable_def: Some(var_def),
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
interface_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::variable(name, location, js_doc, var_def))
|
||||
}
|
||||
Decl::TsInterface(ts_interface_decl) => {
|
||||
if !self.private && !ts_interface_decl.declare {
|
||||
|
@ -439,20 +353,7 @@ impl DocParser {
|
|||
ts_interface_decl,
|
||||
);
|
||||
let (js_doc, location) = self.details_for_span(ts_interface_decl.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Interface,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
interface_def: Some(interface_def),
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::interface(name, location, js_doc, interface_def))
|
||||
}
|
||||
Decl::TsTypeAlias(ts_type_alias) => {
|
||||
if !self.private && !ts_type_alias.declare {
|
||||
|
@ -464,20 +365,7 @@ impl DocParser {
|
|||
ts_type_alias,
|
||||
);
|
||||
let (js_doc, location) = self.details_for_span(ts_type_alias.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::TypeAlias,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
type_alias_def: Some(type_alias_def),
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
enum_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::type_alias(name, location, js_doc, type_alias_def))
|
||||
}
|
||||
Decl::TsEnum(ts_enum) => {
|
||||
if !self.private && !ts_enum.declare {
|
||||
|
@ -486,20 +374,7 @@ impl DocParser {
|
|||
let (name, enum_def) =
|
||||
super::r#enum::get_doc_for_ts_enum_decl(self, ts_enum);
|
||||
let (js_doc, location) = self.details_for_span(ts_enum.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Enum,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
enum_def: Some(enum_def),
|
||||
type_alias_def: None,
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
namespace_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::r#enum(name, location, js_doc, enum_def))
|
||||
}
|
||||
Decl::TsModule(ts_module) => {
|
||||
if !self.private && !ts_module.declare {
|
||||
|
@ -508,20 +383,7 @@ impl DocParser {
|
|||
let (name, namespace_def) =
|
||||
super::namespace::get_doc_for_ts_module(self, ts_module);
|
||||
let (js_doc, location) = self.details_for_span(ts_module.span);
|
||||
Some(DocNode {
|
||||
kind: DocNodeKind::Namespace,
|
||||
name,
|
||||
location,
|
||||
js_doc,
|
||||
namespace_def: Some(namespace_def),
|
||||
enum_def: None,
|
||||
type_alias_def: None,
|
||||
interface_def: None,
|
||||
variable_def: None,
|
||||
function_def: None,
|
||||
class_def: None,
|
||||
import_def: None,
|
||||
})
|
||||
Some(DocNode::namespace(name, location, js_doc, namespace_def))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue