From d795d3436246b609c572921ea4511fbde37f0ff3 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Mon, 30 Mar 2020 04:41:27 +0200 Subject: fix(doc): Safely deal with optional type (#4526) --- cli/doc/tests.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'cli/doc/tests.rs') diff --git a/cli/doc/tests.rs b/cli/doc/tests.rs index 12621c56d..8cc61195a 100644 --- a/cli/doc/tests.rs +++ b/cli/doc/tests.rs @@ -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)") + ); +} -- cgit v1.2.3