0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: create enum for --builtin doc flag (#17423)

This commit is contained in:
David Sherret 2023-01-14 12:39:56 -05:00 committed by GitHub
parent 1712a88e69
commit 01e02d3123
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 68 deletions

View file

@ -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>,
}
@ -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()

View file

@ -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,11 +23,9 @@ 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 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());
@ -60,7 +59,8 @@ pub async fn print_docs(
analyzer.as_capturing_parser(),
);
doc_parser.parse_module(&source_file_specifier)?.definitions
} else {
}
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,
@ -87,6 +87,7 @@ pub async fn print_docs(
ps.parsed_source_cache.as_capturing_parser(),
);
doc_parser.parse_with_reexports(&root_specifier)?
}
};
if doc_flags.json {