use crate::linter::{Diagnostic, Fix, LintResult, Severity, Span};
fn is_posix_sh(source: &str) -> bool {
let first_line = source.lines().next().unwrap_or("");
first_line.starts_with("#!/bin/sh")
|| first_line.starts_with("#! /bin/sh")
|| first_line.starts_with("#!/usr/bin/env sh")
}
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
if !is_posix_sh(source) {
return result;
}
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim_start();
if trimmed.starts_with('#') {
continue;
}
if let Some(col) = line.find("[[") {
let after = &line[col + 2..];
if after.starts_with(' ') || after.starts_with('\t') {
let span = Span::new(line_num + 1, col + 1, line_num + 1, col + 3);
let diagnostic = Diagnostic::new(
"PORT003",
Severity::Warning,
"`[[ ]]` is not supported in POSIX sh. Use `[ ]` (test) instead.",
span,
)
.with_fix(Fix::new("Replace [[ ]] with [ ]"));
result.add(diagnostic);
}
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_port003_detects_double_bracket_in_sh() {
let script = "#!/bin/sh\nif [[ -f /etc/config ]]; then\n echo exists\nfi";
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
assert_eq!(result.diagnostics[0].code, "PORT003");
assert_eq!(result.diagnostics[0].severity, Severity::Warning);
}
#[test]
fn test_port003_provides_fix() {
let script = "#!/bin/sh\n[[ -d /tmp ]]";
let result = check(script);
assert!(result.diagnostics[0].fix.is_some());
}
#[test]
fn test_port003_no_flag_in_bash() {
let script = "#!/bin/bash\nif [[ -f /etc/config ]]; then echo yes; fi";
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_port003_no_flag_single_bracket() {
let script = "#!/bin/sh\nif [ -f /etc/config ]; then echo yes; fi";
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_port003_no_false_positive_comment() {
let script = "#!/bin/sh\n# if [[ -f file ]]; then";
let result = check(script);
assert_eq!(result.diagnostics.len(), 0);
}
#[test]
fn test_port003_detects_standalone() {
let script = "#!/bin/sh\n[[ $x == y ]]";
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
}
}