diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-04-08 17:03:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-08 17:03:42 +0200 |
commit | 491b8e1cea76753397bdeb0aeb1598bc78d22c8f (patch) | |
tree | 2eadbbcd2efe744c222c0d1b161a0131e498848b /cli/doc/params.rs | |
parent | fe17496831a0b3dcd252d097c82df529d665aad5 (diff) |
feat(doc): handle function params and type params (#4672)
Diffstat (limited to 'cli/doc/params.rs')
-rw-r--r-- | cli/doc/params.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/cli/doc/params.rs b/cli/doc/params.rs new file mode 100644 index 000000000..04fd7f898 --- /dev/null +++ b/cli/doc/params.rs @@ -0,0 +1,83 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +use crate::swc_ecma_ast; + +use super::ts_type::ts_type_ann_to_def; +use super::ParamDef; +use super::ParamKind; +use crate::swc_ecma_ast::Pat; +use crate::swc_ecma_ast::TsFnParam; + +pub fn ident_to_param_def(ident: &swc_ecma_ast::Ident) -> ParamDef { + let ts_type = ident.type_ann.as_ref().map(|rt| ts_type_ann_to_def(rt)); + + ParamDef { + name: ident.sym.to_string(), + kind: ParamKind::Identifier, + ts_type, + } +} + +fn rest_pat_to_param_def(rest_pat: &swc_ecma_ast::RestPat) -> ParamDef { + let name = match &*rest_pat.arg { + Pat::Ident(ident) => ident.sym.to_string(), + _ => "<TODO>".to_string(), + }; + let ts_type = rest_pat.type_ann.as_ref().map(|rt| ts_type_ann_to_def(rt)); + + ParamDef { + name, + kind: ParamKind::Rest, + ts_type, + } +} + +fn object_pat_to_param_def(object_pat: &swc_ecma_ast::ObjectPat) -> ParamDef { + let ts_type = object_pat + .type_ann + .as_ref() + .map(|rt| ts_type_ann_to_def(rt)); + + ParamDef { + name: "".to_string(), + kind: ParamKind::Object, + ts_type, + } +} + +fn array_pat_to_param_def(array_pat: &swc_ecma_ast::ArrayPat) -> ParamDef { + let ts_type = array_pat.type_ann.as_ref().map(|rt| ts_type_ann_to_def(rt)); + + ParamDef { + name: "".to_string(), + kind: ParamKind::Array, + ts_type, + } +} + +pub fn assign_pat_to_param_def( + assign_pat: &swc_ecma_ast::AssignPat, +) -> ParamDef { + pat_to_param_def(&*assign_pat.left) +} + +pub fn pat_to_param_def(pat: &swc_ecma_ast::Pat) -> ParamDef { + match pat { + Pat::Ident(ident) => ident_to_param_def(ident), + Pat::Array(array_pat) => array_pat_to_param_def(array_pat), + Pat::Rest(rest_pat) => rest_pat_to_param_def(rest_pat), + Pat::Object(object_pat) => object_pat_to_param_def(object_pat), + Pat::Assign(assign_pat) => assign_pat_to_param_def(assign_pat), + _ => unreachable!(), + } +} + +pub fn ts_fn_param_to_param_def( + ts_fn_param: &swc_ecma_ast::TsFnParam, +) -> ParamDef { + match ts_fn_param { + TsFnParam::Ident(ident) => ident_to_param_def(ident), + TsFnParam::Array(array_pat) => array_pat_to_param_def(array_pat), + TsFnParam::Rest(rest_pat) => rest_pat_to_param_def(rest_pat), + TsFnParam::Object(object_pat) => object_pat_to_param_def(object_pat), + } +} |