Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): evaluate code snippets in JSDoc and markdown #25220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
d79a10f
feat(cli): make `deno test --doc` run doc tests
magurotuna Aug 26, 2024
fb2b27e
fix integration tests
magurotuna Aug 26, 2024
b95d187
enrich test_watch_doc
magurotuna Aug 26, 2024
26497e2
fmt and clippy
magurotuna Aug 26, 2024
20b2d06
migrate itest! to specs
magurotuna Aug 26, 2024
f8def17
update the count
magurotuna Aug 26, 2024
701f5d9
remove todo comment
magurotuna Aug 26, 2024
4d9f6f3
skip ambient declarations
magurotuna Aug 28, 2024
56f4c33
add spec tests
magurotuna Aug 28, 2024
55a28e8
address duplicate identifier issue
magurotuna Aug 28, 2024
d80e178
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Aug 28, 2024
59dcc59
fix help message of --doc option
magurotuna Aug 28, 2024
5147611
move extract logic to crate root
magurotuna Aug 28, 2024
b872b8c
deal with overload correctly
magurotuna Aug 28, 2024
288ab93
handle top-level defined vars
magurotuna Aug 29, 2024
3795d8c
add --doc option to check command
magurotuna Aug 28, 2024
3daecab
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Aug 29, 2024
56f0c47
remove unused struct
magurotuna Aug 29, 2024
4769d56
add test
magurotuna Aug 29, 2024
de4854f
fix tests
magurotuna Aug 29, 2024
8eb6868
fix test for windows
magurotuna Aug 30, 2024
9dbeed5
try using url_file()
magurotuna Aug 30, 2024
e2b8135
add a test
magurotuna Aug 30, 2024
f5e886e
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Aug 30, 2024
c5d8a99
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Aug 31, 2024
418d390
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Aug 31, 2024
64e40c8
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Sep 5, 2024
95d864d
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Sep 6, 2024
3242ea5
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Sep 12, 2024
53b6e85
make expected test outputs not rely on the specific directory structure
magurotuna Sep 12, 2024
cef7c94
move extract.rs to one level deeper
magurotuna Sep 12, 2024
6279721
remove leftover
magurotuna Sep 12, 2024
00280eb
refactor: extract common part of getting pseudo File instances for do…
magurotuna Sep 12, 2024
0c8b4c6
do not assume specific location where assertion fails
magurotuna Sep 12, 2024
13235e2
add test case to ensure permission works in doc tests
magurotuna Sep 13, 2024
fbc7b8e
not filter specifiers
magurotuna Sep 13, 2024
50d87d3
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Sep 13, 2024
43aaa6b
Merge branch 'main' into magurotuna/deno-test-doc-actually-run
magurotuna Sep 17, 2024
ff87ee0
add --doc-only flag to check subcommand
magurotuna Sep 17, 2024
c9f139e
fix
magurotuna Sep 17, 2024
2d4ce94
fix
magurotuna Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub struct CacheFlags {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CheckFlags {
pub files: Vec<String>,
pub doc: bool,
pub doc_only: bool,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -1694,6 +1696,19 @@ Unless --reload is specified, this command will not re-download already cached d
.conflicts_with("no-remote")
.hide(true)
)
.arg(
Arg::new("doc")
.long("doc")
.help("Type-check code blocks in JSDoc as well as actual code")
.action(ArgAction::SetTrue)
)
.arg(
Arg::new("doc-only")
.long("doc-only")
.help("Type-check code blocks in JSDoc and Markdown only")
.action(ArgAction::SetTrue)
.conflicts_with("doc")
)
.arg(
Arg::new("file")
.num_args(1..)
Expand Down Expand Up @@ -2789,7 +2804,7 @@ or <c>**/__tests__/**</>:
.arg(
Arg::new("doc")
.long("doc")
.help("Type-check code blocks in JSDoc and Markdown")
.help("Evaluate code blocks in JSDoc and Markdown")
.action(ArgAction::SetTrue)
.help_heading(TEST_HEADING),
)
Expand Down Expand Up @@ -4121,7 +4136,11 @@ fn check_parse(
if matches.get_flag("all") || matches.get_flag("remote") {
flags.type_check_mode = TypeCheckMode::All;
}
flags.subcommand = DenoSubcommand::Check(CheckFlags { files });
flags.subcommand = DenoSubcommand::Check(CheckFlags {
files,
doc: matches.get_flag("doc"),
doc_only: matches.get_flag("doc-only"),
});
Ok(())
}

Expand Down Expand Up @@ -6862,19 +6881,64 @@ mod tests {
Flags {
subcommand: DenoSubcommand::Check(CheckFlags {
files: svec!["script.ts"],
doc: false,
doc_only: false,
}),
type_check_mode: TypeCheckMode::Local,
..Flags::default()
}
);

let r = flags_from_vec(svec!["deno", "check", "--doc", "script.ts"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Check(CheckFlags {
files: svec!["script.ts"],
doc: true,
doc_only: false,
}),
type_check_mode: TypeCheckMode::Local,
..Flags::default()
}
);

let r = flags_from_vec(svec!["deno", "check", "--doc-only", "markdown.md"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Check(CheckFlags {
files: svec!["markdown.md"],
doc: false,
doc_only: true,
}),
type_check_mode: TypeCheckMode::Local,
..Flags::default()
}
);

// `--doc` and `--doc-only` are mutually exclusive
let r = flags_from_vec(svec![
"deno",
"check",
"--doc",
"--doc-only",
"script.ts"
]);
assert_eq!(
r.unwrap_err().kind(),
clap::error::ErrorKind::ArgumentConflict
);

for all_flag in ["--remote", "--all"] {
let r = flags_from_vec(svec!["deno", "check", all_flag, "script.ts"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Check(CheckFlags {
files: svec!["script.ts"],
doc: false,
doc_only: false,
}),
type_check_mode: TypeCheckMode::All,
..Flags::default()
Expand Down
13 changes: 3 additions & 10 deletions cli/lsp/testing/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,9 @@ impl TestRun {
&cli_options.permissions_options(),
)?;
let main_graph_container = factory.main_module_graph_container().await?;
test::check_specifiers(
factory.file_fetcher()?,
main_graph_container,
self
.queue
.iter()
.map(|s| (s.clone(), test::TestMode::Executable))
.collect(),
)
.await?;
main_graph_container
.check_specifiers(&self.queue.iter().cloned().collect::<Vec<_>>())
.await?;

let (concurrent_jobs, fail_fast) =
if let DenoSubcommand::Test(test_flags) = cli_options.sub_command() {
Expand Down
7 changes: 1 addition & 6 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::installer::install_from_entrypoints(flags, &cache_flags.files).await
}),
DenoSubcommand::Check(check_flags) => spawn_subcommand(async move {
let factory = CliFactory::from_flags(flags);
let main_graph_container =
factory.main_module_graph_container().await?;
main_graph_container
.load_and_type_check_files(&check_flags.files)
.await
tools::check::check(flags, check_flags).await
}),
DenoSubcommand::Clean => spawn_subcommand(async move {
tools::clean::clean()
Expand Down
46 changes: 46 additions & 0 deletions cli/tools/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use once_cell::sync::Lazy;
use regex::Regex;

use crate::args::check_warn_tsconfig;
use crate::args::CheckFlags;
use crate::args::CliOptions;
use crate::args::Flags;
use crate::args::TsConfig;
use crate::args::TsConfigType;
use crate::args::TsTypeLib;
Expand All @@ -24,13 +26,57 @@ use crate::cache::CacheDBHash;
use crate::cache::Caches;
use crate::cache::FastInsecureHasher;
use crate::cache::TypeCheckCache;
use crate::factory::CliFactory;
use crate::graph_util::BuildFastCheckGraphOptions;
use crate::graph_util::ModuleGraphBuilder;
use crate::npm::CliNpmResolver;
use crate::tsc;
use crate::tsc::Diagnostics;
use crate::util::extract;
use crate::util::path::to_percent_decoded_str;

pub async fn check(
flags: Arc<Flags>,
check_flags: CheckFlags,
) -> Result<(), AnyError> {
let factory = CliFactory::from_flags(flags);

let main_graph_container = factory.main_module_graph_container().await?;

let specifiers =
main_graph_container.collect_specifiers(&check_flags.files)?;
if specifiers.is_empty() {
log::warn!("{} No matching files found.", colors::yellow("Warning"));
}

let specifiers_for_typecheck = if check_flags.doc || check_flags.doc_only {
let file_fetcher = factory.file_fetcher()?;

let mut specifiers_for_typecheck = if check_flags.doc {
specifiers.clone()
} else {
vec![]
};

for s in specifiers {
let file = file_fetcher.fetch_bypass_permissions(&s).await?;
let snippet_files = extract::extract_snippet_files(file)?;
for snippet_file in snippet_files {
specifiers_for_typecheck.push(snippet_file.specifier.clone());
file_fetcher.insert_memory_files(snippet_file);
}
}

specifiers_for_typecheck
} else {
specifiers
};

main_graph_container
.check_specifiers(&specifiers_for_typecheck)
.await
}

/// Options for performing a check of a module graph. Note that the decision to
/// emit or not is determined by the `ts_config` settings.
pub struct CheckOptions {
Expand Down
Loading