diff options
author | Valentin Anger <syrupthinker@gryphno.de> | 2020-07-12 14:16:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-12 14:16:33 +0200 |
commit | 3374c73fba3a96df22d0c04e6c17078ca8cce45b (patch) | |
tree | 91d996554a2e276724a86ecb3bff19ea5411238b /cli/doc/interface.rs | |
parent | 871f9255e37b4d2e63439c84da8e9bed6b388034 (diff) |
feat(doc): Improve terminal printer (#6594)
- Add more support for generics
- Add the --private flag - displays documentation for
not exported and private nodes
- Display more attributes like abstract, static and readonly
- Display type aliases
- Refactor module to use the Display trait
- Use a bit more color
Diffstat (limited to 'cli/doc/interface.rs')
-rw-r--r-- | cli/doc/interface.rs | 105 |
1 files changed, 96 insertions, 9 deletions
diff --git a/cli/doc/interface.rs b/cli/doc/interface.rs index 9e3c1fc90..d3caa8748 100644 --- a/cli/doc/interface.rs +++ b/cli/doc/interface.rs @@ -1,10 +1,11 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +use crate::colors; +use crate::doc::display::{display_optional, display_readonly, SliceDisplayer}; use crate::swc_ecma_ast; use serde::Serialize; use super::params::ts_fn_param_to_param_def; use super::parser::DocParser; -use super::ts_type::ts_entity_name_to_name; use super::ts_type::ts_type_ann_to_def; use super::ts_type::TsTypeDef; use super::ts_type_param::maybe_type_param_decl_to_type_param_defs; @@ -12,6 +13,8 @@ use super::ts_type_param::TsTypeParamDef; use super::Location; use super::ParamDef; +use std::fmt::{Display, Formatter, Result as FmtResult}; + #[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct InterfaceMethodDef { @@ -24,6 +27,22 @@ pub struct InterfaceMethodDef { pub type_params: Vec<TsTypeParamDef>, } +impl Display for InterfaceMethodDef { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + write!( + f, + "{}{}({})", + colors::bold(&self.name), + display_optional(self.optional), + SliceDisplayer::new(&self.params, ", ", false), + )?; + if let Some(return_type) = &self.return_type { + write!(f, ": {}", return_type)?; + } + Ok(()) + } +} + #[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct InterfacePropertyDef { @@ -37,6 +56,44 @@ pub struct InterfacePropertyDef { pub type_params: Vec<TsTypeParamDef>, } +impl Display for InterfacePropertyDef { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + write!( + f, + "{}{}", + colors::bold(&self.name), + display_optional(self.optional), + )?; + if let Some(ts_type) = &self.ts_type { + write!(f, ": {}", ts_type)?; + } + Ok(()) + } +} + +#[derive(Debug, Serialize, Clone)] +#[serde(rename_all = "camelCase")] +pub struct InterfaceIndexSignatureDef { + pub readonly: bool, + pub params: Vec<ParamDef>, + pub ts_type: Option<TsTypeDef>, +} + +impl Display for InterfaceIndexSignatureDef { + 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, Clone)] #[serde(rename_all = "camelCase")] pub struct InterfaceCallSignatureDef { @@ -50,10 +107,11 @@ pub struct InterfaceCallSignatureDef { #[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] pub struct InterfaceDef { - pub extends: Vec<String>, + pub extends: Vec<TsTypeDef>, pub methods: Vec<InterfaceMethodDef>, pub properties: Vec<InterfacePropertyDef>, pub call_signatures: Vec<InterfaceCallSignatureDef>, + pub index_signatures: Vec<InterfaceIndexSignatureDef>, pub type_params: Vec<TsTypeParamDef>, } @@ -84,6 +142,7 @@ pub fn get_doc_for_ts_interface_decl( let mut methods = vec![]; let mut properties = vec![]; let mut call_signatures = vec![]; + let mut index_signatures = vec![]; for type_element in &interface_decl.body.body { use crate::swc_ecma_ast::TsTypeElement::*; @@ -95,7 +154,10 @@ pub fn get_doc_for_ts_interface_decl( let mut params = vec![]; for param in &ts_method_sig.params { - let param_def = ts_fn_param_to_param_def(param); + let param_def = ts_fn_param_to_param_def( + param, + Some(&doc_parser.ast_parser.source_map), + ); params.push(param_def); } @@ -131,7 +193,10 @@ pub fn get_doc_for_ts_interface_decl( let mut params = vec![]; for param in &ts_prop_sig.params { - let param_def = ts_fn_param_to_param_def(param); + let param_def = ts_fn_param_to_param_def( + param, + Some(&doc_parser.ast_parser.source_map), + ); params.push(param_def); } @@ -164,7 +229,10 @@ pub fn get_doc_for_ts_interface_decl( let mut params = vec![]; for param in &ts_call_sig.params { - let param_def = ts_fn_param_to_param_def(param); + let param_def = ts_fn_param_to_param_def( + param, + Some(&doc_parser.ast_parser.source_map), + ); params.push(param_def); } @@ -189,9 +257,27 @@ pub fn get_doc_for_ts_interface_decl( }; call_signatures.push(call_sig_def); } + TsIndexSignature(ts_index_sig) => { + let mut params = vec![]; + for param in &ts_index_sig.params { + let param_def = ts_fn_param_to_param_def(param, None); + params.push(param_def); + } + + let ts_type = ts_index_sig + .type_ann + .as_ref() + .map(|rt| (&*rt.type_ann).into()); + + let index_sig_def = InterfaceIndexSignatureDef { + readonly: ts_index_sig.readonly, + params, + ts_type, + }; + index_signatures.push(index_sig_def); + } // TODO: TsConstructSignatureDecl(_) => {} - TsIndexSignature(_) => {} } } @@ -199,17 +285,18 @@ pub fn get_doc_for_ts_interface_decl( interface_decl.type_params.as_ref(), ); - let extends: Vec<String> = interface_decl + let extends = interface_decl .extends .iter() - .map(|expr| ts_entity_name_to_name(&expr.expr)) - .collect(); + .map(|expr| expr.into()) + .collect::<Vec<TsTypeDef>>(); let interface_def = InterfaceDef { extends, methods, properties, call_signatures, + index_signatures, type_params, }; |