summaryrefslogtreecommitdiff
path: root/cli/args/flags.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-10-14 20:48:39 -0400
committerGitHub <noreply@github.com>2024-10-14 20:48:39 -0400
commit1a0cb5b5312941521ab021cfe9eaed498f35900b (patch)
tree2e5c58e25e8506b993ac678e83ba0c2feac37d75 /cli/args/flags.rs
parentee7d4501435f0ebd655c8b50bd6e41ca19e71abc (diff)
feat(unstable): `--unstable-detect-cjs` for respecting explicit `"type": "commonjs"` (#26149)
When using the `--unstable-detect-cjs` flag or adding `"unstable": ["detect-cjs"]` to a deno.json, it will make a JS file CJS if the closest package.json contains `"type": "commonjs"` and the file is not an ESM module (no TLA, no `import.meta`, no `import`/`export`).
Diffstat (limited to 'cli/args/flags.rs')
-rw-r--r--cli/args/flags.rs115
1 files changed, 69 insertions, 46 deletions
diff --git a/cli/args/flags.rs b/cli/args/flags.rs
index d59e5ac1a..acaf74a67 100644
--- a/cli/args/flags.rs
+++ b/cli/args/flags.rs
@@ -575,7 +575,8 @@ fn parse_packages_allowed_scripts(s: &str) -> Result<String, AnyError> {
pub struct UnstableConfig {
// TODO(bartlomieju): remove in Deno 2.5
pub legacy_flag_enabled: bool, // --unstable
- pub bare_node_builtins: bool, // --unstable-bare-node-builts
+ pub bare_node_builtins: bool,
+ pub detect_cjs: bool,
pub sloppy_imports: bool,
pub features: Vec<String>, // --unstabe-kv --unstable-cron
}
@@ -1528,7 +1529,7 @@ pub fn clap_root() -> Command {
);
run_args(Command::new("deno"), true)
- .args(unstable_args(UnstableArgsConfig::ResolutionAndRuntime))
+ .with_unstable_args(UnstableArgsConfig::ResolutionAndRuntime)
.next_line_help(false)
.bin_name("deno")
.styles(
@@ -1630,7 +1631,7 @@ fn command(
) -> Command {
Command::new(name)
.about(about)
- .args(unstable_args(unstable_args_config))
+ .with_unstable_args(unstable_args_config)
}
fn help_subcommand(app: &Command) -> Command {
@@ -4142,23 +4143,29 @@ enum UnstableArgsConfig {
ResolutionAndRuntime,
}
-struct UnstableArgsIter {
- idx: usize,
- cfg: UnstableArgsConfig,
+trait CommandExt {
+ fn with_unstable_args(self, cfg: UnstableArgsConfig) -> Self;
}
-impl Iterator for UnstableArgsIter {
- type Item = Arg;
+impl CommandExt for Command {
+ fn with_unstable_args(self, cfg: UnstableArgsConfig) -> Self {
+ let mut next_display_order = {
+ let mut value = 1000;
+ move || {
+ value += 1;
+ value
+ }
+ };
- fn next(&mut self) -> Option<Self::Item> {
- let arg = if self.idx == 0 {
+ let mut cmd = self.arg(
Arg::new("unstable")
- .long("unstable")
- .help(cstr!("Enable all unstable features and APIs. Instead of using this flag, consider enabling individual unstable features
+ .long("unstable")
+ .help(cstr!("Enable all unstable features and APIs. Instead of using this flag, consider enabling individual unstable features
<p(245)>To view the list of individual unstable feature flags, run this command again with --help=unstable</>"))
- .action(ArgAction::SetTrue)
- .hide(matches!(self.cfg, UnstableArgsConfig::None))
- } else if self.idx == 1 {
+ .action(ArgAction::SetTrue)
+ .hide(matches!(cfg, UnstableArgsConfig::None))
+ .display_order(next_display_order())
+ ).arg(
Arg::new("unstable-bare-node-builtins")
.long("unstable-bare-node-builtins")
.help("Enable unstable bare node builtins feature")
@@ -4166,20 +4173,36 @@ impl Iterator for UnstableArgsIter {
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
- .long_help(match self.cfg {
+ .long_help(match cfg {
+ UnstableArgsConfig::None => None,
+ UnstableArgsConfig::ResolutionOnly
+ | UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
+ })
+ .help_heading(UNSTABLE_HEADING)
+ .display_order(next_display_order()),
+ ).arg(
+ Arg::new("unstable-detect-cjs")
+ .long("unstable-detect-cjs")
+ .help("Reads the package.json type field in a project to treat .js files as .cjs")
+ .value_parser(FalseyValueParser::new())
+ .action(ArgAction::SetTrue)
+ .hide(true)
+ .long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly
| UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
})
.help_heading(UNSTABLE_HEADING)
- } else if self.idx == 2 {
+ .display_order(next_display_order())
+ ).arg(
Arg::new("unstable-byonm")
.long("unstable-byonm")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
.help_heading(UNSTABLE_HEADING)
- } else if self.idx == 3 {
+ .display_order(next_display_order()),
+ ).arg(
Arg::new("unstable-sloppy-imports")
.long("unstable-sloppy-imports")
.help("Enable unstable resolving of specifiers by extension probing, .js to .ts, and directory probing")
@@ -4187,40 +4210,39 @@ impl Iterator for UnstableArgsIter {
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue)
.hide(true)
- .long_help(match self.cfg {
+ .long_help(match cfg {
UnstableArgsConfig::None => None,
UnstableArgsConfig::ResolutionOnly | UnstableArgsConfig::ResolutionAndRuntime => Some("true")
})
.help_heading(UNSTABLE_HEADING)
- } else if self.idx > 3 {
- let granular_flag = crate::UNSTABLE_GRANULAR_FLAGS.get(self.idx - 4)?;
- Arg::new(format!("unstable-{}", granular_flag.name))
- .long(format!("unstable-{}", granular_flag.name))
- .help(granular_flag.help_text)
- .action(ArgAction::SetTrue)
- .hide(true)
- .help_heading(UNSTABLE_HEADING)
- // we don't render long help, so using it here as a sort of metadata
- .long_help(if granular_flag.show_in_help {
- match self.cfg {
- UnstableArgsConfig::None | UnstableArgsConfig::ResolutionOnly => {
- None
+ .display_order(next_display_order())
+ );
+
+ for granular_flag in crate::UNSTABLE_GRANULAR_FLAGS.iter() {
+ cmd = cmd.arg(
+ Arg::new(format!("unstable-{}", granular_flag.name))
+ .long(format!("unstable-{}", granular_flag.name))
+ .help(granular_flag.help_text)
+ .action(ArgAction::SetTrue)
+ .hide(true)
+ .help_heading(UNSTABLE_HEADING)
+ // we don't render long help, so using it here as a sort of metadata
+ .long_help(if granular_flag.show_in_help {
+ match cfg {
+ UnstableArgsConfig::None | UnstableArgsConfig::ResolutionOnly => {
+ None
+ }
+ UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
}
- UnstableArgsConfig::ResolutionAndRuntime => Some("true"),
- }
- } else {
- None
- })
- } else {
- return None;
- };
- self.idx += 1;
- Some(arg.display_order(self.idx + 1000))
- }
-}
+ } else {
+ None
+ })
+ .display_order(next_display_order()),
+ );
+ }
-fn unstable_args(cfg: UnstableArgsConfig) -> impl IntoIterator<Item = Arg> {
- UnstableArgsIter { idx: 0, cfg }
+ cmd
+ }
}
fn allow_scripts_arg_parse(
@@ -5678,6 +5700,7 @@ fn unstable_args_parse(
flags.unstable_config.bare_node_builtins =
matches.get_flag("unstable-bare-node-builtins");
+ flags.unstable_config.detect_cjs = matches.get_flag("unstable-detect-cjs");
flags.unstable_config.sloppy_imports =
matches.get_flag("unstable-sloppy-imports");