0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -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::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,

View file

@ -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![];

View file

@ -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!(

View file

@ -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![];

View file

@ -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(),