summaryrefslogtreecommitdiff
path: root/cli/doc/ts_type.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-04-08 17:03:42 +0200
committerGitHub <noreply@github.com>2020-04-08 17:03:42 +0200
commit491b8e1cea76753397bdeb0aeb1598bc78d22c8f (patch)
tree2eadbbcd2efe744c222c0d1b161a0131e498848b /cli/doc/ts_type.rs
parentfe17496831a0b3dcd252d097c82df529d665aad5 (diff)
feat(doc): handle function params and type params (#4672)
Diffstat (limited to 'cli/doc/ts_type.rs')
-rw-r--r--cli/doc/ts_type.rs177
1 files changed, 48 insertions, 129 deletions
diff --git a/cli/doc/ts_type.rs b/cli/doc/ts_type.rs
index 9590c7e60..8359f64cc 100644
--- a/cli/doc/ts_type.rs
+++ b/cli/doc/ts_type.rs
@@ -1,6 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use super::params::ts_fn_param_to_param_def;
+use super::ts_type_param::maybe_type_param_decl_to_type_param_defs;
+use super::ts_type_param::TsTypeParamDef;
use super::ParamDef;
-use crate::swc_common::SourceMap;
use crate::swc_ecma_ast;
use crate::swc_ecma_ast::TsArrayType;
use crate::swc_ecma_ast::TsConditionalType;
@@ -36,12 +38,12 @@ use serde::Serialize;
// * TsRestType(TsRestType),
// * TsUnionOrIntersectionType(TsUnionOrIntersectionType),
// * TsConditionalType(TsConditionalType),
-// TsInferType(TsInferType),
// * TsParenthesizedType(TsParenthesizedType),
// * TsTypeOperator(TsTypeOperator),
// * TsIndexedAccessType(TsIndexedAccessType),
-// TsMappedType(TsMappedType),
// * TsLitType(TsLitType),
+// TsInferType(TsInferType),
+// TsMappedType(TsMappedType),
// TsTypePredicate(TsTypePredicate),
// TsImportType(TsImportType),
// }
@@ -340,24 +342,7 @@ impl Into<TsTypeDef> for &TsTypeLit {
let mut params = vec![];
for param in &ts_method_sig.params {
- use crate::swc_ecma_ast::TsFnParam::*;
-
- let param_def = match param {
- Ident(ident) => {
- let ts_type =
- ident.type_ann.as_ref().map(|rt| (&*rt.type_ann).into());
-
- ParamDef {
- name: ident.sym.to_string(),
- ts_type,
- }
- }
- _ => ParamDef {
- name: "<TODO>".to_string(),
- ts_type: None,
- },
- };
-
+ let param_def = ts_fn_param_to_param_def(param);
params.push(param_def);
}
@@ -366,10 +351,14 @@ impl Into<TsTypeDef> for &TsTypeLit {
.as_ref()
.map(|rt| (&*rt.type_ann).into());
+ let type_params = maybe_type_param_decl_to_type_param_defs(
+ ts_method_sig.type_params.as_ref(),
+ );
let method_def = LiteralMethodDef {
name: "<TODO>".to_string(),
params,
return_type: maybe_return_type,
+ type_params,
};
methods.push(method_def);
}
@@ -382,24 +371,7 @@ impl Into<TsTypeDef> for &TsTypeLit {
let mut params = vec![];
for param in &ts_prop_sig.params {
- use crate::swc_ecma_ast::TsFnParam::*;
-
- let param_def = match param {
- Ident(ident) => {
- let ts_type =
- ident.type_ann.as_ref().map(|rt| (&*rt.type_ann).into());
-
- ParamDef {
- name: ident.sym.to_string(),
- ts_type,
- }
- }
- _ => ParamDef {
- name: "<TODO>".to_string(),
- ts_type: None,
- },
- };
-
+ let param_def = ts_fn_param_to_param_def(param);
params.push(param_def);
}
@@ -408,36 +380,23 @@ impl Into<TsTypeDef> for &TsTypeLit {
.as_ref()
.map(|rt| (&*rt.type_ann).into());
+ let type_params = maybe_type_param_decl_to_type_param_defs(
+ ts_prop_sig.type_params.as_ref(),
+ );
let prop_def = LiteralPropertyDef {
name,
params,
ts_type,
computed: ts_prop_sig.computed,
optional: ts_prop_sig.optional,
+ type_params,
};
properties.push(prop_def);
}
TsCallSignatureDecl(ts_call_sig) => {
let mut params = vec![];
for param in &ts_call_sig.params {
- use crate::swc_ecma_ast::TsFnParam::*;
-
- let param_def = match param {
- Ident(ident) => {
- let ts_type =
- ident.type_ann.as_ref().map(|rt| (&*rt.type_ann).into());
-
- ParamDef {
- name: ident.sym.to_string(),
- ts_type,
- }
- }
- _ => ParamDef {
- name: "<TODO>".to_string(),
- ts_type: None,
- },
- };
-
+ let param_def = ts_fn_param_to_param_def(param);
params.push(param_def);
}
@@ -446,7 +405,15 @@ impl Into<TsTypeDef> for &TsTypeLit {
.as_ref()
.map(|rt| (&*rt.type_ann).into());
- let call_sig_def = LiteralCallSignatureDef { params, ts_type };
+ let type_params = maybe_type_param_decl_to_type_param_defs(
+ ts_call_sig.type_params.as_ref(),
+ );
+
+ let call_sig_def = LiteralCallSignatureDef {
+ params,
+ ts_type,
+ type_params,
+ };
call_signatures.push(call_sig_def);
}
// TODO:
@@ -495,68 +462,37 @@ impl Into<TsTypeDef> for &TsFnOrConstructorType {
let mut params = vec![];
for param in &ts_fn_type.params {
- use crate::swc_ecma_ast::TsFnParam::*;
-
- let param_def = match param {
- Ident(ident) => {
- let ts_type: Option<TsTypeDef> =
- ident.type_ann.as_ref().map(|rt| {
- let type_box = &*rt.type_ann;
- (&*type_box).into()
- });
-
- ParamDef {
- name: ident.sym.to_string(),
- ts_type,
- }
- }
- _ => ParamDef {
- name: "<TODO>".to_string(),
- ts_type: None,
- },
- };
-
+ let param_def = ts_fn_param_to_param_def(param);
params.push(param_def);
}
+ let type_params = maybe_type_param_decl_to_type_param_defs(
+ ts_fn_type.type_params.as_ref(),
+ );
+
TsFnOrConstructorDef {
constructor: false,
- ts_type: (&*ts_fn_type.type_ann.type_ann).into(),
+ ts_type: ts_type_ann_to_def(&ts_fn_type.type_ann),
params,
+ type_params,
}
}
TsConstructorType(ctor_type) => {
let mut params = vec![];
for param in &ctor_type.params {
- use crate::swc_ecma_ast::TsFnParam::*;
-
- let param_def = match param {
- Ident(ident) => {
- let ts_type: Option<TsTypeDef> =
- ident.type_ann.as_ref().map(|rt| {
- let type_box = &*rt.type_ann;
- (&*type_box).into()
- });
-
- ParamDef {
- name: ident.sym.to_string(),
- ts_type,
- }
- }
- _ => ParamDef {
- name: "<TODO>".to_string(),
- ts_type: None,
- },
- };
-
+ let param_def = ts_fn_param_to_param_def(param);
params.push(param_def);
}
+ let type_params = maybe_type_param_decl_to_type_param_defs(
+ ctor_type.type_params.as_ref(),
+ );
TsFnOrConstructorDef {
constructor: true,
- ts_type: (&*ctor_type.type_ann.type_ann).into(),
- params: vec![],
+ ts_type: ts_type_ann_to_def(&ctor_type.type_ann),
+ params,
+ type_params,
}
}
};
@@ -638,10 +574,10 @@ pub struct TsTypeOperatorDef {
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TsFnOrConstructorDef {
- // TODO: type_params
pub constructor: bool,
pub ts_type: TsTypeDef,
pub params: Vec<ParamDef>,
+ pub type_params: Vec<TsTypeParamDef>,
}
#[derive(Debug, Serialize, Clone)]
@@ -664,35 +600,29 @@ pub struct TsIndexedAccessDef {
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LiteralMethodDef {
- // TODO: type_params
pub name: String,
- // pub location: Location,
- // pub js_doc: Option<String>,
pub params: Vec<ParamDef>,
pub return_type: Option<TsTypeDef>,
+ pub type_params: Vec<TsTypeParamDef>,
}
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LiteralPropertyDef {
- // TODO: type_params
pub name: String,
- // pub location: Location,
- // pub js_doc: Option<String>,
pub params: Vec<ParamDef>,
pub computed: bool,
pub optional: bool,
pub ts_type: Option<TsTypeDef>,
+ pub type_params: Vec<TsTypeParamDef>,
}
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct LiteralCallSignatureDef {
- // TODO: type_params
- // pub location: Location,
- // pub js_doc: Option<String>,
pub params: Vec<ParamDef>,
pub ts_type: Option<TsTypeDef>,
+ pub type_params: Vec<TsTypeParamDef>,
}
#[derive(Debug, Serialize, Clone)]
@@ -732,7 +662,6 @@ pub struct TsTypeDef {
pub kind: Option<TsTypeDefKind>,
- // TODO: make this struct more conrete
#[serde(skip_serializing_if = "Option::is_none")]
pub keyword: Option<String>,
@@ -785,10 +714,7 @@ pub struct TsTypeDef {
pub type_literal: Option<TsTypeLiteralDef>,
}
-pub fn ts_type_ann_to_def(
- source_map: &SourceMap,
- type_ann: &TsTypeAnn,
-) -> TsTypeDef {
+pub fn ts_type_ann_to_def(type_ann: &TsTypeAnn) -> TsTypeDef {
use crate::swc_ecma_ast::TsType::*;
match &*type_ann.type_ann {
@@ -808,16 +734,9 @@ pub fn ts_type_ann_to_def(
TsConditionalType(conditional_type) => conditional_type.into(),
TsIndexedAccessType(indexed_access_type) => indexed_access_type.into(),
TsTypeLit(type_literal) => type_literal.into(),
- _ => {
- let repr = source_map
- .span_to_snippet(type_ann.span)
- .expect("Class prop type not found");
- let repr = repr.trim_start_matches(':').trim_start().to_string();
-
- TsTypeDef {
- repr,
- ..Default::default()
- }
- }
+ _ => TsTypeDef {
+ repr: "<TODO>".to_string(),
+ ..Default::default()
+ },
}
}