2020-03-28 14:16:57 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
use super::DocParser;
|
2020-03-28 17:35:31 -04:00
|
|
|
use crate::colors;
|
2020-03-28 14:16:57 -04:00
|
|
|
use serde_json::json;
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
use super::parser::DocFileLoader;
|
|
|
|
use crate::op_error::OpError;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use futures::Future;
|
|
|
|
use futures::FutureExt;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
pub struct TestLoader {
|
|
|
|
files: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestLoader {
|
|
|
|
pub fn new(files_vec: Vec<(String, String)>) -> Box<Self> {
|
|
|
|
let mut files = HashMap::new();
|
|
|
|
|
|
|
|
for file_tuple in files_vec {
|
|
|
|
files.insert(file_tuple.0, file_tuple.1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::new(Self { files })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocFileLoader for TestLoader {
|
|
|
|
fn load_source_code(
|
|
|
|
&self,
|
|
|
|
specifier: &str,
|
|
|
|
) -> Pin<Box<dyn Future<Output = Result<String, OpError>>>> {
|
|
|
|
let res = match self.files.get(specifier) {
|
|
|
|
Some(source_code) => Ok(source_code.to_string()),
|
|
|
|
None => Err(OpError::other("not found".to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
async move { res }.boxed_local()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn export_fn() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"/**
|
2020-05-27 17:38:36 -04:00
|
|
|
* @module foo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-03-28 14:16:57 -04:00
|
|
|
* Hello there, this is a multiline JSdoc.
|
2020-04-03 04:32:46 -04:00
|
|
|
*
|
2020-03-28 14:16:57 -04:00
|
|
|
* It has many lines
|
2020-04-03 04:32:46 -04:00
|
|
|
*
|
2020-03-28 14:16:57 -04:00
|
|
|
* Or not that many?
|
|
|
|
*/
|
2020-04-13 18:07:06 -04:00
|
|
|
export function foo(a: string, b?: number, cb: (...cbArgs: unknown[]) => void, ...args: unknown[]): void {
|
2020-05-27 17:38:36 -04:00
|
|
|
/**
|
|
|
|
* @todo document all the things.
|
|
|
|
*/
|
2020-03-28 14:16:57 -04:00
|
|
|
console.log("Hello world");
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"functionDef": {
|
|
|
|
"isAsync": false,
|
|
|
|
"isGenerator": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"keyword": "string",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": true,
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"keyword": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "number",
|
|
|
|
},
|
|
|
|
},
|
2020-04-08 11:03:42 -04:00
|
|
|
{
|
|
|
|
"name": "cb",
|
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "",
|
|
|
|
"kind": "fnOrConstructor",
|
|
|
|
"fnOrConstructor": {
|
|
|
|
"constructor": false,
|
|
|
|
"tsType": {
|
|
|
|
"keyword": "void",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "void"
|
|
|
|
},
|
|
|
|
"typeParams": [],
|
|
|
|
"params": [{
|
|
|
|
"kind": "rest",
|
|
|
|
"name": "cbArgs",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "",
|
|
|
|
"kind": "array",
|
|
|
|
"array": {
|
|
|
|
"repr": "unknown",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "unknown"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "args",
|
|
|
|
"kind": "rest",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "",
|
|
|
|
"kind": "array",
|
|
|
|
"array": {
|
|
|
|
"repr": "unknown",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 14:16:57 -04:00
|
|
|
],
|
|
|
|
"returnType": {
|
|
|
|
"keyword": "void",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "void",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jsDoc": "Hello there, this is a multiline JSdoc.\n\nIt has many lines\n\nOr not that many?",
|
|
|
|
"kind": "function",
|
|
|
|
"location": {
|
|
|
|
"col": 0,
|
|
|
|
"filename": "test.ts",
|
2020-05-27 17:38:36 -04:00
|
|
|
"line": 12,
|
2020-03-28 14:16:57 -04:00
|
|
|
},
|
|
|
|
"name": "foo",
|
|
|
|
});
|
2020-04-08 11:03:42 -04:00
|
|
|
|
2020-03-28 14:16:57 -04:00
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-04-13 18:07:06 -04:00
|
|
|
assert!(colors::strip_ansi_codes(
|
|
|
|
super::printer::format(entries.clone()).as_str()
|
|
|
|
)
|
|
|
|
.contains("Hello there"));
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
2020-04-13 18:07:06 -04:00
|
|
|
.contains("b?: number")
|
2020-03-28 17:35:31 -04:00
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 18:55:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn format_type_predicate() {
|
|
|
|
let source_code = r#"
|
|
|
|
export function isFish(pet: Fish | Bird): pet is Fish {
|
|
|
|
return (pet as Fish).swim !== undefined;
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
|
|
|
super::printer::format(entries);
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:03:42 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_fn2() {
|
|
|
|
let source_code = r#"
|
|
|
|
interface AssignOpts {
|
|
|
|
a: string;
|
|
|
|
b: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function foo([e,,f, ...g]: number[], { c, d: asdf, i = "asdf", ...rest}, ops: AssignOpts = {}): void {
|
|
|
|
console.log("Hello world");
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
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!({
|
|
|
|
"functionDef": {
|
|
|
|
"isAsync": false,
|
|
|
|
"isGenerator": false,
|
|
|
|
"typeParams": [],
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"kind": "array",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "",
|
|
|
|
"kind": "array",
|
|
|
|
"array": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"kind": "object",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "ops",
|
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "AssignOpts",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeName": "AssignOpts",
|
|
|
|
"typeParams": null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"returnType": {
|
|
|
|
"keyword": "void",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "void",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"kind": "function",
|
|
|
|
"location": {
|
|
|
|
"col": 0,
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 7,
|
|
|
|
},
|
|
|
|
"name": "foo",
|
|
|
|
});
|
|
|
|
|
|
|
|
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("foo")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_const() {
|
2020-04-30 10:40:51 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-04-30 10:40:51 -04:00
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
let expected_json = json!([
|
|
|
|
{
|
|
|
|
"kind":"variable",
|
|
|
|
"name":"fizzBuzz",
|
|
|
|
"location":{
|
|
|
|
"filename":"test.ts",
|
|
|
|
"line":3,
|
|
|
|
"col":0
|
2020-03-28 14:16:57 -04:00
|
|
|
},
|
2020-04-30 10:40:51 -04:00
|
|
|
"jsDoc":"Something about fizzBuzz",
|
|
|
|
"variableDef":{
|
|
|
|
"tsType":null,
|
|
|
|
"kind":"const"
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
2020-04-30 10:40:51 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"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();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
|
|
|
.contains("Something about fizzBuzz")
|
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_class() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"
|
|
|
|
/** Class doc */
|
2020-04-03 08:11:50 -04:00
|
|
|
export class Foobar extends Fizz implements Buzz, Aldrin {
|
2020-04-13 18:07:06 -04:00
|
|
|
private private1?: boolean;
|
2020-03-28 14:16:57 -04:00
|
|
|
protected protected1: number;
|
|
|
|
public public1: boolean;
|
|
|
|
public2: number;
|
|
|
|
|
|
|
|
/** Constructor js doc */
|
|
|
|
constructor(name: string, private private2: number, protected protected2: number) {}
|
|
|
|
|
|
|
|
/** Async foo method */
|
|
|
|
async foo(): Promise<void> {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sync bar method */
|
2020-04-13 18:07:06 -04:00
|
|
|
bar?(): void {
|
2020-03-28 14:16:57 -04:00
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "class",
|
|
|
|
"name": "Foobar",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Class doc",
|
|
|
|
"classDef": {
|
|
|
|
"isAbstract": false,
|
2020-04-13 18:28:49 -04:00
|
|
|
"extends": "Fizz",
|
2020-04-03 08:11:50 -04:00
|
|
|
"implements": ["Buzz", "Aldrin"],
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
"constructors": [
|
|
|
|
{
|
|
|
|
"jsDoc": "Constructor js doc",
|
|
|
|
"accessibility": null,
|
|
|
|
"name": "constructor",
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "name",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "string",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "string"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2020-04-08 11:03:42 -04:00
|
|
|
"name": "private2",
|
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
2020-03-28 14:16:57 -04:00
|
|
|
},
|
|
|
|
{
|
2020-04-08 11:03:42 -04:00
|
|
|
"name": "protected2",
|
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 10,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"properties": [
|
|
|
|
{
|
|
|
|
"jsDoc": null,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "boolean",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "boolean"
|
|
|
|
},
|
|
|
|
"readonly": false,
|
|
|
|
"accessibility": "private",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": true,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "private1",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 4,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"jsDoc": null,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
},
|
|
|
|
"readonly": false,
|
|
|
|
"accessibility": "protected",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "protected1",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"jsDoc": null,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "boolean",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "boolean"
|
|
|
|
},
|
|
|
|
"readonly": false,
|
|
|
|
"accessibility": "public",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "public1",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 6,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"jsDoc": null,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
},
|
|
|
|
"readonly": false,
|
|
|
|
"accessibility": null,
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "public2",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 7,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"methods": [
|
|
|
|
{
|
|
|
|
"jsDoc": "Async foo method",
|
|
|
|
"accessibility": null,
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "foo",
|
|
|
|
"kind": "method",
|
|
|
|
"functionDef": {
|
|
|
|
"params": [],
|
|
|
|
"returnType": {
|
|
|
|
"repr": "Promise",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": [
|
|
|
|
{
|
|
|
|
"repr": "void",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "void"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeName": "Promise"
|
|
|
|
}
|
|
|
|
},
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAsync": true,
|
|
|
|
"isGenerator": false
|
|
|
|
},
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 13,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"jsDoc": "Sync bar method",
|
|
|
|
"accessibility": null,
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": true,
|
2020-03-28 14:16:57 -04:00
|
|
|
"isAbstract": false,
|
|
|
|
"isStatic": false,
|
|
|
|
"name": "bar",
|
|
|
|
"kind": "method",
|
|
|
|
"functionDef": {
|
|
|
|
"params": [],
|
2020-04-08 11:03:42 -04:00
|
|
|
"returnType": {
|
|
|
|
"repr": "void",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "void"
|
2020-03-28 14:16:57 -04:00
|
|
|
},
|
2020-04-08 11:03:42 -04:00
|
|
|
"isAsync": false,
|
|
|
|
"isGenerator": false,
|
|
|
|
"typeParams": []
|
|
|
|
},
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 18,
|
|
|
|
"col": 4
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
2020-04-08 11:03:42 -04:00
|
|
|
}
|
2020-03-28 14:16:57 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let entry = &entries[0];
|
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-04-13 18:07:06 -04:00
|
|
|
assert!(colors::strip_ansi_codes(
|
|
|
|
super::printer::format_details(entry.clone()).as_str()
|
|
|
|
)
|
|
|
|
.contains("bar?(): void"));
|
|
|
|
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
2020-04-03 08:11:50 -04:00
|
|
|
.contains("class Foobar extends Fizz implements Buzz, Aldrin")
|
2020-03-28 17:35:31 -04:00
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_interface() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"
|
2020-04-13 18:28:49 -04:00
|
|
|
interface Foo {
|
|
|
|
foo(): void;
|
|
|
|
}
|
|
|
|
interface Bar {
|
|
|
|
bar(): void;
|
|
|
|
}
|
2020-03-28 14:16:57 -04:00
|
|
|
/**
|
|
|
|
* Interface js doc
|
|
|
|
*/
|
2020-04-13 18:28:49 -04:00
|
|
|
export interface Reader extends Foo, Bar {
|
2020-03-28 14:16:57 -04:00
|
|
|
/** Read n bytes */
|
2020-04-13 18:07:06 -04:00
|
|
|
read?(buf: Uint8Array, something: unknown): Promise<number>
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "interface",
|
|
|
|
"name": "Reader",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
2020-04-13 18:28:49 -04:00
|
|
|
"line": 11,
|
2020-03-28 14:16:57 -04:00
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Interface js doc",
|
|
|
|
"interfaceDef": {
|
2020-04-13 18:28:49 -04:00
|
|
|
"extends": ["Foo", "Bar"],
|
2020-03-28 14:16:57 -04:00
|
|
|
"methods": [
|
|
|
|
{
|
|
|
|
"name": "read",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
2020-04-13 18:28:49 -04:00
|
|
|
"line": 13,
|
2020-03-28 14:16:57 -04:00
|
|
|
"col": 4
|
|
|
|
},
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": true,
|
2020-03-28 14:16:57 -04:00
|
|
|
"jsDoc": "Read n bytes",
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "buf",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "Uint8Array",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": null,
|
|
|
|
"typeName": "Uint8Array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "something",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "unknown",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
"returnType": {
|
|
|
|
"repr": "Promise",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": [
|
|
|
|
{
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeName": "Promise"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"properties": [],
|
2020-04-08 11:03:42 -04:00
|
|
|
"callSignatures": [],
|
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
2020-04-13 18:28:49 -04:00
|
|
|
.contains("interface Reader extends Foo, Bar")
|
2020-03-28 17:35:31 -04:00
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-08 11:03:42 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_interface2() {
|
|
|
|
let source_code = r#"
|
|
|
|
export interface TypedIface<T> {
|
|
|
|
something(): T
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
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": "interface",
|
|
|
|
"name": "TypedIface",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 2,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"interfaceDef": {
|
2020-04-13 18:28:49 -04:00
|
|
|
"extends": [],
|
2020-04-08 11:03:42 -04:00
|
|
|
"methods": [
|
|
|
|
{
|
|
|
|
"name": "something",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 4
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-08 11:03:42 -04:00
|
|
|
"params": [],
|
|
|
|
"typeParams": [],
|
|
|
|
"returnType": {
|
|
|
|
"repr": "T",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": null,
|
|
|
|
"typeName": "T"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"properties": [],
|
|
|
|
"callSignatures": [],
|
|
|
|
"typeParams": [
|
|
|
|
{ "name": "T" }
|
|
|
|
],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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("interface TypedIface")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_type_alias() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"
|
|
|
|
/** Array holding numbers */
|
|
|
|
export type NumberArray = Array<number>;
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "typeAlias",
|
|
|
|
"name": "NumberArray",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Array holding numbers",
|
|
|
|
"typeAliasDef": {
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-28 14:16:57 -04:00
|
|
|
"tsType": {
|
|
|
|
"repr": "Array",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": [
|
|
|
|
{
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeName": "Array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
|
|
|
.contains("Array holding numbers")
|
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_enum() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"
|
|
|
|
/**
|
|
|
|
* Some enum for good measure
|
|
|
|
*/
|
|
|
|
export enum Hello {
|
|
|
|
World = "world",
|
|
|
|
Fizz = "fizz",
|
|
|
|
Buzz = "buzz",
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "enum",
|
|
|
|
"name": "Hello",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Some enum for good measure",
|
|
|
|
"enumDef": {
|
|
|
|
"members": [
|
|
|
|
{
|
|
|
|
"name": "World"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Fizz"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Buzz"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
2020-06-03 15:40:50 -04:00
|
|
|
assert!(colors::strip_ansi_codes(
|
|
|
|
super::printer::format_details(entry.clone()).as_str()
|
|
|
|
)
|
|
|
|
.contains("World"));
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(colors::strip_ansi_codes(
|
|
|
|
super::printer::format(entries.clone()).as_str()
|
|
|
|
)
|
|
|
|
.contains("Some enum for good measure"));
|
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
|
|
|
.contains("enum Hello")
|
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn export_namespace() {
|
2020-03-28 14:16:57 -04:00
|
|
|
let source_code = r#"
|
|
|
|
/** Namespace JSdoc */
|
|
|
|
export namespace RootNs {
|
|
|
|
export const a = "a";
|
|
|
|
|
|
|
|
/** Nested namespace JSDoc */
|
|
|
|
export namespace NestedNs {
|
|
|
|
export enum Foo {
|
|
|
|
a = 1,
|
|
|
|
b = 2,
|
|
|
|
c = 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-28 14:16:57 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "namespace",
|
|
|
|
"name": "RootNs",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Namespace JSdoc",
|
|
|
|
"namespaceDef": {
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"kind": "variable",
|
|
|
|
"name": "a",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 4,
|
|
|
|
"col": 4
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"variableDef": {
|
|
|
|
"tsType": null,
|
|
|
|
"kind": "const"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"kind": "namespace",
|
|
|
|
"name": "NestedNs",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 7,
|
|
|
|
"col": 4
|
|
|
|
},
|
|
|
|
"jsDoc": "Nested namespace JSDoc",
|
|
|
|
"namespaceDef": {
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"kind": "enum",
|
|
|
|
"name": "Foo",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 8,
|
|
|
|
"col": 6
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"enumDef": {
|
|
|
|
"members": [
|
|
|
|
{
|
|
|
|
"name": "a"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "c"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let actual = serde_json::to_value(entry).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
2020-03-28 17:35:31 -04:00
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
|
|
|
.contains("namespace RootNs")
|
|
|
|
);
|
2020-03-28 14:16:57 -04:00
|
|
|
}
|
2020-03-29 22:41:27 -04:00
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn declare_namespace() {
|
2020-04-02 05:38:13 -04:00
|
|
|
let source_code = r#"
|
|
|
|
/** Namespace JSdoc */
|
|
|
|
declare namespace RootNs {
|
|
|
|
declare const a = "a";
|
|
|
|
|
|
|
|
/** Nested namespace JSDoc */
|
|
|
|
declare namespace NestedNs {
|
|
|
|
declare enum Foo {
|
|
|
|
a = 1,
|
|
|
|
b = 2,
|
|
|
|
c = 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-04-02 05:38:13 -04:00
|
|
|
assert_eq!(entries.len(), 1);
|
|
|
|
let entry = &entries[0];
|
|
|
|
let expected_json = json!({
|
|
|
|
"kind": "namespace",
|
|
|
|
"name": "RootNs",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Namespace JSdoc",
|
|
|
|
"namespaceDef": {
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"kind": "variable",
|
|
|
|
"name": "a",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 4,
|
|
|
|
"col": 12
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"variableDef": {
|
|
|
|
"tsType": null,
|
|
|
|
"kind": "const"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"kind": "namespace",
|
|
|
|
"name": "NestedNs",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 7,
|
|
|
|
"col": 4
|
|
|
|
},
|
|
|
|
"jsDoc": "Nested namespace JSDoc",
|
|
|
|
"namespaceDef": {
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"kind": "enum",
|
|
|
|
"name": "Foo",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 8,
|
|
|
|
"col": 6
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"enumDef": {
|
|
|
|
"members": [
|
|
|
|
{
|
|
|
|
"name": "a"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "c"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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("namespace RootNs")
|
|
|
|
);
|
|
|
|
}
|
2020-05-06 08:48:48 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn export_default_fn() {
|
|
|
|
let source_code = r#"
|
|
|
|
export default function foo(a: number) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
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": "function",
|
|
|
|
"name": "default",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 2,
|
|
|
|
"col": 15
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"functionDef": {
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"keyword": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "number",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeParams": [],
|
|
|
|
"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 default(a: number)")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn export_default_class() {
|
|
|
|
let source_code = r#"
|
|
|
|
/** Class doc */
|
|
|
|
export default class Foobar {
|
|
|
|
/** Constructor js doc */
|
|
|
|
constructor(name: string, private private2: number, protected protected2: number) {}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
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 expected_json = json!({
|
|
|
|
"kind": "class",
|
|
|
|
"name": "default",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Class doc",
|
|
|
|
"classDef": {
|
|
|
|
"isAbstract": false,
|
|
|
|
"extends": null,
|
|
|
|
"implements": [],
|
|
|
|
"typeParams": [],
|
|
|
|
"constructors": [
|
|
|
|
{
|
|
|
|
"jsDoc": "Constructor js doc",
|
|
|
|
"accessibility": null,
|
|
|
|
"name": "constructor",
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "name",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "string",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "string"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "private2",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "protected2",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"properties": [],
|
|
|
|
"methods": []
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let entry = &entries[0];
|
|
|
|
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("class default")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn export_default_interface() {
|
|
|
|
let source_code = r#"
|
|
|
|
/**
|
|
|
|
* Interface js doc
|
|
|
|
*/
|
|
|
|
export default interface Reader {
|
|
|
|
/** Read n bytes */
|
|
|
|
read?(buf: Uint8Array, something: unknown): Promise<number>
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
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": "interface",
|
|
|
|
"name": "default",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "Interface js doc",
|
|
|
|
"interfaceDef": {
|
|
|
|
"extends": [],
|
|
|
|
"methods": [
|
|
|
|
{
|
|
|
|
"name": "read",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 7,
|
|
|
|
"col": 4
|
|
|
|
},
|
|
|
|
"optional": true,
|
|
|
|
"jsDoc": "Read n bytes",
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "buf",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "Uint8Array",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": null,
|
|
|
|
"typeName": "Uint8Array"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "something",
|
|
|
|
"kind": "identifier",
|
|
|
|
"optional": false,
|
|
|
|
"tsType": {
|
|
|
|
"repr": "unknown",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeParams": [],
|
|
|
|
"returnType": {
|
|
|
|
"repr": "Promise",
|
|
|
|
"kind": "typeRef",
|
|
|
|
"typeRef": {
|
|
|
|
"typeParams": [
|
|
|
|
{
|
|
|
|
"repr": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"keyword": "number"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"typeName": "Promise"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"properties": [],
|
|
|
|
"callSignatures": [],
|
|
|
|
"typeParams": [],
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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("interface default")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:47:06 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn optional_return_type() {
|
2020-03-29 22:41:27 -04:00
|
|
|
let source_code = r#"
|
|
|
|
export function foo(a: number) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
"#;
|
2020-04-07 13:47:06 -04:00
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
2020-03-29 22:41:27 -04:00
|
|
|
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",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-03-29 22:41:27 -04:00
|
|
|
"tsType": {
|
|
|
|
"keyword": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "number",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-03-29 22:41:27 -04:00
|
|
|
"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)")
|
|
|
|
);
|
|
|
|
}
|
2020-04-07 13:47:06 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn reexports() {
|
|
|
|
let nested_reexport_source_code = r#"
|
|
|
|
/**
|
|
|
|
* JSDoc for bar
|
|
|
|
*/
|
|
|
|
export const bar = "bar";
|
2020-05-06 08:48:48 -04:00
|
|
|
|
|
|
|
export default 42;
|
2020-04-07 13:47:06 -04:00
|
|
|
"#;
|
|
|
|
let reexport_source_code = r#"
|
|
|
|
import { bar } from "./nested_reexport.ts";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSDoc for const
|
|
|
|
*/
|
|
|
|
export const foo = "foo";
|
|
|
|
"#;
|
|
|
|
let test_source_code = r#"
|
2020-05-06 08:48:48 -04:00
|
|
|
export { default, foo as fooConst } from "./reexport.ts";
|
2020-04-07 13:47:06 -04:00
|
|
|
|
|
|
|
/** JSDoc for function */
|
|
|
|
export function fooFn(a: number) {
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let loader = TestLoader::new(vec![
|
|
|
|
("file:///test.ts".to_string(), test_source_code.to_string()),
|
|
|
|
(
|
|
|
|
"file:///reexport.ts".to_string(),
|
|
|
|
reexport_source_code.to_string(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"file:///nested_reexport.ts".to_string(),
|
|
|
|
nested_reexport_source_code.to_string(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
let entries = DocParser::new(loader)
|
|
|
|
.parse_with_reexports("file:///test.ts")
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(entries.len(), 2);
|
|
|
|
|
|
|
|
let expected_json = json!([
|
|
|
|
{
|
|
|
|
"kind": "variable",
|
|
|
|
"name": "fooConst",
|
|
|
|
"location": {
|
|
|
|
"filename": "file:///reexport.ts",
|
|
|
|
"line": 7,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "JSDoc for const",
|
|
|
|
"variableDef": {
|
|
|
|
"tsType": null,
|
|
|
|
"kind": "const"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"kind": "function",
|
|
|
|
"name": "fooFn",
|
|
|
|
"location": {
|
|
|
|
"filename": "file:///test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": "JSDoc for function",
|
|
|
|
"functionDef": {
|
|
|
|
"params": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
2020-04-08 11:03:42 -04:00
|
|
|
"kind": "identifier",
|
2020-04-13 18:07:06 -04:00
|
|
|
"optional": false,
|
2020-04-07 13:47:06 -04:00
|
|
|
"tsType": {
|
|
|
|
"keyword": "number",
|
|
|
|
"kind": "keyword",
|
|
|
|
"repr": "number",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
2020-04-08 11:03:42 -04:00
|
|
|
"typeParams": [],
|
2020-04-07 13:47:06 -04:00
|
|
|
"returnType": null,
|
|
|
|
"isAsync": false,
|
|
|
|
"isGenerator": false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
let actual = serde_json::to_value(entries.clone()).unwrap();
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
colors::strip_ansi_codes(super::printer::format(entries).as_str())
|
|
|
|
.contains("function fooFn(a: number)")
|
|
|
|
);
|
|
|
|
}
|
2020-05-17 12:56:22 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn ts_lit_types() {
|
|
|
|
let source_code = r#"
|
|
|
|
export type boolLit = false;
|
|
|
|
export type strLit = "text";
|
|
|
|
export type tplLit = `text`;
|
|
|
|
export type numLit = 5;
|
|
|
|
"#;
|
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
|
|
|
let actual = serde_json::to_value(entries).unwrap();
|
|
|
|
let expected_json = json!([
|
|
|
|
{
|
|
|
|
"kind": "typeAlias",
|
|
|
|
"name": "boolLit",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 2,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"typeAliasDef": {
|
|
|
|
"tsType": {
|
|
|
|
"repr": "false",
|
|
|
|
"kind": "literal",
|
|
|
|
"literal": {
|
|
|
|
"kind": "boolean",
|
|
|
|
"boolean": false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"typeParams": []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"kind": "typeAlias",
|
|
|
|
"name": "strLit",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 3,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"typeAliasDef": {
|
|
|
|
"tsType": {
|
|
|
|
"repr": "text",
|
|
|
|
"kind": "literal",
|
|
|
|
"literal": {
|
|
|
|
"kind": "string",
|
|
|
|
"string": "text"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"typeParams": []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"kind": "typeAlias",
|
|
|
|
"name": "tplLit",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 4,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"typeAliasDef": {
|
|
|
|
"tsType": {
|
|
|
|
"repr": "text",
|
|
|
|
"kind": "literal",
|
|
|
|
"literal": {
|
|
|
|
"kind": "string",
|
|
|
|
"string": "text"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"typeParams": []
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
"kind": "typeAlias",
|
|
|
|
"name": "numLit",
|
|
|
|
"location": {
|
|
|
|
"filename": "test.ts",
|
|
|
|
"line": 5,
|
|
|
|
"col": 0
|
|
|
|
},
|
|
|
|
"jsDoc": null,
|
|
|
|
"typeAliasDef": {
|
|
|
|
"tsType": {
|
|
|
|
"repr": "5",
|
|
|
|
"kind": "literal",
|
|
|
|
"literal": {
|
|
|
|
"kind": "number",
|
|
|
|
"number": 5.0
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"typeParams": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
assert_eq!(actual, expected_json);
|
|
|
|
}
|
2020-06-09 08:12:47 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn filter_nodes_by_name() {
|
|
|
|
use super::find_nodes_by_name_recursively;
|
|
|
|
let source_code = r#"
|
|
|
|
export namespace Deno {
|
|
|
|
export class Buffer {}
|
|
|
|
export function test(options: object): void;
|
|
|
|
export function test(name: string, fn: Function): void;
|
|
|
|
export function test(name: string | object, fn?: Function): void {}
|
|
|
|
}
|
|
|
|
|
|
|
|
export namespace Deno {
|
|
|
|
export namespace Inner {
|
|
|
|
export function a(): void {}
|
|
|
|
export const b = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
let loader =
|
|
|
|
TestLoader::new(vec![("test.ts".to_string(), source_code.to_string())]);
|
|
|
|
let entries = DocParser::new(loader).parse("test.ts").await.unwrap();
|
|
|
|
|
|
|
|
let found =
|
|
|
|
find_nodes_by_name_recursively(entries.clone(), "Deno".to_string());
|
|
|
|
assert_eq!(found.len(), 2);
|
|
|
|
assert_eq!(found[0].name, "Deno".to_string());
|
|
|
|
assert_eq!(found[1].name, "Deno".to_string());
|
|
|
|
|
|
|
|
let found =
|
|
|
|
find_nodes_by_name_recursively(entries.clone(), "Deno.test".to_string());
|
|
|
|
assert_eq!(found.len(), 3);
|
|
|
|
assert_eq!(found[0].name, "test".to_string());
|
|
|
|
assert_eq!(found[1].name, "test".to_string());
|
|
|
|
assert_eq!(found[2].name, "test".to_string());
|
|
|
|
|
|
|
|
let found =
|
|
|
|
find_nodes_by_name_recursively(entries.clone(), "Deno.Inner.a".to_string());
|
|
|
|
assert_eq!(found.len(), 1);
|
|
|
|
assert_eq!(found[0].name, "a".to_string());
|
|
|
|
|
|
|
|
let found =
|
|
|
|
find_nodes_by_name_recursively(entries.clone(), "Deno.test.a".to_string());
|
|
|
|
assert_eq!(found.len(), 0);
|
|
|
|
|
|
|
|
let found = find_nodes_by_name_recursively(entries, "a.b.c".to_string());
|
|
|
|
assert_eq!(found.len(), 0);
|
|
|
|
}
|