Skip to content
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/cli-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ strum.workspace = true
strum_macros.workspace = true
fern = "0.7"
log.workspace = true
ignore = "0.4.26"
anstyle = "1.0"
clap = { version = "4.6.1", features = ["derive"] }
pyo3 = { version = "0.29.0", optional = true }
Expand Down
103 changes: 46 additions & 57 deletions crates/cli-lib/src/commands_fix.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::FixArgs;
use crate::commands::Format;
use crate::linter;
use crate::commands_lint::{ApplyFixes, Input, LintCommand, run_lint_command};
use sqruff_lib::api::Mode;
use sqruff_lib::core::config::FluffConfig;
use std::path::Path;

Expand All @@ -11,41 +12,17 @@ pub(crate) fn run_fix(
collect_parse_errors: bool,
) -> i32 {
let FixArgs { paths, format } = args;
let mut linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
return 1;
}
};
let result = match linter.lint_paths(paths, true, &ignorer) {
Ok(result) => result,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

if !result.has_violations() {
println!("{} files processed, nothing to fix.", result.len());
0
} else {
let any_unfixable_errors = result.has_unfixable_violations();
let files = result.len();

for mut file in result {
if !file.has_fixes() {
continue;
}
let path = std::mem::take(&mut file.path);
let fixed = file.fix_string();
std::fs::write(path, fixed).unwrap();
}

linter.formatter_mut().unwrap().completion_message(files);

any_unfixable_errors as i32
}
run_lint_command(
LintCommand {
mode: Mode::Fix,
input: Input::Paths(paths),
apply: ApplyFixes::ToDisk,
format,
},
config,
ignorer,
collect_parse_errors,
)
}

pub(crate) fn run_fix_stdin(
Expand All @@ -55,27 +32,17 @@ pub(crate) fn run_fix_stdin(
) -> i32 {
let read_in = crate::stdin::read_std_in().unwrap();

let linter = match linter(config, format, collect_parse_errors) {
Ok(l) => l,
Err(e) => {
eprintln!("{}", e);
return 1;
}
};
let result = match linter.lint_string(&read_in, None, true) {
Ok(result) => result,
Err(e) => {
eprintln!("{}", e.value);
return 1;
}
};

let has_unfixable_errors = result.has_unfixable_violations();

println!("{}", result.fix_string());

// if all fixable violations are fixable, return 0 else return 1
has_unfixable_errors as i32
run_lint_command(
LintCommand {
mode: Mode::Fix,
input: Input::Stdin(read_in),
apply: ApplyFixes::Stdout,
format,
},
config,
|_| false,
collect_parse_errors,
)
}

#[cfg(test)]
Expand Down Expand Up @@ -112,4 +79,26 @@ mod tests {
let after = std::fs::metadata(&path).unwrap().modified().unwrap();
assert_eq!(before, after);
}

#[test]
fn run_fix_writes_file_when_changes_exist() {
let mut tmp = NamedTempFile::new().unwrap();
write!(tmp, "SELECT foo bar FROM tabs").unwrap();
tmp.flush().unwrap();
let tmp = tmp.into_temp_path();
let path = tmp.to_path_buf();

let args = FixArgs {
paths: vec![path.clone()],
format: Format::Human,
};
let config = FluffConfig::from_source("[sqruff]\nrules = AL02\n", None);
let exit_code = run_fix(args, config, ignore_none, true);

assert_eq!(exit_code, 0);
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"SELECT foo AS bar FROM tabs"
);
}
}
Loading