1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -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)]
mod tests;
pub fn find_node_by_name_recursively(
pub fn find_nodes_by_name_recursively(
doc_nodes: Vec<DocNode>,
name: String,
) -> Option<DocNode> {
) -> Vec<DocNode> {
let mut parts = name.splitn(2, '.');
let name = parts.next();
let leftover = parts.next();
name?;
let node = find_node_by_name(doc_nodes, name.unwrap().to_string());
match node {
Some(node) => match node.kind {
DocNodeKind::Namespace => {
if let Some(leftover) = leftover {
find_node_by_name_recursively(
node.namespace_def.unwrap().elements,
leftover.to_string(),
)
} else {
Some(node)
}
if name.is_none() {
return doc_nodes;
}
let name = name.unwrap();
let doc_nodes = find_nodes_by_name(doc_nodes, name.to_string());
let mut found: Vec<DocNode> = vec![];
match leftover {
Some(leftover) => {
for node in doc_nodes {
let children = find_children_by_name(node, leftover.to_string());
found.extend(children);
}
_ => {
if leftover.is_none() {
Some(node)
} else {
None
}
}
},
_ => None,
found
}
None => doc_nodes,
}
}
fn find_node_by_name(doc_nodes: Vec<DocNode>, name: String) -> Option<DocNode> {
let node = doc_nodes.iter().find(|node| node.name == name);
match node {
Some(node) => Some(node.clone()),
None => None,
fn find_nodes_by_name(doc_nodes: Vec<DocNode>, name: String) -> Vec<DocNode> {
let mut found: Vec<DocNode> = vec![];
for node in doc_nodes {
if node.name == name {
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);
}
#[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)
} else {
let details = if let Some(filter) = maybe_filter {
let node = doc::find_node_by_name_recursively(doc_nodes, filter.clone());
if let Some(node) = node {
doc::printer::format_details(node)
} else {
let nodes =
doc::find_nodes_by_name_recursively(doc_nodes, filter.clone());
if nodes.is_empty() {
eprintln!("Node {} was not found!", filter);
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 {
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

@ -500,7 +500,7 @@ fn ts_dependency_recompilation() {
function print(str: string): void {
console.log(str);
}
print(foo);",
)
.unwrap();
@ -1419,6 +1419,11 @@ itest!(_059_fs_relative_path_perm {
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 {
args: "run --quiet --reload js_import_detect.ts",
output: "js_import_detect.ts.out",