1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

refactor: add "UnstableConfig" struct to cli/args/flags.rs (#21993)

This commit adds "UnstableConfig" struct which centralizes
handling of all "--unstable-*" flags.

Closes https://github.com/denoland/deno/issues/21920
This commit is contained in:
Bartek Iwańczuk 2024-01-22 18:37:28 +01:00 committed by GitHub
parent bc92f87298
commit d20c9e75d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 67 additions and 40 deletions

View file

@ -405,6 +405,17 @@ pub enum CaData {
Bytes(Vec<u8>),
}
#[derive(
Clone, Default, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize,
)]
pub struct UnstableConfig {
pub legacy_flag_enabled: bool, // --unstable
pub bare_node_builtins: bool, // --unstable-bare-node-builts
pub byonm: bool,
pub sloppy_imports: bool,
pub features: Vec<String>, // --unstabe-kv --unstable-cron
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Flags {
/// Vector of CLI arguments - these are user script arguments, all Deno
@ -460,12 +471,7 @@ pub struct Flags {
pub reload: bool,
pub seed: Option<u64>,
pub strace_ops: Option<Vec<String>>,
pub unstable: bool,
pub unstable_bare_node_builtins: bool,
pub unstable_byonm: bool,
pub unstable_sloppy_imports: bool,
pub unstable_workspaces: bool,
pub unstable_features: Vec<String>,
pub unstable_config: UnstableConfig,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub v8_flags: Vec<String>,
}
@ -865,19 +871,20 @@ pub fn flags_from_vec(args: Vec<String>) -> clap::error::Result<Flags> {
let mut flags = Flags::default();
if matches.get_flag("unstable") {
flags.unstable = true;
flags.unstable_config.legacy_flag_enabled = true;
}
for (name, _, _) in crate::UNSTABLE_GRANULAR_FLAGS {
if matches.get_flag(&format!("unstable-{}", name)) {
flags.unstable_features.push(name.to_string());
flags.unstable_config.features.push(name.to_string());
}
}
flags.unstable_bare_node_builtins =
flags.unstable_config.bare_node_builtins =
matches.get_flag("unstable-bare-node-builtins");
flags.unstable_byonm = matches.get_flag("unstable-byonm");
flags.unstable_sloppy_imports = matches.get_flag("unstable-sloppy-imports");
flags.unstable_config.byonm = matches.get_flag("unstable-byonm");
flags.unstable_config.sloppy_imports =
matches.get_flag("unstable-sloppy-imports");
if matches.get_flag("quiet") {
flags.log_level = Some(Level::Error);
@ -4199,7 +4206,10 @@ mod tests {
subcommand: DenoSubcommand::Run(RunFlags::new_default(
"script.ts".to_string()
)),
unstable: true,
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
..Default::default()
},
log_level: Some(Level::Error),
..Flags::default()
}
@ -7054,7 +7064,10 @@ mod tests {
reporter: Default::default(),
junit_path: None,
}),
unstable: true,
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
..Default::default()
},
no_prompt: true,
no_npm: true,
no_remote: true,
@ -8189,7 +8202,10 @@ mod tests {
cwd: None,
task: Some("build".to_string()),
}),
unstable: true,
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
..Default::default()
},
log_level: Some(log::Level::Error),
..Flags::default()
}
@ -8286,7 +8302,10 @@ mod tests {
},
watch: Default::default(),
}),
unstable: true,
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
..Default::default()
},
no_npm: true,
no_remote: true,
type_check_mode: TypeCheckMode::Local,
@ -8430,7 +8449,7 @@ mod tests {
#[test]
fn jupyter() {
let r = flags_from_vec(svec!["deno", "jupyter", "--unstable"]);
let r = flags_from_vec(svec!["deno", "jupyter"]);
assert_eq!(
r.unwrap(),
Flags {
@ -8439,12 +8458,11 @@ mod tests {
kernel: false,
conn_file: None,
}),
unstable: true,
..Flags::default()
}
);
let r = flags_from_vec(svec!["deno", "jupyter", "--unstable", "--install"]);
let r = flags_from_vec(svec!["deno", "jupyter", "--install"]);
assert_eq!(
r.unwrap(),
Flags {
@ -8453,7 +8471,6 @@ mod tests {
kernel: false,
conn_file: None,
}),
unstable: true,
..Flags::default()
}
);
@ -8461,7 +8478,6 @@ mod tests {
let r = flags_from_vec(svec![
"deno",
"jupyter",
"--unstable",
"--kernel",
"--conn",
"path/to/conn/file"
@ -8474,7 +8490,6 @@ mod tests {
kernel: true,
conn_file: Some(PathBuf::from("path/to/conn/file")),
}),
unstable: true,
..Flags::default()
}
);

View file

