1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-09 15:48:16 -05:00

feat(doc): display all overloads in cli details view (#6186)

This commit is contained in:
uki00a 2020-06-09 21:12:47 +09:00 committed by GitHub
parent b3e189ee4f
commit 2b2d800b43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 34 deletions

View file

@ -24,43 +24,50 @@ pub use parser::DocParser;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub fn find_node_by_name_recursively( pub fn find_nodes_by_name_recursively(
doc_nodes: Vec<DocNode>, doc_nodes: Vec<DocNode>,
name: String, name: String,
) -> Option<DocNode> { ) -> Vec<DocNode> {
let mut parts = name.splitn(2, '.'); let mut parts = name.splitn(2, '.');
let name = parts.next(); let name = parts.next();
let leftover = parts.next(); let leftover = parts.next();
name?; if name.is_none() {
let node = find_node_by_name(doc_nodes, name.unwrap().to_string()); return doc_nodes;
match node { }
Some(node) => match node.kind {
DocNodeKind::Namespace => { let name = name.unwrap();
if let Some(leftover) = leftover { let doc_nodes = find_nodes_by_name(doc_nodes, name.to_string());
find_node_by_name_recursively(
node.namespace_def.unwrap().elements, let mut found: Vec<DocNode> = vec![];
leftover.to_string(), match leftover {
) Some(leftover) => {
} else { for node in doc_nodes {
Some(node) let children = find_children_by_name(node, leftover.to_string());
} found.extend(children);
} }
_ => { found
if leftover.is_none() { }
Some(node) None => doc_nodes,
} else {
None
}
}
},
_ => None,
} }
} }
fn find_node_by_name(doc_nodes: Vec<DocNode>, name: String) -> Option<DocNode> { fn find_nodes_by_name(doc_nodes: Vec<DocNode>, name: String) -> Vec<DocNode> {
let node = doc_nodes.iter().find(|node| node.name == name); let mut found: Vec<DocNode> = vec![];
match node { for node in doc_nodes {
Some(node) => Some(node.clone()), if node.name == name {
None => None, found.push(node);
}
}
found
}
fn find_children_by_name(node: DocNode, name: String) -> Vec<DocNode> {
match node.kind {
DocNodeKind::Namespace => {
let namespace_def = node.namespace_def.unwrap();
find_nodes_by_name_recursively(namespace_def.elements, name)
}
// TODO(#4516) handle class, interface etc...
_ => vec![],
} }
} }

View file

@ -1557,3 +1557,51 @@ export type numLit = 5;
]); ]);
assert_eq!(actual, expected_json); assert_eq!(actual, expected_json);
} }
#[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);
}

View file

@ -578,13 +578,17 @@ async fn doc_command(
serde_json::to_writer_pretty(writer, &doc_nodes).map_err(ErrBox::from) serde_json::to_writer_pretty(writer, &doc_nodes).map_err(ErrBox::from)
} else { } else {
let details = if let Some(filter) = maybe_filter { let details = if let Some(filter) = maybe_filter {
let node = doc::find_node_by_name_recursively(doc_nodes, filter.clone()); let nodes =
if let Some(node) = node { doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
doc::printer::format_details(node) if nodes.is_empty() {
} else {
eprintln!("Node {} was not found!", filter); eprintln!("Node {} was not found!", filter);
std::process::exit(1); std::process::exit(1);
} }
let mut details = String::new();
for node in nodes {
details.push_str(doc::printer::format_details(node).as_str());
}
details
} else { } else {
doc::printer::format(doc_nodes) doc::printer::format(doc_nodes)
}; };

View file

@ -0,0 +1,6 @@
/* eslint-disable */
export namespace NS {
export function test(name: string, fn: Function): void;
export function test(options: object): void;
export function test(name: string | object, fn?: Function): void {}
}

View file

@ -0,0 +1,12 @@
Defined in [WILDCARD]/cli/tests/060_deno_doc_displays_all_overloads_in_details_view.ts:3:2
function test(name: string, fn: Function): void
Defined in [WILDCARD]/cli/tests/060_deno_doc_displays_all_overloads_in_details_view.ts:4:2
function test(options: object): void
Defined in [WILDCARD]cli/tests/060_deno_doc_displays_all_overloads_in_details_view.ts:5:2
function test(name: string | object, fn?: Function): void

View file

@ -1419,6 +1419,11 @@ itest!(_059_fs_relative_path_perm {
exit_code: 1, exit_code: 1,
}); });
itest!(_060_deno_doc_displays_all_overloads_in_details_view {
args: "doc 060_deno_doc_displays_all_overloads_in_details_view.ts NS.test",
output: "060_deno_doc_displays_all_overloads_in_details_view.ts.out",
});
itest!(js_import_detect { itest!(js_import_detect {
args: "run --quiet --reload js_import_detect.ts", args: "run --quiet --reload js_import_detect.ts",
output: "js_import_detect.ts.out", output: "js_import_detect.ts.out",