summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/doc/class.rs7
-rw-r--r--cli/doc/interface.rs9
-rw-r--r--cli/doc/tests.rs131
-rw-r--r--cli/doc/ts_type.rs10
-rw-r--r--cli/doc/variable.rs1
5 files changed, 123 insertions, 35 deletions
diff --git a/cli/doc/class.rs b/cli/doc/class.rs
index c4603972b..2370dd18f 100644
--- a/cli/doc/class.rs
+++ b/cli/doc/class.rs
@@ -6,6 +6,7 @@ use serde::Serialize;
use super::function::function_to_function_def;
use super::function::FunctionDef;
+use super::interface::expr_to_name;
use super::params::assign_pat_to_param_def;
use super::params::ident_to_param_def;
use super::params::pat_to_param_def;
@@ -181,11 +182,7 @@ pub fn get_doc_for_class_decl(
.as_ref()
.map(|rt| ts_type_ann_to_def(rt));
- use crate::swc_ecma_ast::Expr;
- let prop_name = match &*class_prop.key {
- Expr::Ident(ident) => ident.sym.to_string(),
- _ => "<TODO>".to_string(),
- };
+ let prop_name = expr_to_name(&*class_prop.key);
let prop_def = ClassPropertyDef {
js_doc: prop_js_doc,
diff --git a/cli/doc/interface.rs b/cli/doc/interface.rs
index fb7a2c853..9e3c1fc90 100644
--- a/cli/doc/interface.rs
+++ b/cli/doc/interface.rs
@@ -57,7 +57,7 @@ pub struct InterfaceDef {
pub type_params: Vec<TsTypeParamDef>,
}
-fn expr_to_name(expr: &swc_ecma_ast::Expr) -> String {
+pub fn expr_to_name(expr: &swc_ecma_ast::Expr) -> String {
use crate::swc_ecma_ast::Expr::*;
use crate::swc_ecma_ast::ExprOrSuper::*;
@@ -65,7 +65,7 @@ fn expr_to_name(expr: &swc_ecma_ast::Expr) -> String {
Ident(ident) => ident.sym.to_string(),
Member(member_expr) => {
let left = match &member_expr.obj {
- Super(_) => "TODO".to_string(),
+ Super(_) => "super".to_string(),
Expr(boxed_expr) => expr_to_name(&*boxed_expr),
};
let right = expr_to_name(&*member_expr.prop);
@@ -126,10 +126,7 @@ pub fn get_doc_for_ts_interface_decl(
}
TsPropertySignature(ts_prop_sig) => {
let prop_js_doc = doc_parser.js_doc_for_span(ts_prop_sig.span);
- let name = match &*ts_prop_sig.key {
- swc_ecma_ast::Expr::Ident(ident) => ident.sym.to_string(),
- _ => "TODO".to_string(),
- };
+ let name = expr_to_name(&*ts_prop_sig.key);
let mut params = vec![];
diff --git a/cli/doc/tests.rs b/cli/doc/tests.rs
index af65d179a..2317c3707 100644
--- a/cli/doc/tests.rs
+++ b/cli/doc/tests.rs
@@ -246,28 +246,125 @@ export function foo([e,,f, ...g]: number[], { c, d: asdf, i = "asdf", ...rest},
#[tokio::test]
async fn export_const() {
- let source_code =
- "/** Something about fizzBuzz */\nexport const fizzBuzz = \"fizzBuzz\";\n";
+ let source_code = r#"
+/** Something about fizzBuzz */
+export const fizzBuzz = "fizzBuzz";
+
+export const env: {
+ /** get doc */
+ get(key: string): string | undefined;
+
+ /** set doc */
+ set(key: string, value: string): void;
+}
+"#;
let loader =
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
- assert_eq!(entries.len(), 1);
- let entry = &entries[0];
- let expected_json = json!({
- "kind": "variable",
- "name": "fizzBuzz",
- "location": {
- "filename": "test.ts",
- "line": 2,
- "col": 0
+ assert_eq!(entries.len(), 2);
+ let expected_json = json!([
+ {
+ "kind":"variable",
+ "name":"fizzBuzz",
+ "location":{
+ "filename":"test.ts",
+ "line":3,
+ "col":0
},
- "jsDoc": "Something about fizzBuzz",
- "variableDef": {
- "tsType": null,
- "kind": "const"
+ "jsDoc":"Something about fizzBuzz",
+ "variableDef":{
+ "tsType":null,
+ "kind":"const"
}
- });
- let actual = serde_json::to_value(entry).unwrap();
+ },
+ {
+ "kind":"variable",
+ "name":"env",
+ "location":{
+ "filename":"test.ts",
+ "line":5,
+ "col":0
+ },
+ "jsDoc":null,
+ "variableDef":{
+ "tsType":{
+ "repr":"",
+ "kind":"typeLiteral",
+ "typeLiteral":{
+ "methods":[{
+ "name":"get",
+ "params":[
+ {
+ "name":"key",
+ "kind":"identifier",
+ "optional":false,
+ "tsType":{
+ "repr":"string",
+ "kind":"keyword",
+ "keyword":"string"
+ }
+ }
+ ],
+ "returnType":{
+ "repr":"",
+ "kind":"union",
+ "union":[
+ {
+ "repr":"string",
+ "kind":"keyword",
+ "keyword":"string"
+ },
+ {
+ "repr":"undefined",
+ "kind":"keyword",
+ "keyword":"undefined"
+ }
+ ]
+ },
+ "typeParams":[]
+ }, {
+ "name":"set",
+ "params":[
+ {
+ "name":"key",
+ "kind":"identifier",
+ "optional":false,
+ "tsType":{
+ "repr":"string",
+ "kind":"keyword",
+ "keyword":"string"
+ }
+ },
+ {
+ "name":"value",
+ "kind":"identifier",
+ "optional":false,
+ "tsType":{
+ "repr":"string",
+ "kind":"keyword",
+ "keyword":"string"
+ }
+ }
+ ],
+ "returnType":{
+ "repr":"void",
+ "kind":"keyword",
+ "keyword":"void"
+ },
+ "typeParams":[]
+ }
+ ],
+ "properties":[],
+ "callSignatures":[]
+ }
+ },
+ "kind":"const"
+ }
+ }
+ ]
+ );
+
+ let actual = serde_json::to_value(entries.clone()).unwrap();
assert_eq!(actual, expected_json);
assert!(
diff --git a/cli/doc/ts_type.rs b/cli/doc/ts_type.rs
index 8359f64cc..b5f5a8807 100644
--- a/cli/doc/ts_type.rs
+++ b/cli/doc/ts_type.rs
@@ -1,4 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+use super::interface::expr_to_name;
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;
@@ -24,7 +25,6 @@ use crate::swc_ecma_ast::TsTypeQuery;
use crate::swc_ecma_ast::TsTypeRef;
use crate::swc_ecma_ast::TsUnionOrIntersectionType;
use serde::Serialize;
-
// pub enum TsType {
// * TsKeywordType(TsKeywordType),
// * TsThisType(TsThisType),
@@ -354,8 +354,9 @@ impl Into<TsTypeDef> for &TsTypeLit {
let type_params = maybe_type_param_decl_to_type_param_defs(
ts_method_sig.type_params.as_ref(),
);
+ let name = expr_to_name(&*ts_method_sig.key);
let method_def = LiteralMethodDef {
- name: "<TODO>".to_string(),
+ name,
params,
return_type: maybe_return_type,
type_params,
@@ -363,10 +364,7 @@ impl Into<TsTypeDef> for &TsTypeLit {
methods.push(method_def);
}
TsPropertySignature(ts_prop_sig) => {
- let name = match &*ts_prop_sig.key {
- swc_ecma_ast::Expr::Ident(ident) => ident.sym.to_string(),
- _ => "TODO".to_string(),
- };
+ let name = expr_to_name(&*ts_prop_sig.key);
let mut params = vec![];
diff --git a/cli/doc/variable.rs b/cli/doc/variable.rs
index e7bc475d1..0b9c28a42 100644
--- a/cli/doc/variable.rs
+++ b/cli/doc/variable.rs
@@ -19,7 +19,6 @@ pub fn get_doc_for_var_decl(
) -> (String, VariableDef) {
assert!(!var_decl.decls.is_empty());
let var_declarator = var_decl.decls.get(0).unwrap();
-
let var_name = match &var_declarator.name {
swc_ecma_ast::Pat::Ident(ident) => ident.sym.to_string(),
_ => "<TODO>".to_string(),