Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge branch 'main' into fix/useCamelCase-false-positive
Browse files Browse the repository at this point in the history
  • Loading branch information
nissy-dev committed May 2, 2023
2 parents 9938477 + 2cb6b01 commit 23b9066
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 41 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
#### Other changes

- Refactored the underling argument parsing logic. Changed the look and feel of the help
output. [#4405](https://github.com/rome/tools/pull/4405)
output. [#4405](https://github.com/rome/tools/pull/4405).
- The command `rome check` can accept input from `stdin`.
- Add the argument `--stdin-file-path` to use when running `rome check` via `stdin`.
- Add the argument `--formatter-enabled` to control the formatter via CLI.
- Add the argument `--linter-enabled` to control the linter via CLI.
- Add the argument `--organize-imports-enabled` to control the import sorting via CLI.

### Linter

Expand Down
55 changes: 54 additions & 1 deletion crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ use crate::vcs::store_path_to_ignore_from_vcs;
use crate::{execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode};
use rome_console::{markup, ConsoleExt};
use rome_diagnostics::{DiagnosticExt, PrintDiagnostic, Severity};
use rome_service::configuration::organize_imports::OrganizeImports;
use rome_service::configuration::{FormatterConfiguration, LinterConfiguration};
use rome_service::workspace::{FixFileMode, UpdateSettingsParams};
use rome_service::{Configuration, MergeWith};
use std::ffi::OsString;
use std::path::PathBuf;

pub(crate) struct CheckCommandPayload {
pub(crate) apply: bool,
pub(crate) apply_unsafe: bool,
pub(crate) cli_options: CliOptions,
pub(crate) configuration: Option<Configuration>,
pub(crate) paths: Vec<OsString>,
pub(crate) stdin_file_path: Option<String>,
pub(crate) formatter_enabled: Option<bool>,
pub(crate) linter_enabled: Option<bool>,
pub(crate) organize_imports_enabled: Option<bool>,
}

/// Handler for the "check" command of the Rome CLI
Expand All @@ -27,6 +34,10 @@ pub(crate) fn check(
cli_options,
configuration,
paths,
stdin_file_path,
linter_enabled,
organize_imports_enabled,
formatter_enabled,
} = payload;

let fix_file_mode = if apply && apply_unsafe {
Expand Down Expand Up @@ -56,6 +67,31 @@ pub(crate) fn check(
})
}
}

let formatter = fs_configuration
.formatter
.get_or_insert_with(FormatterConfiguration::default);

if !matches!(formatter_enabled, None) {
formatter.enabled = formatter_enabled;
}

let linter = fs_configuration
.linter
.get_or_insert_with(LinterConfiguration::default);

if !matches!(linter_enabled, None) {
linter.enabled = linter_enabled;
}

let organize_imports = fs_configuration
.organize_imports
.get_or_insert_with(OrganizeImports::default);

if !matches!(organize_imports_enabled, None) {
organize_imports.enabled = organize_imports_enabled;
}

fs_configuration.merge_with(configuration);

// check if support of git ignore files is enabled
Expand All @@ -67,6 +103,20 @@ pub(crate) fn check(
&cli_options,
)?;

let stdin = if let Some(stdin_file_path) = stdin_file_path {
let console = &mut session.app.console;
let input_code = console.read();
if let Some(input_code) = input_code {
let path = PathBuf::from(stdin_file_path);
Some((path, input_code))
} else {
// we provided the argument without a piped stdin, we bail
return Err(CliDiagnostic::missing_argument("stdin", "check"));
}
} else {
None
};

session
.app
.workspace
Expand All @@ -75,7 +125,10 @@ pub(crate) fn check(
})?;

