use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static RANGE_WITH_PLUS: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r"\[(?:[^\]]|\[:.*?:\])+\]\+").unwrap()
});
fn has_ere_flag(arg: &str) -> bool {
if !arg.starts_with('-') || arg.starts_with("--") {
return false;
}
arg.chars().skip(1).any(|c| c == 'E' || c == 'P')
}
fn is_ere_context(line: &str) -> bool {
if line.contains("=~") {
return true;
}
if line.contains("grep") {
if line.contains("--extended-regexp") || line.contains("--perl-regexp") {
return true;
}
for word in line.split_whitespace() {
if has_ere_flag(word) {
return true;
}
}
}
if line.contains("egrep") {
return true;
}
if line.contains("sed") {
for word in line.split_whitespace() {
if word.starts_with('-')
&& !word.starts_with("--")
&& (word.contains('E') || word.contains('r'))
{
return true;
}
}
}
if line.contains("awk") || line.contains("gawk") {
return true;
}
false
}
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;
}
if is_ere_context(line) {
continue;
}
for mat in RANGE_WITH_PLUS.find_iter(line) {
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let diagnostic = Diagnostic::new(
"SC2102",
Severity::Warning,
"Ranges can only match single chars (to match + literally, use \\+)".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_sc2102_range_plus() {
let code = "[[ $var = [0-9]+ ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2102_case_range_plus() {
let code = "case $x in [a-z]+) ;;";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2102_regex_ok() {
let code = "[[ $var =~ [0-9]+ ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_glob_star_ok() {
let code = "[[ $var = [0-9]* ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_comment_ok() {
let code = "# [[ $var = [0-9]+ ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_literal_plus() {
let code = "[[ $var = [0-9]\\+ ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_multiple_ranges() {
let code = "case $x in [a-z]+|[0-9]+) ;;";
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2102_in_test() {
let code = "[ \"$var\" = [0-9]+ ]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2102_posix_class() {
let code = "[[ $var = [[:digit:]]+ ]]";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2102_find_name() {
let code = "find . -name \"[0-9]+\"";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2102_issue_92_grep_e_flag() {
let code = r#"grep -oE 'error\[E[0-9]+\]' "$COMPILE_LOG""#;
let result = check(code);
assert_eq!(
result.diagnostics.len(),
0,
"SC2102 must NOT flag [0-9]+ in grep -E (ERE context)"
);
}
#[test]
fn test_sc2102_issue_92_grep_extended_regexp() {
let code = "grep --extended-regexp '[0-9]+' file.txt";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_issue_92_grep_p_flag() {
let code = "grep -P '[0-9]+' file.txt";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_issue_92_egrep() {
let code = "egrep '[0-9]+' file.txt";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_issue_92_sed_e_flag() {
let code = "sed -E 's/[0-9]+/X/g' file.txt";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_issue_92_awk() {
let code = "awk '/[0-9]+/ { print }' file.txt";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2102_issue_92_basic_grep_still_flagged() {
let code = "grep '[0-9]+' file.txt";
let result = check(code);
assert_eq!(
result.diagnostics.len(),
1,
"Basic grep (BRE) should still flag + quantifier"
);
}
}