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

Commit

Permalink
chore: unify traversal modes
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Jul 12, 2022
1 parent 9a73f5c commit 41e7a3c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
47 changes: 25 additions & 22 deletions crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,40 @@ pub(crate) fn check(mut session: CliSession) -> Result<(), Termination> {
.as_ref()
.map_or(false, |c| c.is_linter_disabled());

let max_diagnostics: Option<u8> = session
.args
.opt_value_from_str("--max-diagnostics")
.map_err(|source| Termination::ParseError {
argument: "--max-diagnostics",
source,
})?;
let max_diagnostics = if let Some(max_diagnostics) = max_diagnostics {
if max_diagnostics > MAXIMUM_DISPLAYABLE_DIAGNOSTICS {
return Err(Termination::OverflowNumberArgument(
"--max-diagnostics",
"50",
));
}

max_diagnostics
} else {
// default value
20
};

let mode = if session.args.contains("--apply") {
TraversalMode::Fix {
TraversalMode::Check {
linter_disabled,
formatter_disabled,
should_fix: true,
max_diagnostics,
}
} else {
let max_diagnostics: Option<u8> = session
.args
.opt_value_from_str("--max-diagnostics")
.map_err(|source| Termination::ParseError {
argument: "--max-diagnostics",
source,
})?;
let max_diagnostics = if let Some(max_diagnostics) = max_diagnostics {
if max_diagnostics > MAXIMUM_DISPLAYABLE_DIAGNOSTICS {
return Err(Termination::OverflowNumberArgument(
"--max-diagnostics",
"50",
));
}

max_diagnostics
} else {
// default value
20
};

TraversalMode::Check {
max_diagnostics,
linter_disabled,
formatter_disabled,
should_fix: false,
}
};

Expand Down
55 changes: 31 additions & 24 deletions crates/rome_cli/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,20 @@ pub(crate) fn traverse(mode: TraversalMode, mut session: CliSession) -> Result<(
let skipped = skipped.load(Ordering::Relaxed);

match mode {
TraversalMode::Check { .. } | TraversalMode::CI { .. } => {
console.log(rome_console::markup! {
<Info>"Checked "{count}" files in "{duration}</Info>
});
TraversalMode::Check { should_fix, .. } => {
if should_fix {
console.log(rome_console::markup! {
<Info>"Fixed "{count}" files in "{duration}</Info>
});
} else {
console.log(rome_console::markup! {
<Info>"Checked "{count}" files in "{duration}</Info>
});
}
}
TraversalMode::Fix { .. } => {
TraversalMode::CI { .. } => {
console.log(rome_console::markup! {
<Info>"Fixed "{count}" files in "{duration}</Info>
<Info>"Checked "{count}" files in "{duration}</Info>
});
}
TraversalMode::Format { write: false, .. } => {
Expand Down Expand Up @@ -280,23 +286,21 @@ fn print_messages_to_console(

#[derive(Clone, Copy)]
pub(crate) enum TraversalMode {
/// This mode is enabled when running the command `rome check`
Check {
max_diagnostics: u8,
formatter_disabled: bool,
linter_disabled: bool,
/// `true` when running the command `check` with the `--apply` argument
should_fix: bool,
},
/// This mode is enabled when running the command `rome ci`
CI {
formatter_disabled: bool,
linter_disabled: bool,
},
Fix {
formatter_disabled: bool,
linter_disabled: bool,
},
Format {
ignore_errors: bool,
write: bool,
},
/// This mode is enabled when running the command `rome format`
Format { ignore_errors: bool, write: bool },
}

impl TraversalMode {
Expand All @@ -309,6 +313,15 @@ impl TraversalMode {
}
}

/// `true` only when running the traversal in [TraversalMode::Check] and `should_fix` is `true`
fn should_fix(&self) -> bool {
if let TraversalMode::Check { should_fix, .. } = self {
*should_fix
} else {
false
}
}

fn is_ci(&self) -> bool {
matches!(self, TraversalMode::CI { .. })
}
Expand All @@ -318,9 +331,6 @@ impl TraversalMode {
TraversalMode::Check {
formatter_disabled, ..
} => *formatter_disabled,
TraversalMode::Fix {
formatter_disabled, ..
} => *formatter_disabled,
TraversalMode::CI {
formatter_disabled, ..
} => *formatter_disabled,
Expand All @@ -333,9 +343,6 @@ impl TraversalMode {
TraversalMode::Check {
linter_disabled, ..
} => *linter_disabled,
TraversalMode::Fix {
linter_disabled, ..
} => *linter_disabled,
TraversalMode::CI {
linter_disabled, ..
} => *linter_disabled,
Expand Down Expand Up @@ -399,7 +406,7 @@ impl<'ctx, 'app> TraversalContext for TraversalOptions<'ctx, 'app> {

fn can_handle(&self, rome_path: &RomePath) -> bool {
match self.mode {
TraversalMode::Check { .. } | TraversalMode::Fix { .. } => self.can_lint(rome_path),
TraversalMode::Check { .. } => self.can_lint(rome_path),
TraversalMode::CI { .. } => self.can_lint(rome_path) || self.can_format(rome_path),
TraversalMode::Format { .. } => self.can_format(rome_path),
}
Expand Down Expand Up @@ -473,7 +480,7 @@ fn process_file(ctx: &TraversalOptions, path: &Path, file_id: FileId) -> FileRes
let rome_path = RomePath::new(path, file_id);
let can_format = ctx.can_format(&rome_path);
let can_handle = match ctx.mode {
TraversalMode::Check { .. } | TraversalMode::Fix { .. } => ctx.can_lint(&rome_path),
TraversalMode::Check { .. } => ctx.can_lint(&rome_path),
TraversalMode::CI { .. } => ctx.can_lint(&rome_path) || can_format,
TraversalMode::Format { .. } => can_format,
};
Expand Down Expand Up @@ -502,7 +509,7 @@ fn process_file(ctx: &TraversalOptions, path: &Path, file_id: FileId) -> FileRes
)
.with_file_id_and_code(file_id, "IO")?;

if let TraversalMode::Fix { .. } = ctx.mode {
if ctx.mode.should_fix() {
let fixed = file_guard
.fix_file()
.with_file_id_and_code(file_id, "Lint")?;
Expand Down Expand Up @@ -581,7 +588,7 @@ fn process_file(ctx: &TraversalOptions, path: &Path, file_id: FileId) -> FileRes
if can_format {
let write = match ctx.mode {
// In check mode do not run the formatter and return the result immediately
TraversalMode::Check { .. } | TraversalMode::Fix { .. } => return Ok(result),
TraversalMode::Check { .. } => return Ok(result),
TraversalMode::CI { .. } => false,
TraversalMode::Format { write, .. } => write,
};
Expand Down

0 comments on commit 41e7a3c

Please sign in to comment.