use crate::linter::{Diagnostic, LintResult, Severity, Span};
use regex::Regex;
static SET_A_ARRAY: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| {
Regex::new(r"\bset\s+-A\s+[a-zA-Z_][a-zA-Z0-9_]*\b").unwrap()
});
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
let has_ksh_shebang = source
.lines()
.next()
.is_some_and(|line| line.starts_with("#!/bin/ksh") || line.starts_with("#!/usr/bin/ksh"));
let has_sh_shebang = source
.lines()
.next()
.is_some_and(|line| line.starts_with("#!/bin/sh") || line.starts_with("#!/usr/bin/sh"));
for (line_num, line) in source.lines().enumerate() {
let line_num = line_num + 1;
if line.trim_start().starts_with('#') {
continue;
}
for mat in SET_A_ARRAY.find_iter(line) {
let start_col = mat.start() + 1;
let end_col = mat.end() + 1;
let message = if has_sh_shebang {
"set -A is ksh-specific and won't work in sh. Use bash arrays or change shebang"
.to_string()
} else if !has_ksh_shebang {
"set -A is ksh-specific. Add #!/bin/ksh shebang".to_string()
} else {
continue;
};
let diagnostic = Diagnostic::new(
"SC2118",
Severity::Error,
message,
Span::new(line_num, start_col, line_num, end_col),
);
result.add(diagnostic);
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sc2118_set_a_in_sh() {
let code = r#"#!/bin/sh
set -A myarray value1 value2
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2118_set_a_no_shebang() {
let code = "set -A array val1 val2";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2118_set_a_in_ksh_ok() {
let code = r#"#!/bin/ksh
set -A myarray value1 value2
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2118_comment_ok() {
let code = "# set -A array value1 value2";
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2118_bash_array_ok() {
let code = r#"#!/bin/bash
array=(value1 value2)
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_sc2118_multiple() {
let code = r#"#!/bin/sh
set -A arr1 val1
set -A arr2 val2
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 2);
}
#[test]
fn test_sc2118_with_flags() {
let code = "set -A myarray item1 item2 item3";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2118_underscore_name() {
let code = "set -A _private_array val";
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2118_in_function() {
let code = r#"
foo() {
set -A local_array item1 item2
}
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 1);
}
#[test]
fn test_sc2118_usr_bin_ksh_ok() {
let code = r#"#!/usr/bin/ksh
set -A array value
"#;
let result = check(code);
assert_eq!(result.diagnostics.len(), 0);
}
}