1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 08:39:09 -05:00

refactor: helpers methods on TypeCheckMode (#19393)

This commit is contained in:
David Sherret 2023-06-07 10:09:10 -04:00 committed by Bartek Iwańczuk
parent 82c3a56674
commit 7d44315ee6
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
8 changed files with 34 additions and 42 deletions

View file

@ -9,6 +9,7 @@ use clap::Command;
use clap::ValueHint; use clap::ValueHint;
use deno_core::resolve_url_or_path; use deno_core::resolve_url_or_path;
use deno_core::url::Url; use deno_core::url::Url;
use deno_graph::GraphKind;
use deno_runtime::permissions::parse_sys_kind; use deno_runtime::permissions::parse_sys_kind;
use log::debug; use log::debug;
use log::Level; use log::Level;
@ -255,6 +256,25 @@ pub enum TypeCheckMode {
Local, Local,
} }
impl TypeCheckMode {
/// Gets if type checking will occur under this mode.
pub fn is_true(&self) -> bool {
match self {
Self::None => false,
Self::Local | Self::All => true,
}
}
/// Gets the corresponding module `GraphKind` that should be created
/// for the current `TypeCheckMode`.
pub fn as_graph_kind(&self) -> GraphKind {
match self.is_true() {
true => GraphKind::All,
false => GraphKind::CodeOnly,
}
}
}
impl Default for TypeCheckMode { impl Default for TypeCheckMode {
fn default() -> Self { fn default() -> Self {
Self::None Self::None

View file

@ -8,7 +8,6 @@ use crate::args::Lockfile;
use crate::args::PackageJsonDepsProvider; use crate::args::PackageJsonDepsProvider;
use crate::args::StorageKeyResolver; use crate::args::StorageKeyResolver;
use crate::args::TsConfigType; use crate::args::TsConfigType;
use crate::args::TypeCheckMode;
use crate::cache::Caches; use crate::cache::Caches;
use crate::cache::DenoDir; use crate::cache::DenoDir;
use crate::cache::DenoDirProvider; use crate::cache::DenoDirProvider;
@ -541,14 +540,10 @@ impl CliFactory {
pub fn graph_container(&self) -> &Arc<ModuleGraphContainer> { pub fn graph_container(&self) -> &Arc<ModuleGraphContainer> {
self.services.graph_container.get_or_init(|| { self.services.graph_container.get_or_init(|| {
let graph_kind = match self.options.sub_command() { let graph_kind = match self.options.sub_command() {
// todo(dsherret): ideally the graph container would not be used
// for deno cache because it doesn't dynamically load modules
DenoSubcommand::Cache(_) => GraphKind::All, DenoSubcommand::Cache(_) => GraphKind::All,
_ => { _ => self.options.type_check_mode().as_graph_kind(),
if self.options.type_check_mode() == TypeCheckMode::None {
GraphKind::CodeOnly
} else {
GraphKind::All
}
}
}; };
Arc::new(ModuleGraphContainer::new(graph_kind)) Arc::new(ModuleGraphContainer::new(graph_kind))
}) })

View file

@ -3,7 +3,6 @@
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::Lockfile; use crate::args::Lockfile;
use crate::args::TsTypeLib; use crate::args::TsTypeLib;
use crate::args::TypeCheckMode;
use crate::cache; use crate::cache;
use crate::cache::ParsedSourceCache; use crate::cache::ParsedSourceCache;
use crate::colors; use crate::colors;
@ -57,7 +56,7 @@ pub fn graph_valid_with_cli_options(
roots, roots,
GraphValidOptions { GraphValidOptions {
is_vendoring: false, is_vendoring: false,
follow_type_only: options.type_check_mode() != TypeCheckMode::None, follow_type_only: options.type_check_mode().is_true(),
check_js: options.check_js(), check_js: options.check_js(),
}, },
) )
@ -229,9 +228,7 @@ impl ModuleGraphBuilder {
) )
.await?; .await?;
if graph.has_node_specifier if graph.has_node_specifier && self.options.type_check_mode().is_true() {
&& self.options.type_check_mode() != TypeCheckMode::None
{
self self
.npm_resolver .npm_resolver
.inject_synthetic_types_node_package() .inject_synthetic_types_node_package()
@ -251,12 +248,7 @@ impl ModuleGraphBuilder {
let graph_resolver = cli_resolver.as_graph_resolver(); let graph_resolver = cli_resolver.as_graph_resolver();
let graph_npm_resolver = cli_resolver.as_graph_npm_resolver(); let graph_npm_resolver = cli_resolver.as_graph_npm_resolver();
let analyzer = self.parsed_source_cache.as_analyzer(); let analyzer = self.parsed_source_cache.as_analyzer();
let should_type_check = let graph_kind = self.options.type_check_mode().as_graph_kind();
self.options.type_check_mode() != TypeCheckMode::None;
let graph_kind = match should_type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let mut graph = ModuleGraph::new(graph_kind); let mut graph = ModuleGraph::new(graph_kind);
self self
.build_graph_with_npm_resolution( .build_graph_with_npm_resolution(
@ -280,7 +272,7 @@ impl ModuleGraphBuilder {
graph_lock_or_exit(&graph, &mut lockfile.lock()); graph_lock_or_exit(&graph, &mut lockfile.lock());
} }
if should_type_check { if self.options.type_check_mode().is_true() {
self self
.type_checker .type_checker
.check( .check(

View file

@ -3,7 +3,6 @@
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::DenoSubcommand; use crate::args::DenoSubcommand;
use crate::args::TsTypeLib; use crate::args::TsTypeLib;
use crate::args::TypeCheckMode;
use crate::cache::ParsedSourceCache; use crate::cache::ParsedSourceCache;
use crate::emit::Emitter; use crate::emit::Emitter;
use crate::graph_util::graph_lock_or_exit; use crate::graph_util::graph_lock_or_exit;
@ -169,7 +168,7 @@ impl ModuleLoadPreparer {
drop(_pb_clear_guard); drop(_pb_clear_guard);
// type check if necessary // type check if necessary
if self.options.type_check_mode() != TypeCheckMode::None if self.options.type_check_mode().is_true()
&& !self.graph_container.is_type_checked(&roots, lib) && !self.graph_container.is_type_checked(&roots, lib)
{ {
let graph = Arc::new(graph.segment(&roots)); let graph = Arc::new(graph.segment(&roots));

View file

@ -2,7 +2,6 @@
use crate::args::BenchOptions; use crate::args::BenchOptions;
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::TypeCheckMode;
use crate::colors; use crate::colors;
use crate::display::write_json_to_stdout; use crate::display::write_json_to_stdout;
use crate::factory::CliFactory; use crate::factory::CliFactory;
@ -31,7 +30,6 @@ use deno_core::task::spawn;
use deno_core::task::spawn_blocking; use deno_core::task::spawn_blocking;
use deno_core::v8; use deno_core::v8;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::tokio_util::create_and_run_current_thread; use deno_runtime::tokio_util::create_and_run_current_thread;
@ -694,11 +692,7 @@ pub async fn run_benchmarks_with_watch(
// file would have impact on other files, which is undesirable. // file would have impact on other files, which is undesirable.
let permissions = let permissions =
Permissions::from_options(&cli_options.permissions_options())?; Permissions::from_options(&cli_options.permissions_options())?;
let type_check = cli_options.type_check_mode() != TypeCheckMode::None; let graph_kind = cli_options.type_check_mode().as_graph_kind();
let graph_kind = match type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let resolver = |changed: Option<Vec<PathBuf>>| { let resolver = |changed: Option<Vec<PathBuf>>| {
let paths_to_watch = bench_options.files.include.clone(); let paths_to_watch = bench_options.files.include.clone();

View file

@ -12,7 +12,6 @@ use crate::args::BundleFlags;
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::Flags; use crate::args::Flags;
use crate::args::TsConfigType; use crate::args::TsConfigType;
use crate::args::TypeCheckMode;
use crate::factory::CliFactory; use crate::factory::CliFactory;
use crate::graph_util::error_for_any_npm_specifier; use crate::graph_util::error_for_any_npm_specifier;
use crate::util; use crate::util;
@ -157,7 +156,7 @@ fn bundle_module_graph(
let ts_config_result = let ts_config_result =
cli_options.resolve_ts_config_for_emit(TsConfigType::Bundle)?; cli_options.resolve_ts_config_for_emit(TsConfigType::Bundle)?;
if cli_options.type_check_mode() == TypeCheckMode::None { if !cli_options.type_check_mode().is_true() {
if let Some(ignored_options) = ts_config_result.maybe_ignored_options { if let Some(ignored_options) = ts_config_result.maybe_ignored_options {
log::warn!("{}", ignored_options); log::warn!("{}", ignored_options);
} }

View file

@ -2,7 +2,6 @@
use crate::args::CompileFlags; use crate::args::CompileFlags;
use crate::args::Flags; use crate::args::Flags;
use crate::args::TypeCheckMode;
use crate::factory::CliFactory; use crate::factory::CliFactory;
use crate::standalone::is_standalone_binary; use crate::standalone::is_standalone_binary;
use crate::util::path::path_has_trailing_slash; use crate::util::path::path_has_trailing_slash;
@ -50,15 +49,15 @@ pub async fn compile(
.await?, .await?,
) )
.unwrap(); .unwrap();
let graph = if cli_options.type_check_mode() == TypeCheckMode::None { let graph = if cli_options.type_check_mode().is_true() {
graph
} else {
// In this case, the previous graph creation did type checking, which will // In this case, the previous graph creation did type checking, which will
// create a module graph with types information in it. We don't want to // create a module graph with types information in it. We don't want to
// store that in the eszip so create a code only module graph from scratch. // store that in the eszip so create a code only module graph from scratch.
module_graph_builder module_graph_builder
.create_graph(GraphKind::CodeOnly, module_roots) .create_graph(GraphKind::CodeOnly, module_roots)
.await? .await?
} else {
graph
}; };
let parser = parsed_source_cache.as_capturing_parser(); let parser = parsed_source_cache.as_capturing_parser();

View file

@ -3,7 +3,6 @@
use crate::args::CliOptions; use crate::args::CliOptions;
use crate::args::FilesConfig; use crate::args::FilesConfig;
use crate::args::TestOptions; use crate::args::TestOptions;
use crate::args::TypeCheckMode;
use crate::colors; use crate::colors;
use crate::display; use crate::display;
use crate::factory::CliFactory; use crate::factory::CliFactory;
@ -39,7 +38,6 @@ use deno_core::task::spawn_blocking;
use deno_core::url::Url; use deno_core::url::Url;
use deno_core::v8; use deno_core::v8;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_graph::GraphKind;
use deno_runtime::deno_io::Stdio; use deno_runtime::deno_io::Stdio;
use deno_runtime::deno_io::StdioPipe; use deno_runtime::deno_io::StdioPipe;
use deno_runtime::fmt_errors::format_js_error; use deno_runtime::fmt_errors::format_js_error;
@ -1707,11 +1705,7 @@ pub async fn run_tests_with_watch(
// file would have impact on other files, which is undesirable. // file would have impact on other files, which is undesirable.
let permissions = let permissions =
Permissions::from_options(&cli_options.permissions_options())?; Permissions::from_options(&cli_options.permissions_options())?;
let type_check = cli_options.type_check_mode() != TypeCheckMode::None; let graph_kind = cli_options.type_check_mode().as_graph_kind();
let graph_kind = match type_check {
true => GraphKind::All,
false => GraphKind::CodeOnly,
};
let log_level = cli_options.log_level(); let log_level = cli_options.log_level();
let resolver = |changed: Option<Vec<PathBuf>>| { let resolver = |changed: Option<Vec<PathBuf>>| {