use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static DOUBLE_BRACKET_MIXED: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r"\[\[([^\]]+)\]\]").unwrap()
});
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
for (line_num, line) in source.lines().enumerate() {
let line_num = line_num + 1;
if line.trim_start().starts_with('#') {
continue;
}
for mat in DOUBLE_BRACKET_MIXED.find_iter(line) {
let matched = mat.as_str();
let content = &matched[2..matched.len() - 2];
let has_old_a = content.contains(" -a ");
let has_old_o = content.contains(" -o ");
let has_new_and = content.contains("&&");
let has_new_or = content.contains("||");
let has_old = has_old_a || has_old_o;
let has_new = has_new_and || has_new_or;
if has_old && has_new {
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2110",
Severity::Warning,
"In [[ ]], don't mix && and || with -a and -o".to_string(),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2110_mixed_and() {
let code = "[[ $x -eq 1 -a $y -eq 2 && $z -eq 3 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2110_mixed_or() {
let code = "[[ $x -eq 1 || $y -eq 2 -o $z -eq 3 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2110_all_new_ok() {
let code = "[[ $x -eq 1 && $y -eq 2 || $z -eq 3 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2110_all_old() {
let code = "[[ $x -eq 1 -a $y -eq 2 -o $z -eq 3 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2110_comment_ok() {
let code = "# [[ $x -eq 1 -a $y -eq 2 && $z -eq 3 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2110_single_bracket_ok() {
let code = "[ $x -eq 1 -a $y -eq 2 ]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2110_file_tests_mixed() {
let code = "[[ -f file && -d dir -o -r file ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2110_complex_mixed() {
let code = "[[ $a -eq 1 -a $b -eq 2 || $c -eq 3 && $d -eq 4 ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2110_multiline() {
let code = r#"
if [[ $x -eq 1 -a $y -eq 2 || $z -eq 3 ]]; then
echo "test"
fi
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2110_string_tests() {
let code = r#"[[ "$a" = "test" && "$b" = "value" -o -z "$c" ]]"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
}