Skip to content

Commit

Permalink
fix: proper single file mode handling (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Dec 1, 2023
1 parent d2630f7 commit 359f268
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn generate(
Some(doc_nodes_by_url.keys().next().unwrap().clone());
}

let common_ancestor = find_common_ancestor(doc_nodes_by_url, false);
let common_ancestor = find_common_ancestor(doc_nodes_by_url, true);
let tt = setup_tt()?;
let ctx = GenerateCtx {
package_name: options.package_name,
Expand Down Expand Up @@ -247,7 +247,7 @@ pub fn generate(
all_symbols.clone(),
None,
)?;
files.insert("index.html".to_string(), index);
files.insert("./index.html".to_string(), index);
}

// All symbols (list of all symbols in all files)
Expand All @@ -257,7 +257,7 @@ pub fn generate(

let all_symbols_render =
render_all_symbols(&ctx, &partitions_by_kind, all_symbols.clone())?;
files.insert("all_symbols.html".to_string(), all_symbols_render);
files.insert("./all_symbols.html".to_string(), all_symbols_render);
}

// Pages for all discovered symbols
Expand All @@ -279,7 +279,17 @@ pub fn generate(
Some(short_path.clone()),
)?;

files.insert(format!("{short_path}/~/index.html"), index);
files.insert(
format!(
"{}/~/index.html",
if short_path.is_empty() {
"."
} else {
&short_path
}
),
index,
);

files.extend(generate_pages_for_file(
&ctx,
Expand Down Expand Up @@ -378,10 +388,11 @@ fn generate_pages_inner(
Vec::with_capacity(name_partitions.values().len() * 2);

for (name, doc_nodes) in name_partitions.iter() {
let file_name = if file.is_empty() { "." } else { file };
let file_name = if namespace_paths.is_empty() {
format!("{file}/~/{}.html", name)
format!("{file_name}/~/{}.html", name)
} else {
format!("{file}/~/{}.{name}.html", namespace_paths.join("."))
format!("{file_name}/~/{}.{name}.html", namespace_paths.join("."))
};

let page = render_page(
Expand Down
2 changes: 1 addition & 1 deletion src/html/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn render_params(

let content = items.join("");

format!(r#"<div class="ident">{content}</div>"#)
format!(r#"<div style="margin-left: 1rem;">{content}</div>"#)
}
}

Expand Down
125 changes: 125 additions & 0 deletions tests/html_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use deno_ast::ModuleSpecifier;
use deno_doc::html::*;
use deno_doc::{DocNode, DocParser, DocParserOptions};
use deno_graph::source::{LoadFuture, LoadResponse, Loader};
use deno_graph::{
BuildOptions, CapturingModuleAnalyzer, GraphKind, ModuleGraph,
};
use futures::future;
use indexmap::IndexMap;
use std::fs::read_dir;
use std::fs::read_to_string;
use std::rc::Rc;

struct SourceFileLoader {}

impl Loader for SourceFileLoader {
fn load(
&mut self,
specifier: &ModuleSpecifier,
_is_dynamic: bool,
_cache_setting: deno_graph::source::CacheSetting,
) -> LoadFuture {
let result = if specifier.scheme() == "file" {
let path = specifier.to_file_path().unwrap();
read_to_string(path)
.map(|content| {
Some(LoadResponse::Module {
specifier: specifier.clone(),
maybe_headers: None,
content: content.into(),
})
})
.map_err(|err| err.into())
} else {
Ok(None)
};
Box::pin(future::ready(result))
}
}

async fn get_files() -> IndexMap<ModuleSpecifier, Vec<DocNode>> {
let files = read_dir(
std::env::current_dir()
.unwrap()
.join("tests")
.join("testdata"),
)
.unwrap();

let source_files: Vec<ModuleSpecifier> = files
.into_iter()
.map(|entry| {
let entry = entry.unwrap();
ModuleSpecifier::from_file_path(entry.path()).unwrap()
})
.collect();

let mut loader = SourceFileLoader {};
let analyzer = CapturingModuleAnalyzer::default();
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
graph
.build(
source_files.clone(),
&mut loader,
BuildOptions {
module_analyzer: Some(&analyzer),
..Default::default()
},
)
.await;

let parser = DocParser::new(
&graph,
analyzer.as_capturing_parser(),
DocParserOptions {
diagnostics: false,
private: false,
},
)
.unwrap();

let mut source_files = source_files.clone();
source_files.sort();
let mut doc_nodes_by_url = IndexMap::with_capacity(source_files.len());
for source_file in source_files {
let nodes = parser.parse_with_reexports(&source_file).unwrap();
doc_nodes_by_url.insert(source_file, nodes);
}

doc_nodes_by_url
}

#[tokio::test]
async fn html_doc_files() {
let files = generate(
GenerateOptions {
package_name: None,
main_entrypoint: None,
global_symbols: Default::default(),
global_symbol_href_resolver: Rc::new(|_, _| String::new()),
url_resolver: Rc::new(default_url_resolver),
},
&get_files().await,
)
.unwrap();

let mut file_names = files.keys().collect::<Vec<_>>();
file_names.sort();

assert_eq!(
file_names,
[
"./all_symbols.html",
"./index.html",
"./~/Bar.html",
"./~/Foo.html",
"./~/index.html",
"fuse.js",
"page.css",
"search.js",
"search_index.js",
"styles.css",
]
);
}
3 changes: 3 additions & 0 deletions tests/testdata/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {}

export class Bar extends Foo {}

0 comments on commit 359f268

Please sign in to comment.