summaryrefslogtreecommitdiff
path: root/cli/doc/function.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-03-28 19:16:57 +0100
committerGitHub <noreply@github.com>2020-03-28 14:16:57 -0400
commit3fac487461abf055165fe0e2bb962573950277b8 (patch)
treec0ba09a2049975f9eb8625365e3ba395e67d8b50 /cli/doc/function.rs
parentbced52505f32d6cca4f944bb610a8a26767908a8 (diff)
feat: Add "deno doc" subcommand (#4500)
Diffstat (limited to 'cli/doc/function.rs')
-rw-r--r--cli/doc/function.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/cli/doc/function.rs b/cli/doc/function.rs
new file mode 100644
index 000000000..ec7f9bf38
--- /dev/null
+++ b/cli/doc/function.rs
@@ -0,0 +1,70 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use serde::Serialize;
+use swc_ecma_ast;
+
+use super::parser::DocParser;
+use super::ts_type::ts_type_ann_to_def;
+use super::ts_type::TsTypeDef;
+use super::ParamDef;
+
+#[derive(Debug, Serialize, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct FunctionDef {
+ pub params: Vec<ParamDef>,
+ pub return_type: Option<TsTypeDef>,
+ pub is_async: bool,
+ pub is_generator: bool,
+ // TODO: type_params, decorators
+}
+
+pub fn function_to_function_def(
+ doc_parser: &DocParser,
+ function: &swc_ecma_ast::Function,
+) -> FunctionDef {
+ let mut params = vec![];
+
+ for param in &function.params {
+ use swc_ecma_ast::Pat;
+
+ let param_def = match param {
+ Pat::Ident(ident) => {
+ let ts_type = ident
+ .type_ann
+ .as_ref()
+ .map(|rt| ts_type_ann_to_def(&doc_parser.source_map, rt));
+
+ ParamDef {
+ name: ident.sym.to_string(),
+ ts_type,
+ }
+ }
+ _ => ParamDef {
+ name: "<TODO>".to_string(),
+ ts_type: None,
+ },
+ };
+
+ params.push(param_def);
+ }
+
+ let maybe_return_type = function
+ .return_type
+ .as_ref()
+ .map(|rt| ts_type_ann_to_def(&doc_parser.source_map, rt));
+
+ FunctionDef {
+ params,
+ return_type: maybe_return_type,
+ is_async: function.is_async,
+ is_generator: function.is_generator,
+ }
+}
+
+pub fn get_doc_for_fn_decl(
+ doc_parser: &DocParser,
+ fn_decl: &swc_ecma_ast::FnDecl,
+) -> (String, FunctionDef) {
+ let name = fn_decl.ident.sym.to_string();
+ let fn_def = function_to_function_def(doc_parser, &fn_decl.function);
+ (name, fn_def)
+}