execute_mode(
Execution::new(TraversalMode::Check { fix_file_mode }),
Execution::new(TraversalMode::Check {
fix_file_mode,
stdin,
}),
session,
&cli_options,
paths,
Expand Down
22 changes: 22 additions & 0 deletions crates/rome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,32 @@ pub enum RomeCommand {
/// Apply safe fixes and unsafe fixes, formatting and import sorting
#[bpaf(long("apply-unsafe"), switch)]
apply_unsafe: bool,
/// Allow to enable or disable the formatter check.
#[bpaf(
long("formatter-enabled"),
argument("true|false"),
optional,
hide_usage
)]
formatter_enabled: Option<bool>,
/// Allow to enable or disable the linter check.
#[bpaf(long("linter-enabled"), argument("true|false"), optional, hide_usage)]
linter_enabled: Option<bool>,
/// Allow to enable or disable the organize imports.
#[bpaf(
long("organize-imports-enabled"),
argument("true|false"),
optional,
hide_usage
)]
organize_imports_enabled: Option<bool>,
#[bpaf(external, hide_usage, optional)]
configuration: Option<Configuration>,
#[bpaf(external, hide_usage)]
cli_options: CliOptions,
/// A file name with its extension to pass when reading from standard in, e.g. echo 'let a;' | rome check --stdin-file-path=file.js"
#[bpaf(long("stdin-file-path"), argument("PATH"), hide_usage)]
stdin_file_path: Option<String>,
/// Single file, single path or list of paths
#[bpaf(positional("PATH"), many)]
paths: Vec<OsString>,
Expand Down
13 changes: 10 additions & 3 deletions crates/rome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub(crate) enum TraversalMode {
/// It's [None] if the `check` command is called without `--apply` or `--apply-suggested`
/// arguments.
fix_file_mode: Option<FixFileMode>,
/// An optional tuple.
/// 1. The virtual path to the file
/// 2. The content of the file
stdin: Option<(PathBuf, String)>,
},
/// This mode is enabled when running the command `rome ci`
CI,
Expand Down Expand Up @@ -125,7 +129,8 @@ impl Execution {
matches!(
self.traversal_mode,
TraversalMode::Check {
fix_file_mode: Some(FixFileMode::SafeFixes)
fix_file_mode: Some(FixFileMode::SafeFixes),
..
}
)
}
Expand All @@ -134,7 +139,8 @@ impl Execution {
matches!(
self.traversal_mode,
TraversalMode::Check {
fix_file_mode: Some(FixFileMode::SafeAndUnsafeFixes)
fix_file_mode: Some(FixFileMode::SafeAndUnsafeFixes),
..
}
)
}
Expand All @@ -146,7 +152,7 @@ impl Execution {
/// Whether the traversal mode requires write access to files
pub(crate) const fn requires_write_access(&self) -> bool {
match self.traversal_mode {
TraversalMode::Check { fix_file_mode } => fix_file_mode.is_some(),
TraversalMode::Check { fix_file_mode, .. } => fix_file_mode.is_some(),
TraversalMode::CI => false,
TraversalMode::Format { write, .. } => write,
TraversalMode::Migrate { write: dry_run, .. } => dry_run,
Expand All @@ -156,6 +162,7 @@ impl Execution {
pub(crate) fn as_stdin_file(&self) -> Option<&(PathBuf, String)> {
match &self.traversal_mode {
TraversalMode::Format { stdin, .. } => stdin.as_ref(),
TraversalMode::Check { stdin, .. } => stdin.as_ref(),
_ => None,
}
}
Expand Down
52 changes: 24 additions & 28 deletions crates/rome_cli/src/execute/process_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,21 @@ pub(crate) fn process_file(ctx: &TraversalOptions, path: &Path) -> FileResult {
category!("organizeImports"),
)?;

if let Some(output) = sorted.code {
if output != input {
if ctx.execution.is_check_apply_unsafe() {
file.set_content(output.as_bytes())
.with_file_path(path.display().to_string())?;
file_guard.change_file(file.file_version(), output)?;
} else {
errors += 1;
ctx.messages
.send(Message::Diff {
file_name: path.display().to_string(),
old: input.clone(),
new: output,
diff_kind: DiffKind::OrganizeImports,
})
.ok();
}
if sorted.code != input {
if ctx.execution.is_check_apply_unsafe() {
file.set_content(sorted.code.as_bytes())
.with_file_path(path.display().to_string())?;
file_guard.change_file(file.file_version(), sorted.code)?;
} else {
errors += 1;
ctx.messages
.send(Message::Diff {
file_name: path.display().to_string(),
old: input.clone(),
new: sorted.code,
diff_kind: DiffKind::OrganizeImports,
})
.ok();
}
}
}
Expand Down Expand Up @@ -258,17 +256,15 @@ pub(crate) fn process_file(ctx: &TraversalOptions, path: &Path) -> FileResult {
category!("organizeImports"),
)?;

if let Some(output) = sorted.code {
if output != input {
ctx.messages
.send(Message::Diff {
file_name: path.display().to_string(),
old: input.clone(),
new: output,
diff_kind: DiffKind::OrganizeImports,
})
.ok();
}
if sorted.code != input {
ctx.messages
.send(Message::Diff {
file_name: path.display().to_string(),
old: input.clone(),
new: sorted.code,
diff_kind: DiffKind::OrganizeImports,
})
.ok();
}
}

Expand Down
90 changes: 89 additions & 1 deletion crates/rome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::{CliDiagnostic, CliSession};
use rome_console::{markup, ConsoleExt};
use rome_fs::RomePath;
use rome_service::workspace::{
FeatureName, FormatFileParams, Language, OpenFileParams, SupportsFeatureParams,
ChangeFileParams, FeatureName, FixFileParams, FormatFileParams, Language, OpenFileParams,
OrganizeImportsParams, SupportsFeatureParams,
};
use std::borrow::Cow;

pub(crate) fn run<'a>(
session: CliSession,
Expand All @@ -16,6 +18,7 @@ pub(crate) fn run<'a>(
) -> Result<(), CliDiagnostic> {
let workspace = &*session.app.workspace;
let console = &mut *session.app.console;
let mut version = 0;

if mode.is_format() {
let unsupported_format_reason = workspace
Expand Down Expand Up @@ -44,6 +47,91 @@ pub(crate) fn run<'a>(
<Warn>"The content was not formatted because the formatter is currently disabled."</Warn>
})
}
} else if mode.is_check() {
let mut new_content = Cow::Borrowed(content);

workspace.open_file(OpenFileParams {
path: rome_path.clone(),
version: 0,
content: content.into(),
language_hint: Language::default(),
})?;

if let Some(fix_file_mode) = mode.as_fix_file_mode() {
// apply fix file of the linter
let unsupported_lint_reason = workspace
.supports_feature(SupportsFeatureParams {
path: rome_path.clone(),
feature: FeatureName::Lint,
})?
.reason;

if unsupported_lint_reason.is_none() {
let fix_file_result = workspace.fix_file(FixFileParams {
fix_file_mode: *fix_file_mode,
path: rome_path.clone(),
})?;
if fix_file_result.code != new_content {
version += 1;
workspace.change_file(ChangeFileParams {
content: fix_file_result.code.clone(),
path: rome_path.clone(),
version,
})?;
new_content = Cow::Owned(fix_file_result.code);
}
}

// apply organize imports
let unsupported_organize_imports_reason = workspace
.supports_feature(SupportsFeatureParams {
path: rome_path.clone(),
feature: FeatureName::OrganizeImports,
})?
.reason;

if unsupported_organize_imports_reason.is_none() {
let result = workspace.organize_imports(OrganizeImportsParams {
path: rome_path.clone(),
})?;
if result.code != new_content {
version += 1;
workspace.change_file(ChangeFileParams {
content: result.code.clone(),
path: rome_path.clone(),
version,
})?;
new_content = Cow::Owned(result.code);
}
}
}

let unsupported_format_reason = workspace
.supports_feature(SupportsFeatureParams {
path: rome_path.clone(),
feature: FeatureName::Format,
})?
.reason;

if unsupported_format_reason.is_none() {
let printed = workspace.format_file(FormatFileParams { path: rome_path })?;
if printed.as_code() != new_content {
new_content = Cow::Owned(printed.into_code());
}
}

match new_content {
Cow::Borrowed(original) => {
console.append(markup! {
{original}
});
}
Cow::Owned(new_content) => {
console.append(markup! {
{new_content}
});
}
}
}
Ok(())
}
8 changes: 8 additions & 0 deletions crates/rome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ impl<'app> CliSession<'app> {
cli_options,
configuration: rome_configuration,
paths,
stdin_file_path,
linter_enabled,
organize_imports_enabled,
formatter_enabled,
} => commands::check::check(
self,
CheckCommandPayload {
Expand All @@ -88,6 +92,10 @@ impl<'app> CliSession<'app> {
cli_options,
configuration: rome_configuration,
paths,
stdin_file_path,
linter_enabled,
organize_imports_enabled,
formatter_enabled,
},
),
RomeCommand::Ci {
Expand Down
Loading

0 comments on commit 23b9066

Please sign in to comment.