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
This commit is contained in:
Valentin Anger 2020-07-12 14:16:33 +02:00 committed by GitHub
parent 871f9255e3
commit 3374c73fba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 2888 additions and 1743 deletions

View file

@ -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,
};