diff options
-rw-r--r-- | cli/args/flags.rs | 37 | ||||
-rw-r--r-- | cli/tools/doc.rs | 121 |
2 files changed, 90 insertions, 68 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 2e99aab82..84c1d083b 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -95,10 +95,22 @@ pub struct CoverageFlags { } #[derive(Clone, Debug, Eq, PartialEq)] +pub enum DocSourceFileFlag { + Builtin, + Path(String), +} + +impl Default for DocSourceFileFlag { + fn default() -> Self { + Self::Builtin + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] pub struct DocFlags { pub private: bool, pub json: bool, - pub source_file: Option<String>, + pub source_file: DocSourceFileFlag, pub filter: Option<String>, } @@ -2444,7 +2456,16 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); reload_arg_parse(flags, matches); - let source_file = matches.value_of("source_file").map(String::from); + let source_file = matches + .value_of("source_file") + .map(|value| { + if value == "--builtin" { + DocSourceFileFlag::Builtin + } else { + DocSourceFileFlag::Path(value.to_string()) + } + }) + .unwrap_or_default(); let private = matches.is_present("private"); let json = matches.is_present("json"); let filter = matches.value_of("filter").map(String::from); @@ -4916,7 +4937,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { - source_file: Some("script.ts".to_owned()), + source_file: DocSourceFileFlag::Path("script.ts".to_owned()), private: false, json: false, filter: None, @@ -5922,7 +5943,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: true, - source_file: Some("path/to/module.ts".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.ts".to_string()), filter: None, }), ..Flags::default() @@ -5941,7 +5962,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: Some("path/to/module.ts".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.ts".to_string()), filter: Some("SomeClass.someField".to_string()), }), ..Flags::default() @@ -5955,7 +5976,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: None, + source_file: Default::default(), filter: None, }), ..Flags::default() @@ -5969,7 +5990,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: Some("--builtin".to_string()), + source_file: DocSourceFileFlag::Builtin, filter: Some("Deno.Listener".to_string()), }), ..Flags::default() @@ -5984,7 +6005,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: true, json: false, - source_file: Some("path/to/module.js".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.js".to_string()), filter: None, }), ..Flags::default() diff --git a/cli/tools/doc.rs b/cli/tools/doc.rs index 0067e485a..12f7b7dc4 100644 --- a/cli/tools/doc.rs +++ b/cli/tools/doc.rs @@ -1,6 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use crate::args::DocFlags; +use crate::args::DocSourceFileFlag; use crate::args::Flags; use crate::colors; use crate::display::write_json_to_stdout; @@ -22,71 +23,71 @@ pub async fn print_docs( doc_flags: DocFlags, ) -> Result<(), AnyError> { let ps = ProcState::build(flags).await?; - let source_file = doc_flags - .source_file - .unwrap_or_else(|| "--builtin".to_string()); - let mut doc_nodes = if source_file == "--builtin" { - let source_file_specifier = - ModuleSpecifier::parse("deno://lib.deno.d.ts").unwrap(); - let content = get_types_declaration_file_text(ps.options.unstable()); - let mut loader = deno_graph::source::MemoryLoader::new( - vec![( - source_file_specifier.to_string(), - deno_graph::source::Source::Module { - specifier: source_file_specifier.to_string(), - content, - maybe_headers: None, + let mut doc_nodes = match doc_flags.source_file { + DocSourceFileFlag::Builtin => { + let source_file_specifier = + ModuleSpecifier::parse("deno://lib.deno.d.ts").unwrap(); + let content = get_types_declaration_file_text(ps.options.unstable()); + let mut loader = deno_graph::source::MemoryLoader::new( + vec![( + source_file_specifier.to_string(), + deno_graph::source::Source::Module { + specifier: source_file_specifier.to_string(), + content, + maybe_headers: None, + }, + )], + Vec::new(), + ); + let analyzer = deno_graph::CapturingModuleAnalyzer::default(); + let graph = deno_graph::create_graph( + vec![(source_file_specifier.clone(), ModuleKind::Esm)], + &mut loader, + deno_graph::GraphOptions { + is_dynamic: false, + imports: None, + resolver: None, + module_analyzer: Some(&analyzer), + reporter: None, }, - )], - Vec::new(), - ); - let analyzer = deno_graph::CapturingModuleAnalyzer::default(); - let graph = deno_graph::create_graph( - vec![(source_file_specifier.clone(), ModuleKind::Esm)], - &mut loader, - deno_graph::GraphOptions { - is_dynamic: false, - imports: None, - resolver: None, - module_analyzer: Some(&analyzer), - reporter: None, - }, - ) - .await; - let doc_parser = doc::DocParser::new( - graph, - doc_flags.private, - analyzer.as_capturing_parser(), - ); - doc_parser.parse_module(&source_file_specifier)?.definitions - } else { - let module_specifier = resolve_url_or_path(&source_file)?; + ) + .await; + let doc_parser = doc::DocParser::new( + graph, + doc_flags.private, + analyzer.as_capturing_parser(), + ); + doc_parser.parse_module(&source_file_specifier)?.definitions + } + DocSourceFileFlag::Path(source_file) => { + let module_specifier = resolve_url_or_path(&source_file)?; - // If the root module has external types, the module graph won't redirect it, - // so instead create a dummy file which exports everything from the actual file being documented. - let root_specifier = resolve_url_or_path("./$deno$doc.ts").unwrap(); - let root = File { - local: PathBuf::from("./$deno$doc.ts"), - maybe_types: None, - media_type: MediaType::TypeScript, - source: format!("export * from \"{}\";", module_specifier).into(), - specifier: root_specifier.clone(), - maybe_headers: None, - }; + // If the root module has external types, the module graph won't redirect it, + // so instead create a dummy file which exports everything from the actual file being documented. + let root_specifier = resolve_url_or_path("./$deno$doc.ts").unwrap(); + let root = File { + local: PathBuf::from("./$deno$doc.ts"), + maybe_types: None, + media_type: MediaType::TypeScript, + source: format!("export * from \"{}\";", module_specifier).into(), + specifier: root_specifier.clone(), + maybe_headers: None, + }; - // Save our fake file into file fetcher cache. - ps.file_fetcher.insert_cached(root); + // Save our fake file into file fetcher cache. + ps.file_fetcher.insert_cached(root); - let graph = ps - .create_graph(vec![(root_specifier.clone(), ModuleKind::Esm)]) - .await?; - let doc_parser = doc::DocParser::new( - graph, - doc_flags.private, - ps.parsed_source_cache.as_capturing_parser(), - ); - doc_parser.parse_with_reexports(&root_specifier)? + let graph = ps + .create_graph(vec![(root_specifier.clone(), ModuleKind::Esm)]) + .await?; + let doc_parser = doc::DocParser::new( + graph, + doc_flags.private, + ps.parsed_source_cache.as_capturing_parser(), + ); + doc_parser.parse_with_reexports(&root_specifier)? + } }; if doc_flags.json { |