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:
parent
b3e189ee4f
commit
2b2d800b43
6 changed files with 116 additions and 34 deletions
|
@ -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![],
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
12
cli/main.rs
12
cli/main.rs
|
@ -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)
|
||||
};
|
||||
|
|
|
@ -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 {}
|
||||
}
|
|
@ -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
|
||||
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue