Skip to content

Commit

Permalink
refactor: create enum for --builtin doc flag (denoland#17423)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jan 14, 2023
1 parent 1712a88 commit 01e02d3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 68 deletions.
37 changes: 29 additions & 8 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,23 @@ pub struct CoverageFlags {
pub lcov: bool,
}

#[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>,
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
121 changes: 61 additions & 60 deletions cli/tools/doc.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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:https://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:https://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 {
Expand Down

0 comments on commit 01e02d3

Please sign in to comment.