@ -1409,12 +1409,12 @@ impl CliOptions {
&self.flags.unsafely_ignore_certificate_errors
}
pub fn unstable(&self) -> bool {
self.flags.unstable
pub fn legacy_unstable_flag(&self) -> bool {
self.flags.unstable_config.legacy_flag_enabled
}
pub fn unstable_bare_node_builtins(&self) -> bool {
self.flags.unstable_bare_node_builtins
self.flags.unstable_config.bare_node_builtins
|| self
.maybe_config_file()
.as_ref()
@ -1423,7 +1423,7 @@ impl CliOptions {
}
pub fn unstable_byonm(&self) -> bool {
self.flags.unstable_byonm
self.flags.unstable_config.byonm
|| NPM_PROCESS_STATE
.as_ref()
.map(|s| matches!(s.kind, NpmProcessStateKind::Byonm))
@ -1436,7 +1436,7 @@ impl CliOptions {
}
pub fn unstable_sloppy_imports(&self) -> bool {
self.flags.unstable_sloppy_imports
self.flags.unstable_config.sloppy_imports
|| self
.maybe_config_file()
.as_ref()
@ -1451,7 +1451,7 @@ impl CliOptions {
.map(|c| c.json.unstable.clone())
.unwrap_or_default();
from_config_file.extend_from_slice(&self.flags.unstable_features);
from_config_file.extend_from_slice(&self.flags.unstable_config.features);
from_config_file
}

View file

@ -603,7 +603,7 @@ impl CliFactory {
// TODO(bartlomieju): enable, once we deprecate `--unstable` in favor
// of granular --unstable-* flags.
// feature_checker.set_warn_cb(Box::new(crate::unstable_warn_cb));
if self.options.unstable() {
if self.options.legacy_unstable_flag() {
checker.enable_legacy_unstable();
}
let unstable_features = self.options.unstable_features();
@ -709,7 +709,7 @@ impl CliFactory {
.options
.unsafely_ignore_certificate_errors()
.clone(),
unstable: self.options.unstable(),
unstable: self.options.legacy_unstable_flag(),
maybe_root_package_json_deps: self.options.maybe_package_json_deps(),
})
}

View file

@ -401,8 +401,11 @@ pub fn main() {
// https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214
DenoSubcommand::Lsp => vec!["--max-old-space-size=3072".to_string()],
_ => {
if flags.unstable
|| flags.unstable_features.contains(&"temporal".to_string())
if flags.unstable_config.legacy_flag_enabled
|| flags
.unstable_config
.features
.contains(&"temporal".to_string())
{
vec!["--harmony-temporal".to_string()]
} else {

View file

@ -33,6 +33,7 @@ use crate::args::CaData;
use crate::args::CliOptions;
use crate::args::CompileFlags;
use crate::args::PackageJsonDepsProvider;
use crate::args::UnstableConfig;
use crate::cache::DenoDir;
use crate::file_fetcher::FileFetcher;
use crate::http_util::HttpClient;
@ -137,8 +138,6 @@ pub enum NodeModules {
#[derive(Deserialize, Serialize)]
pub struct Metadata {
pub argv: Vec<String>,
pub unstable: bool,
pub unstable_features: Vec<String>,
pub seed: Option<u64>,
pub permissions: PermissionsOptions,
pub location: Option<Url>,
@ -151,6 +150,7 @@ pub struct Metadata {
pub entrypoint: ModuleSpecifier,
pub node_modules: Option<NodeModules>,
pub disable_deprecated_api_warning: bool,
pub unstable_config: UnstableConfig,
}
pub fn load_npm_vfs(root_dir_path: PathBuf) -> Result<FileBackedVfs, AnyError> {
@ -543,8 +543,6 @@ impl<'a> DenoCompileBinaryWriter<'a> {
let metadata = Metadata {
argv: compile_flags.args.clone(),
unstable: cli_options.unstable(),
unstable_features: cli_options.unstable_features(),
seed: cli_options.seed(),
location: cli_options.location_flag().clone(),
permissions: cli_options.permissions_options(),
@ -560,6 +558,13 @@ impl<'a> DenoCompileBinaryWriter<'a> {
node_modules,
disable_deprecated_api_warning: cli_options
.disable_deprecated_api_warning,
unstable_config: UnstableConfig {
legacy_flag_enabled: cli_options.legacy_unstable_flag(),
bare_node_builtins: cli_options.unstable_bare_node_builtins(),
byonm: cli_options.unstable_byonm(),
sloppy_imports: cli_options.unstable_sloppy_imports(),
features: cli_options.unstable_features(),
},
};
write_binary_bytes(

View file

@ -487,10 +487,10 @@ pub async fn run(
// TODO(bartlomieju): enable, once we deprecate `--unstable` in favor
// of granular --unstable-* flags.
// feature_checker.set_warn_cb(Box::new(crate::unstable_warn_cb));
if metadata.unstable {
if metadata.unstable_config.legacy_flag_enabled {
checker.enable_legacy_unstable();
}
for feature in metadata.unstable_features {
for feature in metadata.unstable_config.features {
// `metadata` is valid for the whole lifetime of the program, so we
// can leak the string here.
checker.enable_feature(feature.leak());
@ -535,7 +535,7 @@ pub async fn run(
seed: metadata.seed,
unsafely_ignore_certificate_errors: metadata
.unsafely_ignore_certificate_errors,
unstable: metadata.unstable,
unstable: metadata.unstable_config.legacy_flag_enabled,
maybe_root_package_json_deps: package_json_deps_provider.deps().cloned(),
},
None,

View file

@ -378,7 +378,7 @@ async fn resolve_shim_data(
TypeCheckMode::Local => executable_args.push("--check".to_string()),
}
if flags.unstable {
if flags.unstable_config.legacy_flag_enabled {
executable_args.push("--unstable".to_string());
}
@ -499,6 +499,7 @@ fn is_in_path(dir: &Path) -> bool {
mod tests {
use super::*;
use crate::args::UnstableConfig;
use crate::util::fs::canonicalize_path;
use deno_config::ConfigFlag;
use std::process::Command;
@ -647,7 +648,10 @@ mod tests {
create_install_shim(
Flags {
unstable: true,
unstable_config: UnstableConfig {
legacy_flag_enabled: true,
..Default::default()
},
..Flags::default()
},
InstallFlags {