summaryrefslogtreecommitdiff
path: root/cli/doc/function.rs
blob: fbfd2d01546b9f1197f43b85e2a81a666e507859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::swc_ecma_ast;
use serde::Serialize;

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 crate::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)
}