mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
feat(cli): support importmap flag with deno doc subcommand (#7821)
Fixes #7783
This commit is contained in:
parent
5f3028af13
commit
fede13f2eb
7 changed files with 101 additions and 35 deletions
25
cli/flags.rs
25
cli/flags.rs
|
@ -634,6 +634,7 @@ fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
|||
}
|
||||
|
||||
fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||
importmap_arg_parse(flags, matches);
|
||||
reload_arg_parse(flags, matches);
|
||||
|
||||
let source_file = matches.value_of("source_file").map(String::from);
|
||||
|
@ -978,6 +979,7 @@ Show documentation for runtime built-ins:
|
|||
deno doc
|
||||
deno doc --builtin Deno.Listener",
|
||||
)
|
||||
.arg(importmap_arg())
|
||||
.arg(reload_arg())
|
||||
.arg(
|
||||
Arg::with_name("json")
|
||||
|
@ -2420,6 +2422,29 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doc_importmap() {
|
||||
let r = flags_from_vec_safe(svec![
|
||||
"deno",
|
||||
"doc",
|
||||
"--importmap=importmap.json",
|
||||
"script.ts"
|
||||
]);
|
||||
assert_eq!(
|
||||
r.unwrap(),
|
||||
Flags {
|
||||
subcommand: DenoSubcommand::Doc {
|
||||
source_file: Some("script.ts".to_owned()),
|
||||
private: false,
|
||||
json: false,
|
||||
filter: None,
|
||||
},
|
||||
import_map_path: Some("importmap.json".to_owned()),
|
||||
..Flags::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_multiple() {
|
||||
let r =
|
||||
|
|
93
cli/main.rs
93
cli/main.rs
|
@ -78,6 +78,7 @@ use deno_doc::parser::DocFileLoader;
|
|||
use flags::DenoSubcommand;
|
||||
use flags::Flags;
|
||||
use global_state::exit_unstable;
|
||||
use import_map::ImportMap;
|
||||
use log::Level;
|
||||
use log::LevelFilter;
|
||||
use std::env;
|
||||
|
@ -319,6 +320,59 @@ async fn bundle_command(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
struct DocLoader {
|
||||
fetcher: SourceFileFetcher,
|
||||
maybe_import_map: Option<ImportMap>,
|
||||
}
|
||||
|
||||
impl DocFileLoader for DocLoader {
|
||||
fn resolve(
|
||||
&self,
|
||||
specifier: &str,
|
||||
referrer: &str,
|
||||
) -> Result<String, doc::DocError> {
|
||||
let maybe_resolved =
|
||||
if let Some(import_map) = self.maybe_import_map.as_ref() {
|
||||
import_map
|
||||
.resolve(specifier, referrer)
|
||||
.map_err(|e| doc::DocError::Resolve(e.to_string()))?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let resolved_specifier = if let Some(resolved) = maybe_resolved {
|
||||
resolved
|
||||
} else {
|
||||
ModuleSpecifier::resolve_import(specifier, referrer)
|
||||
.map_err(|e| doc::DocError::Resolve(e.to_string()))?
|
||||
};
|
||||
|
||||
Ok(resolved_specifier.to_string())
|
||||
}
|
||||
|
||||
fn load_source_code(
|
||||
&self,
|
||||
specifier: &str,
|
||||
) -> Pin<Box<dyn Future<Output = Result<String, doc::DocError>>>> {
|
||||
let fetcher = self.fetcher.clone();
|
||||
let specifier = ModuleSpecifier::resolve_url_or_path(specifier)
|
||||
.expect("Expected valid specifier");
|
||||
async move {
|
||||
let source_file = fetcher
|
||||
.fetch_source_file(&specifier, None, Permissions::allow_all())
|
||||
.await
|
||||
.map_err(|e| {
|
||||
doc::DocError::Io(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
e.to_string(),
|
||||
))
|
||||
})?;
|
||||
Ok(source_file.source_code)
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
async fn doc_command(
|
||||
flags: Flags,
|
||||
source_file: Option<String>,
|
||||
|
@ -329,41 +383,10 @@ async fn doc_command(
|
|||
let global_state = GlobalState::new(flags.clone())?;
|
||||
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
|
||||
|
||||
impl DocFileLoader for SourceFileFetcher {
|
||||
fn resolve(
|
||||
&self,
|
||||
specifier: &str,
|
||||
referrer: &str,
|
||||
) -> Result<String, doc::DocError> {
|
||||
ModuleSpecifier::resolve_import(specifier, referrer)
|
||||
.map(|specifier| specifier.to_string())
|
||||
.map_err(|e| doc::DocError::Resolve(e.to_string()))
|
||||
}
|
||||
|
||||
fn load_source_code(
|
||||
&self,
|
||||
specifier: &str,
|
||||
) -> Pin<Box<dyn Future<Output = Result<String, doc::DocError>>>> {
|
||||
let fetcher = self.clone();
|
||||
let specifier = ModuleSpecifier::resolve_url_or_path(specifier)
|
||||
.expect("Expected valid specifier");
|
||||
async move {
|
||||
let source_file = fetcher
|
||||
.fetch_source_file(&specifier, None, Permissions::allow_all())
|
||||
.await
|
||||
.map_err(|e| {
|
||||
doc::DocError::Io(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
e.to_string(),
|
||||
))
|
||||
})?;
|
||||
Ok(source_file.source_code)
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
|
||||
let loader = Box::new(global_state.file_fetcher.clone());
|
||||
let loader = Box::new(DocLoader {
|
||||
fetcher: global_state.file_fetcher.clone(),
|
||||
maybe_import_map: global_state.maybe_import_map.clone(),
|
||||
});
|
||||
let doc_parser = doc::DocParser::new(loader, private);
|
||||
|
||||
let parse_result = if source_file == "--builtin" {
|
||||
|
|
5
cli/tests/doc/importmap.json
Normal file
5
cli/tests/doc/importmap.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"imports": {
|
||||
"rex/": "./module/"
|
||||
}
|
||||
}
|
2
cli/tests/doc/module/fun.js
Normal file
2
cli/tests/doc/module/fun.js
Normal file
|
@ -0,0 +1,2 @@
|
|||
/** This is some documentation */
|
||||
export function fun(_a, _b) {}
|
1
cli/tests/doc/use_importmap.js
Normal file
1
cli/tests/doc/use_importmap.js
Normal file
|
@ -0,0 +1 @@
|
|||
export { fun } from "rex/fun.js";
|
5
cli/tests/doc/use_importmap.out
Normal file
5
cli/tests/doc/use_importmap.out
Normal file
|
@ -0,0 +1,5 @@
|
|||
Defined in [WILDCARD]/doc/module/fun.js:2:0
|
||||
|
||||
function fun(_a, _b)
|
||||
This is some documentation
|
||||
|
|
@ -2633,6 +2633,11 @@ itest!(deno_doc {
|
|||
output: "deno_doc.out",
|
||||
});
|
||||
|
||||
itest!(deno_doc_importmap {
|
||||
args: "doc --unstable --importmap=doc/importmap.json doc/use_importmap.js",
|
||||
output: "doc/use_importmap.out",
|
||||
});
|
||||
|
||||
itest!(compiler_js_error {
|
||||
args: "run --unstable compiler_js_error.ts",
|
||||
output: "compiler_js_error.ts.out",
|
||||
|
|
Loading…
Reference in a new issue