use crate::linter::diagnostic::FixSafetyLevel;
use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
use regex::Regex;
static PATTERN: std::sync::LazyLock<Regex> =
std::sync::LazyLock::new(|| Regex::new(r"\|\s*echo(\s|$)").unwrap());
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let pattern = &*PATTERN;
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
if trimmed.starts_with('#') {
continue;
}
if let Some(mat) = pattern.find(line) {
let start = mat.start();
let end = mat.end();
result.diagnostics.push(Diagnostic {
code: "SC2008".to_string(),
severity: Severity::Warning,
message: "echo doesn't read from stdin. Use 'cat' to output stdin, or 'xargs echo' to convert stdin to arguments.".to_string(),
span: Span {
start_line: line_num + 1, end_line: line_num + 1, start_col: start + 1, end_col: end + 1, },
fix: Some(Fix {
replacement: "| cat".to_string(),
safety_level: FixSafetyLevel::Safe,
assumptions: vec![],
suggested_alternatives: vec!["| xargs echo".to_string()],
}),
});
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2008_basic_pipe_to_echo() {
let source = "command | echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2008");
assert!(result.diagnostics[0].message.contains("echo"));
assert!(result.diagnostics[0].message.contains("stdin"));
}
#[test]
fn test_sc2008_pipe_with_echo_arguments() {
let source = r#"grep pattern file | echo "result""#;
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC2008");
}
#[test]
fn test_sc2008_multiple_pipes_ending_in_echo() {
let source = "cat file.txt | grep foo | echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2008_echo_without_pipe() {
let source = r#"echo "Hello World""#;
let result = check(source);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2008_pipe_to_cat() {
let source = "command | cat";
let result = check(source);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2008_pipe_to_xargs_echo() {
let source = "command | xargs echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2008_multiline_pipeline() {
let source = r#"cat file.txt \
| grep pattern \
| echo"#;
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2008_echo_in_comment() {
let source = "# This is wrong: command | echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2008_auto_fix_suggests_cat() {
let source = "command | echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
let fix = &result.diagnostics[0].fix;
assert!(fix.is_some());
let fix = fix.as_ref().unwrap();
assert!(fix.replacement.contains("cat") || fix.replacement.contains("xargs echo"));
}
#[test]
fn test_sc2008_severity_warning() {
let source = "command | echo";
let result = check(source);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].severity, Severity::Warning);
}
}