bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! `.bashrsignore` file support for excluding files and rules from linting
//!
//! Implements gitignore-style pattern matching for file exclusion,
//! plus rule code ignoring (Issue #85) and line-specific ignoring (Issue #109).
//!
//! # Syntax
//!
//! - `pattern` - Glob pattern to match files (e.g., `vendor/**/*.sh`)
//! - `RULE_CODE` - Rule code to ignore globally (e.g., `SEC010`, `SC2031`, `DET001`)
//! - `RULE_CODE:path` - Ignore rule only in specific file (Issue #109)
//! - `RULE_CODE:path:line` - Ignore rule only on specific line (Issue #109)
//! - `# comment` - Comments (lines starting with #)
//! - Empty lines are ignored
//! - `!pattern` - Negation (re-include a previously excluded file pattern)
//!
//! # Rule Code Format
//!
//! Rule codes are automatically detected when a line matches:
//! - SEC followed by digits (e.g., `SEC001`, `SEC010`)
//! - SC followed by digits (e.g., `SC2031`, `SC2046`)
//! - DET followed by digits (e.g., `DET001`, `DET002`)
//! - IDEM followed by digits (e.g., `IDEM001`, `IDEM002`)
//!
//! # Examples
//!
//! ```text
//! # .bashrsignore example
//!
//! # Ignore specific lint rules globally (Issue #85)
//! SEC010
//! SC2031
//! SC2046
//!
//! # Ignore rule only in specific file (Issue #109)
//! SEC010:scripts/install.sh
//! DET001:scripts/metrics.sh
//!
//! # Ignore rule only on specific line (Issue #109)
//! SEC010:scripts/install.sh:42
//! DET002:scripts/record.sh:15
//!
//! # Exclude vendor scripts
//! vendor/**/*.sh
//!
//! # Exclude specific file with documented rationale
//! # Rationale: DET002 (timestamps) is intentional for metrics recording
//! scripts/record-metric.sh
//!
//! # Exclude by pattern
//! **/generated/*.sh
//!
//! # Re-include important.sh even if in vendor
//! !vendor/important.sh
//! ```

use glob::Pattern;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::Path;

/// Result of checking if a file should be ignored
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IgnoreResult {
    /// File should be ignored (matched a pattern)
    Ignored(String), // The pattern that matched
    /// File should NOT be ignored
    NotIgnored,
}

/// Parser for `.bashrsignore` files
#[derive(Debug, Default)]
pub struct IgnoreFile {
    /// Include patterns (files to ignore)
    include_patterns: Vec<CompiledPattern>,
    /// Exclude patterns (files to NOT ignore, even if matched by include)
    exclude_patterns: Vec<CompiledPattern>,
    /// Rule codes to ignore globally (Issue #85)
    /// Stored in uppercase for case-insensitive matching
    ignored_rule_codes: HashSet<String>,
    /// Issue #109: File-specific rule ignores
    /// Key: normalized file path, Value: set of rule codes to ignore
    file_specific_ignores: HashMap<String, HashSet<String>>,
    /// Issue #109: Line-specific rule ignores
    /// Key: (normalized file path, line number), Value: set of rule codes to ignore
    line_specific_ignores: HashMap<(String, usize), HashSet<String>>,
}

#[derive(Debug)]
struct CompiledPattern {
    /// Original pattern string (for error messages)
    original: String,
    /// Compiled glob pattern
    pattern: Pattern,
}

