Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
d795d343
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
d795d343
编写于
3月 30, 2020
作者:
L
Luca Casonato
提交者:
GitHub
3月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(doc): Safely deal with optional type (#4526)
上级
92f1c71a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
94 addition
and
30 deletion
+94
-30
cli/doc/printer.rs
cli/doc/printer.rs
+48
-30
cli/doc/tests.rs
cli/doc/tests.rs
+46
-0
未找到文件。
cli/doc/printer.rs
浏览文件 @
d795d343
...
...
@@ -90,12 +90,8 @@ fn format_(doc_nodes: Vec<doc::DocNode>, indent: i64) -> String {
for
node
in
sorted
{
output
.push_str
(
&
format_signature
(
&
node
,
indent
));
if
node
.js_doc
.is_some
()
{
output
.push_str
(
&
format_jsdoc
(
node
.js_doc
.as_ref
()
.unwrap
()
.to_string
(),
true
,
indent
,
));
if
let
Some
(
js_doc
)
=
node
.js_doc
{
output
.push_str
(
&
format_jsdoc
(
js_doc
,
true
,
indent
));
}
output
.push_str
(
"
\n
"
);
if
DocNodeKind
::
Namespace
==
node
.kind
{
...
...
@@ -115,9 +111,9 @@ fn render_params(params: Vec<doc::ParamDef>) -> String {
if
!
params
.is_empty
()
{
for
param
in
params
{
rendered
+=
param
.name
.as_str
();
if
param
.ts_type
.is_some
()
{
if
let
Some
(
ts_type
)
=
param
.ts_type
{
rendered
+=
": "
;
rendered
+=
render_ts_type
(
param
.ts_type
.unwrap
()
)
.as_str
();
rendered
+=
render_ts_type
(
ts_type
)
.as_str
();
}
rendered
+=
", "
;
}
...
...
@@ -215,24 +211,39 @@ fn render_ts_type(ts_type: doc::ts_type::TsTypeDef) -> String {
let
type_literal
=
ts_type
.type_literal
.unwrap
();
for
node
in
type_literal
.call_signatures
{
output
+=
format!
(
"({})
:
{}, "
,
"({}){}, "
,
render_params
(
node
.params
),
render_ts_type
(
node
.ts_type
.unwrap
())
if
let
Some
(
ts_type
)
=
node
.ts_type
{
format!
(
": {}"
,
render_ts_type
(
ts_type
))
}
else
{
""
.to_string
()
}
)
.as_str
()
}
for
node
in
type_literal
.methods
{
output
+=
format!
(
"{}({})
:
{}, "
,
"{}({}){}, "
,
node
.name
,
render_params
(
node
.params
),
render_ts_type
(
node
.return_type
.unwrap
())
if
let
Some
(
return_type
)
=
node
.return_type
{
format!
(
": {}"
,
render_ts_type
(
return_type
))
}
else
{
""
.to_string
()
}
)
.as_str
()
}
for
node
in
type_literal
.properties
{
output
+=
format!
(
"{}: {}, "
,
node
.name
,
render_ts_type
(
node
.ts_type
.unwrap
()))
output
+=
format!
(
"{}{}, "
,
node
.name
,
if
let
Some
(
ts_type
)
=
node
.ts_type
{
format!
(
": {}"
,
render_ts_type
(
ts_type
))
}
else
{
""
.to_string
()
}
)
.as_str
()
}
if
!
output
.is_empty
()
{
...
...
@@ -250,9 +261,8 @@ fn render_ts_type(ts_type: doc::ts_type::TsTypeDef) -> String {
TsTypeDefKind
::
TypeRef
=>
{
let
type_ref
=
ts_type
.type_ref
.unwrap
();
let
mut
final_output
=
type_ref
.type_name
;
if
type_ref
.type_params
.is_some
()
{
if
let
Some
(
type_params
)
=
type_ref
.type_params
{
let
mut
output
=
""
.to_string
();
let
type_params
=
type_ref
.type_params
.unwrap
();
if
!
type_params
.is_empty
()
{
for
ts_type
in
type_params
{
output
+=
render_ts_type
(
ts_type
)
.as_str
();
...
...
@@ -328,7 +338,7 @@ fn format_class_details(node: doc::DocNode) -> String {
})
{
details
.push_str
(
&
add_indent
(
format!
(
"{}{}
:
{}
\n
"
,
"{}{}{}
\n
"
,
colors
::
magenta
(
match
node
.accessibility
...
...
@@ -339,7 +349,11 @@ fn format_class_details(node: doc::DocNode) -> String {
}
),
colors
::
bold
(
node
.name
.clone
()),
render_ts_type
(
node
.ts_type
.clone
()
.unwrap
())
if
let
Some
(
ts_type
)
=
node
.ts_type
.clone
()
{
format!
(
": {}"
,
render_ts_type
(
ts_type
))
}
else
{
""
.to_string
()
}
),
1
,
));
...
...
@@ -353,7 +367,7 @@ fn format_class_details(node: doc::DocNode) -> String {
let
function_def
=
node
.function_def
.clone
();
details
.push_str
(
&
add_indent
(
format!
(
"{}{}{}({})
:
{}
\n
"
,
"{}{}{}({}){}
\n
"
,
colors
::
magenta
(
match
node
.accessibility
...
...
@@ -370,7 +384,11 @@ fn format_class_details(node: doc::DocNode) -> String {
}),
colors
::
bold
(
node
.name
.clone
()),
render_params
(
function_def
.params
),
render_ts_type
(
function_def
.return_type
.unwrap
())
if
let
Some
(
return_type
)
=
function_def
.return_type
{
format!
(
": {}"
,
render_ts_type
(
return_type
))
}
else
{
""
.to_string
()
}
),
1
,
));
...
...
@@ -392,17 +410,17 @@ fn format_namespace_details(node: doc::DocNode) -> String {
fn
format_function_signature
(
node
:
&
doc
::
DocNode
,
indent
:
i64
)
->
String
{
let
function_def
=
node
.function_def
.clone
()
.unwrap
();
let
return_type
=
function_def
.return_type
.unwrap
();
add_indent
(
format!
(
"{} {}{}
\n
"
,
"{} {}
({})
{}
\n
"
,
colors
::
magenta
(
"function"
.to_string
()),
colors
::
bold
(
node
.name
.clone
()),
format!
(
"({}): {}"
,
render_params
(
function_def
.params
),
render_ts_type
(
return_type
)
.as_str
()
)
if
let
Some
(
return_type
)
=
function_def
.return_type
{
format!
(
": {}"
,
render_ts_type
(
return_type
)
.as_str
())
}
else
{
""
.to_string
()
}
),
indent
,
)
...
...
@@ -430,8 +448,8 @@ fn format_variable_signature(node: &doc::DocNode, indent: i64) -> String {
swc_ecma_ast
::
VarDeclKind
::
Var
=>
"var"
.to_string
(),
}),
colors
::
bold
(
node
.name
.clone
()),
if
variable_def
.ts_type
.is_some
()
{
format!
(
": {}"
,
render_ts_type
(
variable_def
.ts_type
.unwrap
()
))
if
let
Some
(
ts_type
)
=
variable_def
.ts_type
{
format!
(
": {}"
,
render_ts_type
(
ts_type
))
}
else
{
""
.to_string
()
}
...
...
cli/doc/tests.rs
浏览文件 @
d795d343
...
...
@@ -590,3 +590,49 @@ export namespace RootNs {
.contains
(
"namespace RootNs"
)
);
}
#[test]
fn
optional_return_type
()
{
let
source_code
=
r#"
export function foo(a: number) {
return a;
}
"#
;
let
entries
=
DocParser
::
default
()
.parse
(
"test.ts"
.to_string
(),
source_code
.to_string
())
.unwrap
();
assert_eq!
(
entries
.len
(),
1
);
let
entry
=
&
entries
[
0
];
let
expected_json
=
json!
({
"kind"
:
"function"
,
"name"
:
"foo"
,
"location"
:
{
"filename"
:
"test.ts"
,
"line"
:
2
,
"col"
:
2
},
"jsDoc"
:
null
,
"functionDef"
:
{
"params"
:
[
{
"name"
:
"a"
,
"tsType"
:
{
"keyword"
:
"number"
,
"kind"
:
"keyword"
,
"repr"
:
"number"
,
},
}
],
"returnType"
:
null
,
"isAsync"
:
false
,
"isGenerator"
:
false
}
});
let
actual
=
serde_json
::
to_value
(
entry
)
.unwrap
();
assert_eq!
(
actual
,
expected_json
);
assert
!
(
colors
::
strip_ansi_codes
(
super
::
printer
::
format
(
entries
)
.as_str
())
.contains
(
"function foo(a: number)"
)
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录