0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix(doc): better repr for object literal types (#4998)

This commit is contained in:
Bartek Iwańczuk 2020-04-30 16:40:51 +02:00 committed by GitHub
parent 81c75332fb
commit 4993a6504b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 35 deletions

View file

@ -6,6 +6,7 @@ use serde::Serialize;
use super::function::function_to_function_def; use super::function::function_to_function_def;
use super::function::FunctionDef; use super::function::FunctionDef;
use super::interface::expr_to_name;
use super::params::assign_pat_to_param_def; use super::params::assign_pat_to_param_def;
use super::params::ident_to_param_def; use super::params::ident_to_param_def;
use super::params::pat_to_param_def; use super::params::pat_to_param_def;
@ -181,11 +182,7 @@ pub fn get_doc_for_class_decl(
.as_ref() .as_ref()
.map(|rt| ts_type_ann_to_def(rt)); .map(|rt| ts_type_ann_to_def(rt));
use crate::swc_ecma_ast::Expr; let prop_name = expr_to_name(&*class_prop.key);
let prop_name = match &*class_prop.key {
Expr::Ident(ident) => ident.sym.to_string(),
_ => "<TODO>".to_string(),
};
let prop_def = ClassPropertyDef { let prop_def = ClassPropertyDef {
js_doc: prop_js_doc, js_doc: prop_js_doc,

View file

@ -57,7 +57,7 @@ pub struct InterfaceDef {
pub type_params: Vec<TsTypeParamDef>, 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::Expr::*;
use crate::swc_ecma_ast::ExprOrSuper::*; 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(), Ident(ident) => ident.sym.to_string(),
Member(member_expr) => { Member(member_expr) => {
let left = match &member_expr.obj { let left = match &member_expr.obj {
Super(_) => "TODO".to_string(), Super(_) => "super".to_string(),
Expr(boxed_expr) => expr_to_name(&*boxed_expr), Expr(boxed_expr) => expr_to_name(&*boxed_expr),
}; };
let right = expr_to_name(&*member_expr.prop); let right = expr_to_name(&*member_expr.prop);
@ -126,10 +126,7 @@ pub fn get_doc_for_ts_interface_decl(
} }
TsPropertySignature(ts_prop_sig) => { TsPropertySignature(ts_prop_sig) => {
let prop_js_doc = doc_parser.js_doc_for_span(ts_prop_sig.span); let prop_js_doc = doc_parser.js_doc_for_span(ts_prop_sig.span);
let name = match &*ts_prop_sig.key { let name = expr_to_name(&*ts_prop_sig.key);
swc_ecma_ast::Expr::Ident(ident) => ident.sym.to_string(),
_ => "TODO".to_string(),
};
let mut params = vec![]; let mut params = vec![];

View file

@ -246,28 +246,125 @@ export function foo([e,,f, ...g]: number[], { c, d: asdf, i = "asdf", ...rest},
#[tokio::test] #[tokio::test]
async fn export_const() { async fn export_const() {
let source_code = let source_code = r#"
"/** Something about fizzBuzz */\nexport const fizzBuzz = \"fizzBuzz\";\n"; /** 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 = let loader =
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]); TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
let entries = DocParser::new(loader).parse("test.ts").await.unwrap(); let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
assert_eq!(entries.len(), 1); assert_eq!(entries.len(), 2);
let entry = &entries[0]; let expected_json = json!([
let expected_json = json!({ {
"kind": "variable", "kind":"variable",
"name": "fizzBuzz", "name":"fizzBuzz",
"location": { "location":{
"filename": "test.ts", "filename":"test.ts",
"line": 2, "line":3,
"col": 0 "col":0
}, },
"jsDoc": "Something about fizzBuzz", "jsDoc":"Something about fizzBuzz",
"variableDef": { "variableDef":{
"tsType": null, "tsType":null,
"kind": "const" "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_eq!(actual, expected_json);
assert!( assert!(

View file

@ -1,4 +1,5 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // 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::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::maybe_type_param_decl_to_type_param_defs;
use super::ts_type_param::TsTypeParamDef; 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::TsTypeRef;
use crate::swc_ecma_ast::TsUnionOrIntersectionType; use crate::swc_ecma_ast::TsUnionOrIntersectionType;
use serde::Serialize; use serde::Serialize;
// pub enum TsType { // pub enum TsType {
// * TsKeywordType(TsKeywordType), // * TsKeywordType(TsKeywordType),
// * TsThisType(TsThisType), // * TsThisType(TsThisType),
@ -354,8 +354,9 @@ impl Into<TsTypeDef> for &TsTypeLit {
let type_params = maybe_type_param_decl_to_type_param_defs( let type_params = maybe_type_param_decl_to_type_param_defs(
ts_method_sig.type_params.as_ref(), ts_method_sig.type_params.as_ref(),
); );
let name = expr_to_name(&*ts_method_sig.key);
let method_def = LiteralMethodDef { let method_def = LiteralMethodDef {
name: "<TODO>".to_string(), name,
params, params,
return_type: maybe_return_type, return_type: maybe_return_type,
type_params, type_params,
@ -363,10 +364,7 @@ impl Into<TsTypeDef> for &TsTypeLit {
methods.push(method_def); methods.push(method_def);
} }
TsPropertySignature(ts_prop_sig) => { TsPropertySignature(ts_prop_sig) => {
let name = match &*ts_prop_sig.key { let name = expr_to_name(&*ts_prop_sig.key);
swc_ecma_ast::Expr::Ident(ident) => ident.sym.to_string(),
_ => "TODO".to_string(),
};
let mut params = vec![]; let mut params = vec![];

View file

@ -19,7 +19,6 @@ pub fn get_doc_for_var_decl(
) -> (String, VariableDef) { ) -> (String, VariableDef) {
assert!(!var_decl.decls.is_empty()); assert!(!var_decl.decls.is_empty());
let var_declarator = var_decl.decls.get(0).unwrap(); let var_declarator = var_decl.decls.get(0).unwrap();
let var_name = match &var_declarator.name { let var_name = match &var_declarator.name {
swc_ecma_ast::Pat::Ident(ident) => ident.sym.to_string(), swc_ecma_ast::Pat::Ident(ident) => ident.sym.to_string(),
_ => "<TODO>".to_string(), _ => "<TODO>".to_string(),