/// Check if a string looks like a rule code (Issue #85)
///
/// Rule codes follow patterns like:
/// - SEC001, SEC010 (security rules)
/// - SC2031, SC2046 (shellcheck rules)
/// - DET001, DET002 (determinism rules)
/// - IDEM001, IDEM002 (idempotency rules)
fn is_rule_code(s: &str) -> bool {
    let s = s.trim().to_uppercase();

    // Check for common rule code patterns
    // SEC followed by digits
    if s.starts_with("SEC") && s.len() >= 4 && s[3..].chars().all(|c| c.is_ascii_digit()) {
        return true;
    }

    // SC followed by digits (shellcheck)
    if s.starts_with("SC") && s.len() >= 3 && s[2..].chars().all(|c| c.is_ascii_digit()) {
        return true;
    }

    // DET followed by digits
    if s.starts_with("DET") && s.len() >= 4 && s[3..].chars().all(|c| c.is_ascii_digit()) {
        return true;
    }

    // IDEM followed by digits
    if s.starts_with("IDEM") && s.len() >= 5 && s[4..].chars().all(|c| c.is_ascii_digit()) {
        return true;
    }

    false
}

/// Issue #109: Parse a rule specifier with optional file and line
///
/// Format: RULE_CODE or RULE_CODE:path or RULE_CODE:path:line
///
/// Returns: Some((rule_code, file_path, line_num)) if valid, None otherwise
fn parse_rule_specifier(s: &str) -> Option<(String, Option<String>, Option<usize>)> {
    let trimmed = s.trim();

    // Split by ':'
    let parts: Vec<&str> = trimmed.splitn(3, ':').collect();

    // First part must be a rule code
    let rule_code = parts.first()?;
    if !is_rule_code(rule_code) {
        return None;
    }

    match parts.len() {
        // Just rule code: SEC010
        1 => Some((rule_code.to_string(), None, None)),
        // Rule + file: SEC010:path/to/file.sh
        2 => {
            let path = parts[1].trim();
            if path.is_empty() {
                return None;
            }
            Some((rule_code.to_string(), Some(path.to_string()), None))
        }
        // Rule + file + line: SEC010:path/to/file.sh:42
        3 => {
            let path = parts[1].trim();
            let line_str = parts[2].trim();
            if path.is_empty() {
                return None;
            }
            let line_num = line_str.parse::<usize>().ok()?;
            Some((
                rule_code.to_string(),
                Some(path.to_string()),
                Some(line_num),
            ))
        }
        _ => None,
    }
}

/// Normalize a file path for consistent matching
fn normalize_path(path: &str) -> String {
    // Remove leading ./ if present
    let path = path.strip_prefix("./").unwrap_or(path);
    // Convert backslashes to forward slashes (Windows compatibility)
    path.replace('\\', "/")
}

impl IgnoreFile {
    /// Create an empty ignore file (no patterns)
    pub fn empty() -> Self {
        Self::default()
    }

    /// Load ignore patterns from a file
    ///
    /// Returns `Ok(None)` if the file doesn't exist.
    /// Returns `Ok(Some(IgnoreFile))` if the file exists and was parsed.
    /// Returns `Err` if the file exists but couldn't be read.
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::IgnoreFile;
    /// use std::path::Path;
    ///
    /// // Load from project root
    /// let ignore = IgnoreFile::load(Path::new(".bashrsignore"));
    /// ```
    pub fn load(path: &Path) -> Result<Option<Self>, String> {
        if !path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(path)
            .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;

        Ok(Some(Self::parse(&content)?))
    }

