use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static DOLLAR_ASSIGN: std::sync::LazyLock<Regex> =
std::sync::LazyLock::new(|| Regex::new(r"(?:^|[;\s])\$([A-Za-z_]\w*)=").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;
let trimmed = line.trim_start();
if trimmed.starts_with('#') {
continue;
}
for caps in DOLLAR_ASSIGN.captures_iter(line) {
let var_match = caps.get(1).unwrap();
let var_name = var_match.as_str();
let dollar_pos = var_match.start() - 1;
let start_col = dollar_pos + 1;
let end_col = var_match.end() + 2;
let diagnostic = Diagnostic::new(
"SC1066",
Severity::Error,
format!(
"Don't use `$` on the left side of assignments. Use `{}=` instead of `${}=`.",
var_name, var_name
),
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc1066_dollar_assignment() {
let code = "$VAR=hello";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "SC1066");
assert_eq!(result.diagnostics[0].severity, Severity::Error);
assert!(result.diagnostics[0].message.contains("VAR="));
}
#[test]
fn test_sc1066_dollar_path_assignment() {
let code = "$MY_PATH=/usr/bin";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc1066_after_semicolon() {
let code = "echo hi; $X=5";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc1066_normal_assignment_ok() {
let code = "VAR=hello";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc1066_variable_expansion_ok() {
let code = "echo $VAR=hello";
let result = check(code);
let _count = result.diagnostics.len();
}
#[test]
fn test_sc1066_comment_ok() {
let code = "# $VAR=hello";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
}