Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 34 additions & 0 deletions packages/cli/binding/src/cli/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ fn has_flag_before_terminator(args: &[String], flag: &str) -> bool {
false
}

pub(super) fn has_help_flag_before_terminator(args: &[String]) -> bool {
args.iter()
.take_while(|arg| arg.as_str() != "--")
.any(|arg| matches!(arg.as_str(), "-h" | "--help"))
}

pub(super) fn should_suppress_subcommand_stdout(subcommand: &SynthesizableSubcommand) -> bool {
match subcommand {
SynthesizableSubcommand::Lint { args } => has_flag_before_terminator(args, "--init"),
Expand All @@ -67,6 +73,15 @@ pub(super) fn should_suppress_subcommand_stdout(subcommand: &SynthesizableSubcom
}
}

pub(super) fn should_capture_help_stdout(subcommand: &SynthesizableSubcommand) -> bool {
let args = match subcommand {
SynthesizableSubcommand::Lint { args } | SynthesizableSubcommand::Fmt { args } => args,
_ => return false,
};

has_help_flag_before_terminator(args)
}

pub(super) fn should_prepend_vitest_run(args: &[String]) -> bool {
let Some(first_arg) = args.first().map(String::as_str) else {
return true;
Expand Down Expand Up @@ -295,6 +310,25 @@ mod tests {
assert!(!should_suppress_subcommand_stdout(&subcommand));
}

#[test]
fn lint_help_captures_stdout() {
let subcommand = SynthesizableSubcommand::Lint { args: vec!["--help".to_string()] };
assert!(should_capture_help_stdout(&subcommand));
}

#[test]
fn fmt_short_help_captures_stdout() {
let subcommand = SynthesizableSubcommand::Fmt { args: vec!["-h".to_string()] };
assert!(should_capture_help_stdout(&subcommand));
}

#[test]
fn help_after_terminator_is_not_captured() {
let subcommand =
SynthesizableSubcommand::Lint { args: vec!["--".to_string(), "--help".to_string()] };
assert!(!should_capture_help_stdout(&subcommand));
}

#[test]
fn global_subcommands_produce_invalid_subcommand_error() {
use clap::error::ErrorKind;
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/binding/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use self::{
execution::{FilterStream, resolve_and_execute, resolve_and_execute_with_filter},
handler::{VitePlusCommandHandler, VitePlusConfigLoader},
help::{
handle_cli_parse_error, normalize_help_args, print_help, should_print_help,
should_suppress_subcommand_stdout,
handle_cli_parse_error, normalize_help_args, print_help, should_capture_help_stdout,
should_print_help, should_suppress_subcommand_stdout,
},
types::CLIArgs,
};
Expand Down Expand Up @@ -96,6 +96,18 @@ async fn execute_direct_subcommand(
|_| Cow::Borrowed(""),
)
.await?
} else if should_capture_help_stdout(&other) {
resolve_and_execute_with_filter(
&resolver,
other,
None,
&envs,
cwd,
&cwd_arc,
FilterStream::Stdout,
|s| Cow::Borrowed(s),
)
.await?
} else if matches!(&other, SynthesizableSubcommand::Fmt { .. }) {
resolve_and_execute_with_filter(
&resolver,
Expand Down
28 changes: 17 additions & 11 deletions packages/cli/binding/src/cli/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use vite_task::config::user::{
};

use super::{
help::should_prepend_vitest_run,
help::{has_help_flag_before_terminator, should_prepend_vitest_run},
types::{CliOptions, ResolvedSubcommand, ResolvedUniversalViteConfig, SynthesizableSubcommand},
};

Expand Down Expand Up @@ -94,15 +94,18 @@ impl SubcommandResolver {
.to_str()
.ok_or_else(|| anyhow::anyhow!("lint JS path is not valid UTF-8"))?;
let owned_resolved_vite_config;
let resolved_vite_config = if let Some(config) = resolved_vite_config {
config
let resolved_vite_config = if has_help_flag_before_terminator(&args) {
None
} else if let Some(config) = resolved_vite_config {
Some(config)
} else {
owned_resolved_vite_config = self.resolve_universal_vite_config().await?;
&owned_resolved_vite_config
Some(&owned_resolved_vite_config)
};

if let (Some(_), Some(config_file)) =
(&resolved_vite_config.lint, &resolved_vite_config.config_file)
if let Some(resolved_vite_config) = resolved_vite_config
&& let (Some(_), Some(config_file)) =
(&resolved_vite_config.lint, &resolved_vite_config.config_file)
{
args.insert(0, "-c".to_string());
args.insert(1, config_file.clone());
Expand Down Expand Up @@ -130,15 +133,18 @@ impl SubcommandResolver {
.to_str()
.ok_or_else(|| anyhow::anyhow!("fmt JS path is not valid UTF-8"))?;
let owned_resolved_vite_config;
let resolved_vite_config = if let Some(config) = resolved_vite_config {
config
let resolved_vite_config = if has_help_flag_before_terminator(&args) {
None
} else if let Some(config) = resolved_vite_config {
Some(config)
} else {
owned_resolved_vite_config = self.resolve_universal_vite_config().await?;
&owned_resolved_vite_config
Some(&owned_resolved_vite_config)
};

if let (Some(_), Some(config_file)) =
(&resolved_vite_config.fmt, &resolved_vite_config.config_file)
if let Some(resolved_vite_config) = resolved_vite_config
&& let (Some(_), Some(config_file)) =
(&resolved_vite_config.fmt, &resolved_vite_config.config_file)
{
args.insert(0, "-c".to_string());
args.insert(1, config_file.clone());
Expand Down
27 changes: 0 additions & 27 deletions packages/cli/snap-tests-todo/test-panicked-fix/snap.txt

This file was deleted.

21 changes: 21 additions & 0 deletions packages/cli/snap-tests/test-panicked-fix/snap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
> vp lint --help | head -n 20 # print help message and no panicked
Usage: [-c=<./.oxlintrc.json>] [PATH]...

Basic Configuration
-c, --config=<./.oxlintrc.json> Oxlint configuration file
* `.json` and `.jsonc` config files are supported in all runtimes
* JavaScript/TypeScript config files are experimental and require
running via Node.js
* you can use comments in configuration files.
* tries to be compatible with ESLint v8's format
--tsconfig=<./tsconfig.json> Override the TypeScript config used for import resolution.
Oxlint automatically discovers the relevant `tsconfig.json` for each
file. Use this only when your project uses a non-standard tsconfig
name or location.
--init Initialize oxlint configuration with default values

Allowing / Denying Multiple Lints
Accumulate rules and categories from left to right on the command-line.
For example `-D correctness -A no-debugger` or `-A all -D no-debugger`.
The categories are:
* `correctness` - Code that is outright wrong or useless (default)
Loading