    /// Parse ignore patterns from string content
    ///
    /// Supports both file patterns (glob syntax) and rule codes (Issue #85).
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::IgnoreFile;
    ///
    /// let content = r#"
    /// # Vendor scripts
    /// vendor/*.sh
    ///
    /// # Generated files
    /// **/generated/*.sh
    ///
    /// # Ignore specific rules (Issue #85)
    /// SEC010
    /// SC2031
    /// "#;
    ///
    /// let ignore = IgnoreFile::parse(content).expect("valid patterns");
    /// assert!(ignore.should_ignore_rule("SEC010"));
    /// ```
    pub fn parse(content: &str) -> Result<Self, String> {
        let mut ignore = Self::default();

        for (line_num, line) in content.lines().enumerate() {
            let trimmed = line.trim();

            // Skip empty lines and comments
            if trimmed.is_empty() || trimmed.starts_with('#') {
                continue;
            }

            // Check for negation pattern (only applies to file patterns)
            let (is_negation, pattern_str) = if let Some(stripped) = trimmed.strip_prefix('!') {
                (true, stripped.trim())
            } else {
                (false, trimmed)
            };

            // Issue #85/#109: Check if this is a rule code (with optional file:line specifier)
            // Format: RULE_CODE or RULE_CODE:path or RULE_CODE:path:line
            if !is_negation {
                if let Some((rule_code, file_path, line_num)) = parse_rule_specifier(pattern_str) {
                    let rule_upper = rule_code.to_uppercase();

                    match (file_path, line_num) {
                        // Issue #109: Line-specific ignore
                        (Some(path), Some(line)) => {
                            let key = (normalize_path(&path), line);
                            ignore
                                .line_specific_ignores
                                .entry(key)
                                .or_default()
                                .insert(rule_upper);
                        }
                        // Issue #109: File-specific ignore
                        (Some(path), None) => {
                            let key = normalize_path(&path);
                            ignore
                                .file_specific_ignores
                                .entry(key)
                                .or_default()
                                .insert(rule_upper);
                        }
                        // Issue #85: Global rule ignore
                        (None, None) => {
                            ignore.ignored_rule_codes.insert(rule_upper);
                        }
                        // Invalid: line without file
                        (None, Some(_)) => {
                            // This shouldn't happen with proper parsing
                        }
                    }
                    continue;
                }
            }

            // Otherwise, treat as a file pattern
            let pattern = Pattern::new(pattern_str).map_err(|e| {
                format!(
                    "Invalid pattern on line {}: '{}' - {}",
                    line_num + 1,
                    pattern_str,
                    e
                )
            })?;

            let compiled = CompiledPattern {
                original: trimmed.to_string(),
                pattern,
            };

            if is_negation {
                ignore.exclude_patterns.push(compiled);
            } else {
                ignore.include_patterns.push(compiled);
            }
        }

        Ok(ignore)
    }

    /// Check if a file path should be ignored
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::{IgnoreFile, IgnoreResult};
    /// use std::path::Path;
    ///
    /// let content = "vendor/*.sh\n!vendor/important.sh";
    /// let ignore = IgnoreFile::parse(content).expect("valid patterns");
    ///
    /// // Matches include pattern
    /// assert!(matches!(
    ///     ignore.should_ignore(Path::new("vendor/foo.sh")),
    ///     IgnoreResult::Ignored(_)
    /// ));
    ///
    /// // Excluded by negation pattern
    /// assert_eq!(
    ///     ignore.should_ignore(Path::new("vendor/important.sh")),
    ///     IgnoreResult::NotIgnored
    /// );
    ///
    /// // Not matched at all
    /// assert_eq!(
    ///     ignore.should_ignore(Path::new("src/main.sh")),
    ///     IgnoreResult::NotIgnored
    /// );
    /// ```
    pub fn should_ignore(&self, path: &Path) -> IgnoreResult {
        let path_str = path.to_string_lossy();

        // Check exclude patterns first (negation wins)
        for pattern in &self.exclude_patterns {
            if pattern.pattern.matches(&path_str) {
                return IgnoreResult::NotIgnored;
            }
        }

        // Check include patterns
        for pattern in &self.include_patterns {
            if pattern.pattern.matches(&path_str) {
                return IgnoreResult::Ignored(pattern.original.clone());
            }
        }

        IgnoreResult::NotIgnored
    }

    /// Check if there are any patterns loaded
    pub fn has_patterns(&self) -> bool {
        !self.include_patterns.is_empty() || !self.exclude_patterns.is_empty()
    }

    /// Get the number of patterns
    pub fn pattern_count(&self) -> usize {
        self.include_patterns.len() + self.exclude_patterns.len()
    }

