diff --git a/.gitignore b/.gitignore index 15a86b6aa4e..5fdead1c773 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ Profile-*.json # Ignore third-party files **/node_modules **/dist +**/build npm/cli-* .pnpm-debug.log diff --git a/Cargo.lock b/Cargo.lock index a113a247030..3c468fb1d54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1654,6 +1654,7 @@ dependencies = [ "crossbeam", "dashmap", "hdrhistogram", + "indexmap", "insta", "lazy_static", "libc", @@ -1828,6 +1829,7 @@ dependencies = [ "indexmap", "parking_lot", "rayon", + "rome_console", "rome_diagnostics", "schemars", "serde", diff --git a/crates/rome_cli/Cargo.toml b/crates/rome_cli/Cargo.toml index 8130a884ddc..841c81c52e8 100644 --- a/crates/rome_cli/Cargo.toml +++ b/crates/rome_cli/Cargo.toml @@ -41,6 +41,7 @@ rome_json_formatter = { path = "../rome_json_formatter" } rome_json_syntax = { path = "../rome_json_syntax" } rome_migrate = { path = "../rome_migrate" } rome_rowan = { path = "../rome_rowan" } +indexmap = { workspace = true } [target.'cfg(unix)'.dependencies] libc = "0.2.127" diff --git a/crates/rome_cli/src/commands/check.rs b/crates/rome_cli/src/commands/check.rs index 01cfa41205b..7d89826082f 100644 --- a/crates/rome_cli/src/commands/check.rs +++ b/crates/rome_cli/src/commands/check.rs @@ -1,5 +1,8 @@ use crate::configuration::load_configuration; -use crate::parse_arguments::{apply_files_settings_from_cli, apply_format_settings_from_cli}; +use crate::parse_arguments::{ + apply_files_settings_from_cli, apply_format_settings_from_cli, apply_vcs_settings_from_cli, +}; +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}; @@ -7,7 +10,8 @@ use rome_service::workspace::{FixFileMode, UpdateSettingsParams}; /// Handler for the "check" command of the Rome CLI pub(crate) fn check(mut session: CliSession) -> Result<(), CliDiagnostic> { - let (mut configuration, diagnostics, _) = load_configuration(&mut session)?.consume(); + let (mut configuration, diagnostics, configuration_path) = + load_configuration(&mut session)?.consume(); if !diagnostics.is_empty() { let console = &mut session.app.console; console.log(markup!{ @@ -22,6 +26,11 @@ pub(crate) fn check(mut session: CliSession) -> Result<(), CliDiagnostic> { } apply_files_settings_from_cli(&mut session, &mut configuration)?; apply_format_settings_from_cli(&mut session, &mut configuration)?; + apply_vcs_settings_from_cli(&mut session, &mut configuration)?; + + // check if support of git ignore files is enabled + let vcs_base_path = configuration_path.or(session.app.fs.working_directory()); + store_path_to_ignore_from_vcs(&mut session, &mut configuration, vcs_base_path)?; session .app diff --git a/crates/rome_cli/src/commands/ci.rs b/crates/rome_cli/src/commands/ci.rs index fcb4d9b5d66..010c72029c8 100644 --- a/crates/rome_cli/src/commands/ci.rs +++ b/crates/rome_cli/src/commands/ci.rs @@ -1,4 +1,7 @@ -use crate::parse_arguments::{apply_files_settings_from_cli, apply_format_settings_from_cli}; +use crate::parse_arguments::{ + apply_files_settings_from_cli, apply_format_settings_from_cli, apply_vcs_settings_from_cli, +}; +use crate::vcs::store_path_to_ignore_from_vcs; use crate::{ configuration::load_configuration, execute_mode, CliDiagnostic, CliSession, Execution, TraversalMode, @@ -11,7 +14,8 @@ use rome_service::workspace::UpdateSettingsParams; /// Handler for the "ci" command of the Rome CLI pub(crate) fn ci(mut session: CliSession) -> Result<(), CliDiagnostic> { - let (mut configuration, diagnostics, _) = load_configuration(&mut session)?.consume(); + let (mut configuration, diagnostics, configuration_path) = + load_configuration(&mut session)?.consume(); if !diagnostics.is_empty() { let console = &mut session.app.console; @@ -77,6 +81,12 @@ pub(crate) fn ci(mut session: CliSession) -> Result<(), CliDiagnostic> { apply_format_settings_from_cli(&mut session, &mut configuration)?; } + apply_vcs_settings_from_cli(&mut session, &mut configuration)?; + + // check if support of git ignore files is enabled + let vcs_base_path = configuration_path.or(session.app.fs.working_directory()); + store_path_to_ignore_from_vcs(&mut session, &mut configuration, vcs_base_path)?; + session .app .workspace diff --git a/crates/rome_cli/src/commands/format.rs b/crates/rome_cli/src/commands/format.rs index f467942d1bb..f8843bbce12 100644 --- a/crates/rome_cli/src/commands/format.rs +++ b/crates/rome_cli/src/commands/format.rs @@ -1,6 +1,9 @@ use crate::configuration::load_configuration; use crate::execute::ReportMode; -use crate::parse_arguments::{apply_files_settings_from_cli, apply_format_settings_from_cli}; +use crate::parse_arguments::{ + apply_files_settings_from_cli, apply_format_settings_from_cli, apply_vcs_settings_from_cli, +}; +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}; @@ -9,7 +12,8 @@ use std::path::PathBuf; /// Handler for the "format" command of the Rome CLI pub(crate) fn format(mut session: CliSession) -> Result<(), CliDiagnostic> { - let (mut configuration, diagnostics, _) = load_configuration(&mut session)?.consume(); + let (mut configuration, diagnostics, configuration_path) = + load_configuration(&mut session)?.consume(); if !diagnostics.is_empty() { let console = &mut session.app.console; console.log(markup!{ @@ -25,7 +29,11 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), CliDiagnostic> { apply_files_settings_from_cli(&mut session, &mut configuration)?; apply_format_settings_from_cli(&mut session, &mut configuration)?; + apply_vcs_settings_from_cli(&mut session, &mut configuration)?; + // check if support of git ignore files is enabled + let vcs_base_path = configuration_path.or(session.app.fs.working_directory()); + store_path_to_ignore_from_vcs(&mut session, &mut configuration, vcs_base_path)?; session .app .workspace diff --git a/crates/rome_cli/src/commands/help.rs b/crates/rome_cli/src/commands/help.rs index 55149021740..b66cbec457c 100644 --- a/crates/rome_cli/src/commands/help.rs +++ b/crates/rome_cli/src/commands/help.rs @@ -22,8 +22,8 @@ const MAIN: Markup = markup! { ""--colors="" Set the formatting mode for markup: \"off\" prints everything as plain text, \"force\" forces the formatting of markup using ANSI even if the console output is determined to be incompatible ""--use-server"" Connect to a running instance of the Rome daemon server ""--version"" Show the Rome version information and quit - ""--files-max-size"" The maximum allowed size for source code files in bytes (default: 1MB) -" + ""--files-max-size"" The maximum allowed size for source code files in bytes (default: 1MB)" +{VCS_OPTIONS} }; const CHECK: Markup = markup! { @@ -46,6 +46,7 @@ const CHECK: Markup = markup! { ""--config-path"" Set the filesystem path to the directory of the rome.json configuration file ""--verbose"" Print additional verbose advices on diagnostics " + {VCS_OPTIONS} }; const FORMAT_OPTIONS: Markup = markup! { @@ -60,6 +61,16 @@ const FORMAT_OPTIONS: Markup = markup! { " }; +const VCS_OPTIONS: Markup = markup! { + " + ""--vcs-enabled "" Whether Rome should integrate itself with the VCS client + ""--vcs-client-kind "" The name of the client + ""--vcs-use-ignore-file "" Whether Rome should ignore the paths inside the ignore file + ""--vcs-root "" Where Rome should look for VCS files. If a rome.json is present, Rome will append this root to the path of where +rome.json is. + " +}; + const CI: Markup = markup! { "Rome CI: Run the linter and formatter check on a set of files @@ -74,13 +85,14 @@ const CI: Markup = markup! { rome ci ./src ./internal ./scripts ""OPTIONS:"" - ""--formatter-enabled"" Allow to enable or disable the formatter check. (default: true) - ""--linter-enabled"" Allow to enable or disable the linter check. (default: true) - ""--organize-imports-enabled"" Allow to enable or disable the organize imports. (default: true) + ""--formatter-enabled "" Allow to enable or disable the formatter check. (default: true) + ""--linter-enabled "" Allow to enable or disable the linter check. (default: true) + ""--organize-imports-enabled "" Allow to enable or disable the organize imports. (default: true) ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50) ""--config-path"" Set the filesystem path to the directory of the rome.json configuration file ""--verbose"" Print additional verbose advices on diagnostics" {FORMAT_OPTIONS} + {VCS_OPTIONS} }; const FORMAT: Markup = markup! { @@ -101,10 +113,10 @@ const FORMAT: Markup = markup! { ""--skip-errors"" Skip over files containing syntax errors instead of emitting an error diagnostic. ""--max-diagnostics"" Cap the amount of diagnostics displayed (default: 50) ""--config-path"" Set the filesystem path to the directory of the rome.json configuration file - ""--verbose"" Print additional verbose advices on diagnostics" + ""--verbose"" Print additional verbose advices on diagnostics + ""--stdin-file-path "" A file name with its extension to pass when reading from standard in, e.g. echo 'let a;' | rome format --stdin-file-path file.js" {FORMAT_OPTIONS} - """--stdin-file-path "" A file name with its extension to pass when reading from standard in, e.g. echo 'let a;' | rome format --stdin-file-path file.js -" + {VCS_OPTIONS} }; const INIT: Markup = markup! { diff --git a/crates/rome_cli/src/commands/migrate.rs b/crates/rome_cli/src/commands/migrate.rs index df799dca695..6be64cc484d 100644 --- a/crates/rome_cli/src/commands/migrate.rs +++ b/crates/rome_cli/src/commands/migrate.rs @@ -6,11 +6,12 @@ use crate::{CliDiagnostic, CliSession}; /// Handler for the "check" command of the Rome CLI pub(crate) fn migrate(mut session: CliSession) -> Result<(), CliDiagnostic> { let (_, _, path) = load_configuration(&mut session)?.consume(); + let config_name = session.app.fs.config_name(); if let Some(path) = path { execute_mode( Execution::new(TraversalMode::Migrate { write: session.args.contains("--dry-run"), - configuration_path: path, + configuration_path: path.join(config_name), }), session, ) diff --git a/crates/rome_cli/src/commands/rage.rs b/crates/rome_cli/src/commands/rage.rs index 4bccfb0397b..ed980e33e09 100644 --- a/crates/rome_cli/src/commands/rage.rs +++ b/crates/rome_cli/src/commands/rage.rs @@ -187,6 +187,7 @@ impl Display for RageConfiguration<'_, '_> { {KeyValuePair("Formatter disabled", markup!({DebugDisplay(configuration.is_formatter_disabled())}))} {KeyValuePair("Linter disabled", markup!({DebugDisplay(configuration.is_linter_disabled())}))} {KeyValuePair("Organize imports disabled", markup!({DebugDisplay(configuration.is_organize_imports_disabled())}))} + {KeyValuePair("VCS disabled", markup!({DebugDisplay(configuration.is_vcs_disabled())}))} ).fmt(fmt)? } Err(err) => markup! ( diff --git a/crates/rome_cli/src/diagnostics.rs b/crates/rome_cli/src/diagnostics.rs index ae0a8a99587..19f1962661a 100644 --- a/crates/rome_cli/src/diagnostics.rs +++ b/crates/rome_cli/src/diagnostics.rs @@ -54,6 +54,8 @@ pub enum CliDiagnostic { NoFilesWereProcessed(NoFilesWereProcessed), /// Errors thrown when running the `rome migrate` command MigrateError(MigrationDiagnostic), + /// When the VCS folder couldn't be found + NoVcsFolderFound(NoVcsFolderFound), } #[derive(Debug, Diagnostic)] @@ -273,6 +275,31 @@ pub struct MigrationDiagnostic { pub reason: String, } +#[derive(Debug, Diagnostic)] +#[diagnostic( + category = "internalError/fs", + severity = Error, + message( + description = "Rome couldn't find the VCS folder at the following path: {path}", + message("Rome couldn't find the VCS folder at the following path: "{self.path}), + ) +)] +pub struct NoVcsFolderFound { + #[location(resource)] + pub path: String, + + #[source] + pub source: Option, +} + +#[derive(Debug, Diagnostic)] +#[diagnostic( + category = "internalError/fs", + severity = Warning, + message = "Rome couldn't determine a directory for the VCS integration. VCS integration will be disabled." +)] +pub struct DisabledVcs {} + /// Advices for the [CliDiagnostic] #[derive(Debug, Default)] struct CliAdvice { @@ -438,6 +465,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.category(), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.category(), CliDiagnostic::MigrateError(diagnostic) => diagnostic.category(), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.category(), } } @@ -459,6 +487,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.tags(), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.tags(), CliDiagnostic::MigrateError(diagnostic) => diagnostic.tags(), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.tags(), } } @@ -480,6 +509,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.severity(), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.severity(), CliDiagnostic::MigrateError(diagnostic) => diagnostic.severity(), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.severity(), } } @@ -501,6 +531,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.location(), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.location(), CliDiagnostic::MigrateError(diagnostic) => diagnostic.location(), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.location(), } } @@ -522,6 +553,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.message(fmt), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.message(fmt), CliDiagnostic::MigrateError(diagnostic) => diagnostic.message(fmt), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.message(fmt), } } @@ -543,6 +575,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.description(fmt), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.description(fmt), CliDiagnostic::MigrateError(diagnostic) => diagnostic.description(fmt), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.description(fmt), } } @@ -564,6 +597,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.advices(visitor), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.advices(visitor), CliDiagnostic::MigrateError(diagnostic) => diagnostic.advices(visitor), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.advices(visitor), } } @@ -589,6 +623,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.verbose_advices(visitor), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.verbose_advices(visitor), CliDiagnostic::MigrateError(diagnostic) => diagnostic.verbose_advices(visitor), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.verbose_advices(visitor), } } @@ -610,6 +645,7 @@ impl Diagnostic for CliDiagnostic { CliDiagnostic::NoFilesWereProcessed(diagnostic) => diagnostic.source(), CliDiagnostic::FileCheckApply(diagnostic) => diagnostic.source(), CliDiagnostic::MigrateError(diagnostic) => diagnostic.source(), + CliDiagnostic::NoVcsFolderFound(diagnostic) => diagnostic.source(), } } } diff --git a/crates/rome_cli/src/lib.rs b/crates/rome_cli/src/lib.rs index 506850d4544..9afbe98380e 100644 --- a/crates/rome_cli/src/lib.rs +++ b/crates/rome_cli/src/lib.rs @@ -22,6 +22,7 @@ mod panic; mod parse_arguments; mod reports; mod service; +mod vcs; pub use diagnostics::CliDiagnostic; pub(crate) use execute::{execute_mode, Execution, TraversalMode}; diff --git a/crates/rome_cli/src/parse_arguments.rs b/crates/rome_cli/src/parse_arguments.rs index 1e4b8ab9538..b16542592c8 100644 --- a/crates/rome_cli/src/parse_arguments.rs +++ b/crates/rome_cli/src/parse_arguments.rs @@ -1,5 +1,6 @@ use crate::{CliDiagnostic, CliSession}; use rome_formatter::IndentStyle; +use rome_service::configuration::vcs::{VcsClientKind, VcsConfiguration}; use rome_service::configuration::{ FormatterConfiguration, JavascriptConfiguration, JavascriptFormatter, PlainIndentStyle, }; @@ -107,3 +108,46 @@ pub(crate) fn apply_files_settings_from_cli( Ok(()) } + +pub(crate) fn apply_vcs_settings_from_cli( + session: &mut CliSession, + configuration: &mut Configuration, +) -> Result<(), CliDiagnostic> { + let vcs = configuration + .vcs + .get_or_insert_with(VcsConfiguration::default); + + let enabled = session + .args + .opt_value_from_str("--vcs-enabled") + .map_err(|source| CliDiagnostic::parse_error("--vcs-enabled", source))?; + let client_kind = session + .args + .opt_value_from_str("--vcs-client-kind") + .map_err(|source| CliDiagnostic::parse_error("--vcs-client-kind", source))?; + + let use_ignore_file = session + .args + .opt_value_from_str("--vcs-use-ignore-file") + .map_err(|source| CliDiagnostic::parse_error("--vcs-use-ignore-file", source))?; + let root = session + .args + .opt_value_from_str("--vcs-root") + .map_err(|source| CliDiagnostic::parse_error("--vcs-root", source))?; + + if let Some(enabled) = enabled { + vcs.enabled = enabled; + } + + match client_kind { + None => {} + Some(VcsClientKind::Git) => { + vcs.client_kind = Some(VcsClientKind::Git); + } + } + + vcs.use_ignore_file = use_ignore_file; + vcs.root = root; + + Ok(()) +} diff --git a/crates/rome_cli/src/vcs.rs b/crates/rome_cli/src/vcs.rs new file mode 100644 index 00000000000..32f46047d5c --- /dev/null +++ b/crates/rome_cli/src/vcs.rs @@ -0,0 +1,98 @@ +use crate::diagnostics::{DisabledVcs, NoVcsFolderFound}; +use crate::{CliDiagnostic, CliSession}; +use indexmap::IndexSet; +use rome_console::{markup, ConsoleExt}; +use rome_diagnostics::PrintDiagnostic; +use rome_service::configuration::vcs::{VcsClientKind, VcsConfiguration}; +use rome_service::configuration::FilesConfiguration; +use rome_service::{Configuration, WorkspaceError}; +use std::path::PathBuf; + +/// This function will check if the configuration is set to use the VCS integration and try to +/// read the ignored files. +pub(crate) fn store_path_to_ignore_from_vcs( + session: &mut CliSession, + configuration: &mut Configuration, + vcs_base_path: Option, +) -> Result<(), CliDiagnostic> { + let verbose = session.args.contains("--verbose"); + let Some(vcs) = &configuration.vcs else { + return Ok(()) + }; + if vcs.enabled { + let vcs_base_path = match (vcs_base_path, &vcs.root) { + (Some(vcs_base_path), Some(root)) => vcs_base_path.join(root), + (None, Some(root)) => PathBuf::from(root), + (Some(vcs_base_path), None) => vcs_base_path, + (None, None) => { + let console = &mut session.app.console; + let diagnostic = DisabledVcs {}; + console.error(markup! { + {if verbose { PrintDiagnostic::verbose(&diagnostic) } else { PrintDiagnostic::simple(&diagnostic) }} + }); + return Ok(()); + } + }; + + let files_to_ignore = read_vcs_ignore_file(session, vcs_base_path, vcs)?; + + if !files_to_ignore.is_empty() { + let files = configuration + .files + .get_or_insert_with(FilesConfiguration::default); + let ignored_files = files.ignore.get_or_insert_with(IndexSet::new); + ignored_files.extend(files_to_ignore.into_iter()); + } + } + Ok(()) +} + +pub(crate) fn read_vcs_ignore_file( + session: &mut CliSession, + current_directory: PathBuf, + configuration: &VcsConfiguration, +) -> Result, CliDiagnostic> { + if !configuration.enabled { + return Ok(vec![]); + } + let file_system = &session.app.fs; + + if let Some(client_kind) = &configuration.client_kind { + match client_kind { + VcsClientKind::Git => { + let git_folder = current_directory.join(".git"); + + if !file_system.path_exists(git_folder.as_path()) { + return Err(CliDiagnostic::NoVcsFolderFound(NoVcsFolderFound { + path: git_folder.display().to_string(), + source: None, + })); + } + } + } + if !configuration.ignore_file_disabled() { + let buffer = file_system + .auto_search(current_directory, client_kind.ignore_file(), false) + .map_err(WorkspaceError::from)?; + + if let Some((buffer, _)) = buffer { + return Ok(buffer + .split('\n') + // remove empty lines + .filter(|line| !line.is_empty()) + .filter_map(|item| { + let line = item.to_string(); + // remove comments + if !line.starts_with('#') { + Some(line) + } else { + None + } + }) + .collect::>()); + } + } + } + + Ok(vec![]) +} diff --git a/crates/rome_cli/tests/commands/check.rs b/crates/rome_cli/tests/commands/check.rs index 6d4112d350b..9a147c7873a 100644 --- a/crates/rome_cli/tests/commands/check.rs +++ b/crates/rome_cli/tests/commands/check.rs @@ -1777,3 +1777,113 @@ fn ignore_configured_globals() { result, )); } + +#[test] +fn ignore_vcs_ignored_file() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let rome_json = r#"{ + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + } + }"#; + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); bar.call();"#; + let code1 = r#"array.map(sentence => sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // configuration + let config_path = Path::new("rome.json"); + fs.insert(config_path.into(), rome_json.as_bytes()); + + // git folder + let git_folder = Path::new(".git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new(".gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("check"), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file", + fs, + console, + result, + )); +} + +#[test] +fn ignore_vcs_ignored_file_via_cli() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); bar.call();"#; + let code1 = r#"array.map(sentence => sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // git folder + let git_folder = Path::new("./.git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new("./.gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("check"), + OsString::from("--vcs-enabled=true"), + OsString::from("--vcs-client-kind=git"), + OsString::from("--vcs-use-ignore-file=true"), + OsString::from("--vcs-root=."), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file_via_cli", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/commands/ci.rs b/crates/rome_cli/tests/commands/ci.rs index de02ec26810..e24f138553b 100644 --- a/crates/rome_cli/tests/commands/ci.rs +++ b/crates/rome_cli/tests/commands/ci.rs @@ -744,3 +744,116 @@ something( ) result, )); } + +#[test] +fn ignore_vcs_ignored_file() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let rome_json = r#"{ + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + } + }"#; + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); bar.call();"#; + let code1 = r#"array.map(sentence => sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // configuration + let config_path = Path::new("rome.json"); + fs.insert(config_path.into(), rome_json.as_bytes()); + + // git folder + let git_folder = Path::new(".git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new(".gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("ci"), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file", + fs, + console, + result, + )); +} + +#[test] +fn ignore_vcs_ignored_file_via_cli() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); + + + bar.call();"#; + let code1 = r#"array.map(sentence => sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // git folder + let git_folder = Path::new("./.git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new("./.gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("ci"), + OsString::from("--vcs-enabled=true"), + OsString::from("--vcs-client-kind=git"), + OsString::from("--vcs-use-ignore-file=true"), + OsString::from("--vcs-root=."), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_err(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file_via_cli", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index 2792c456a3c..590c20f0580 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -1535,3 +1535,127 @@ fn print_verbose() { result, )); } + +#[test] +fn ignore_vcs_ignored_file() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let rome_json = r#"{ + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + } + }"#; + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); + + + bar.call();"#; + let code1 = r#"array.map(sentence => + + + sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // configuration + let config_path = Path::new("rome.json"); + fs.insert(config_path.into(), rome_json.as_bytes()); + + // git folder + let git_folder = Path::new(".git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new(".gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--write"), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file", + fs, + console, + result, + )); +} + +#[test] +fn ignore_vcs_ignored_file_via_cli() { + let mut fs = MemoryFileSystem::default(); + let mut console = BufferConsole::default(); + + let git_ignore = r#" +file2.js +"#; + + let code2 = r#"foo.call(); + + + bar.call();"#; + let code1 = r#"array.map(sentence => + + + sentence.split(' ')).flat();"#; + + // ignored files + let file_path1 = Path::new("file1.js"); + fs.insert(file_path1.into(), code1.as_bytes()); + let file_path2 = Path::new("file2.js"); + fs.insert(file_path2.into(), code2.as_bytes()); + + // git folder + let git_folder = Path::new("./.git"); + fs.insert(git_folder.into(), "".as_bytes()); + + // git ignore file + let ignore_file = Path::new("./.gitignore"); + fs.insert(ignore_file.into(), git_ignore.as_bytes()); + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Arguments::from_vec(vec![ + OsString::from("format"), + OsString::from("--vcs-enabled=true"), + OsString::from("--vcs-client-kind=git"), + OsString::from("--vcs-use-ignore-file=true"), + OsString::from("--vcs-root=."), + OsString::from("--write"), + file_path1.as_os_str().into(), + file_path2.as_os_str().into(), + ]), + ); + + assert!(result.is_ok(), "run_cli returned {result:?}"); + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "ignore_vcs_ignored_file_via_cli", + fs, + console, + result, + )); +} diff --git a/crates/rome_cli/tests/snapshots/main_commands_check/ignore_vcs_ignored_file.snap b/crates/rome_cli/tests/snapshots/main_commands_check/ignore_vcs_ignored_file.snap new file mode 100644 index 00000000000..2f31e89ec92 --- /dev/null +++ b/crates/rome_cli/tests/snapshots/main_commands_check/ignore_vcs_ignored_file.snap @@ -0,0 +1,76 @@ +--- +source: crates/rome_cli/tests/snap_test.rs +expression: content +--- +## `rome.json` + +```json +{ + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + } +} +``` + +## `.git` + +```git + +``` + +## `.gitignore` + +```gitignore + +file2.js + +``` + +## `file1.js` + +```js +array.map(sentence => sentence.split(' ')).flat(); +``` + +## `file2.js` + +```js +foo.call(); bar.call(); +``` + +# Termination Message + +```block +internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Some errors were emitted while running checks + + + +``` + +# Emitted Messages + +```block +file1.js:1:1 lint/complexity/useFlatMap FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × The call chain .map().flat() can be replaced with a single .flatMap() call. + + > 1 │ array.map(sentence => sentence.split(' ')).flat(); + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + i Safe fix: Replace the chain with .flatMap(). + + - array.map(sentence·=>·sentence.split('·')).flat(); + + array.flatMap(sentence·=>·sentence.split('·')); + + +``` + +```block +Checked 1 file(s) in