    /// Check if a rule code should be ignored globally (Issue #85)
    ///
    /// Rule codes are matched case-insensitively.
    /// This only checks global ignores; use `should_ignore_rule_at` for
    /// file/line-specific checks.
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::IgnoreFile;
    ///
    /// let content = "SEC010\nSC2031";
    /// let ignore = IgnoreFile::parse(content).expect("valid patterns");
    ///
    /// assert!(ignore.should_ignore_rule("SEC010"));
    /// assert!(ignore.should_ignore_rule("sec010")); // Case-insensitive
    /// assert!(ignore.should_ignore_rule("SC2031"));
    /// assert!(!ignore.should_ignore_rule("SEC001")); // Not in file
    /// ```
    pub fn should_ignore_rule(&self, rule_code: &str) -> bool {
        self.ignored_rule_codes.contains(&rule_code.to_uppercase())
    }

    /// Issue #109: Check if a rule should be ignored at a specific location
    ///
    /// Checks in order:
    /// 1. Line-specific ignores (RULE:file:line)
    /// 2. File-specific ignores (RULE:file)
    /// 3. Global ignores (RULE)
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::IgnoreFile;
    /// use std::path::Path;
    ///
    /// let content = r#"
    /// SEC010
    /// SC2031:scripts/install.sh
    /// DET001:scripts/metrics.sh:42
    /// "#;
    /// let ignore = IgnoreFile::parse(content).expect("valid patterns");
    ///
    /// // Global ignore applies everywhere
    /// assert!(ignore.should_ignore_rule_at("SEC010", Path::new("any/file.sh"), 1));
    ///
    /// // File-specific ignore only applies in that file
    /// assert!(ignore.should_ignore_rule_at("SC2031", Path::new("scripts/install.sh"), 1));
    /// assert!(!ignore.should_ignore_rule_at("SC2031", Path::new("other/file.sh"), 1));
    ///
    /// // Line-specific ignore only applies on that line
    /// assert!(ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 42));
    /// assert!(!ignore.should_ignore_rule_at("DET001", Path::new("scripts/metrics.sh"), 43));
    /// ```
    pub fn should_ignore_rule_at(&self, rule_code: &str, file_path: &Path, line: usize) -> bool {
        let rule_upper = rule_code.to_uppercase();
        let path_str = normalize_path(&file_path.to_string_lossy());

        // Check line-specific ignore first
        let line_key = (path_str.clone(), line);
        if let Some(rules) = self.line_specific_ignores.get(&line_key) {
            if rules.contains(&rule_upper) {
                return true;
            }
        }

        // Check file-specific ignore
        if let Some(rules) = self.file_specific_ignores.get(&path_str) {
            if rules.contains(&rule_upper) {
                return true;
            }
        }

        // Check global ignore
        self.ignored_rule_codes.contains(&rule_upper)
    }

    /// Get all ignored rule codes (Issue #85)
    ///
    /// Returns a vector of all rule codes that should be ignored,
    /// in uppercase form.
    ///
    /// # Examples
    ///
    /// ```
    /// use bashrs::linter::IgnoreFile;
    ///
    /// let content = "SEC010\nSC2031\nDET001";
    /// let ignore = IgnoreFile::parse(content).expect("valid patterns");
    ///
    /// let rules = ignore.ignored_rules();
    /// assert_eq!(rules.len(), 3);
    /// assert!(rules.contains(&"SEC010".to_string()));
    /// ```
    pub fn ignored_rules(&self) -> Vec<String> {
        self.ignored_rule_codes.iter().cloned().collect()
    }

    /// Check if there are any ignored rule codes (Issue #85)
    pub fn has_ignored_rules(&self) -> bool {
        !self.ignored_rule_codes.is_empty()
    }
}

#[cfg(test)]
#[path = "ignore_file_tests_issue_85.rs"]
mod tests_extracted;