shuck-linter 0.0.26

Lint rule engine and checker for shell scripts
Documentation
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
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

use anyhow::{Context, Result};
use globset::{Glob, GlobMatcher};
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet;
use shuck_semantic::{UnreachedFunctionAnalysisOptions, UnusedAssignmentAnalysisOptions};

use crate::{Category, Rule, RuleSelector, RuleSet, Severity, ShellDialect};

// ShellCheck's optional checks currently map only to compat-only behavior
// toggles in this repository, not to standalone implemented non-style rules.
const DEFAULT_DISABLED_NON_STYLE_RULES: &[Rule] = &[];

/// Per-rule behavior overrides applied during lint analysis.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LinterRuleOptions {
    /// Behavior overrides for `C001`.
    pub c001: C001RuleOptions,
    /// Behavior overrides for `C063`.
    pub c063: C063RuleOptions,
}

/// Behavior overrides for `C001` unused-assignment analysis.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct C001RuleOptions {
    /// Whether scalar indirect-expansion targets like `${!name}` count as a use of the target.
    /// Disabled by default to match ShellCheck. Array-like targets such as
    /// `name=arr[@]; ${!name}` stay live in both modes.
    pub treat_indirect_expansion_targets_as_used: bool,
}

impl C001RuleOptions {
    pub(crate) fn semantic_options(&self) -> UnusedAssignmentAnalysisOptions {
        UnusedAssignmentAnalysisOptions {
            treat_indirect_expansion_targets_as_used: self.treat_indirect_expansion_targets_as_used,
            report_unreachable_assignments: true,
        }
    }
}

/// Behavior overrides for `C063` overwritten/unreached function analysis.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct C063RuleOptions {
    /// Whether nested function definitions should be reported when no reachable direct call
    /// reaches the enclosing function scope before that scope exits.
    pub report_unreached_nested_definitions: bool,
}

impl C063RuleOptions {
    pub(crate) fn semantic_options(&self) -> UnreachedFunctionAnalysisOptions {
        UnreachedFunctionAnalysisOptions {
            report_unreached_nested_definitions: self.report_unreached_nested_definitions,
        }
    }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinterSettings {
    pub rules: RuleSet,
    pub severity_overrides: FxHashMap<Rule, Severity>,
    pub shell: ShellDialect,
    pub ambient_shell_options: AmbientShellOptions,
    pub analyzed_paths: Option<Arc<FxHashSet<PathBuf>>>,
    pub per_file_ignores: Arc<CompiledPerFileIgnoreList>,
    pub report_environment_style_names: bool,
    pub resolve_source_closure: bool,
    pub rule_options: LinterRuleOptions,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AmbientShellOptions {
    pub errexit: bool,
    pub pipefail: bool,
}

impl Default for LinterSettings {
    fn default() -> Self {
        Self {
            rules: Self::default_rules(),
            severity_overrides: FxHashMap::default(),
            shell: ShellDialect::Unknown,
            ambient_shell_options: AmbientShellOptions::default(),
            analyzed_paths: None,
            per_file_ignores: Arc::new(CompiledPerFileIgnoreList::default()),
            report_environment_style_names: false,
            resolve_source_closure: true,
            rule_options: LinterRuleOptions::default(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PerFileIgnore {
    pattern: String,
    rules: RuleSet,
}

impl PerFileIgnore {
    pub fn new(pattern: impl Into<String>, rules: RuleSet) -> Self {
        Self {
            pattern: pattern.into(),
            rules,
        }
    }

    pub fn pattern(&self) -> &str {
        &self.pattern
    }

    pub const fn rules(&self) -> RuleSet {
        self.rules
    }
}

#[derive(Debug, Clone, Default)]
pub struct CompiledPerFileIgnoreList {
    project_root: PathBuf,
    entries: Vec<CompiledPerFileIgnore>,
}

impl PartialEq for CompiledPerFileIgnoreList {
    fn eq(&self, other: &Self) -> bool {
        self.project_root == other.project_root && self.entries == other.entries
    }
}

impl Eq for CompiledPerFileIgnoreList {}

#[derive(Debug, Clone)]
struct CompiledPerFileIgnore {
    pattern: String,
    basename_matcher: GlobMatcher,
    relative_matcher: GlobMatcher,
    absolute_matcher: GlobMatcher,
    negated: bool,
    rules: RuleSet,
}

impl PartialEq for CompiledPerFileIgnore {
    fn eq(&self, other: &Self) -> bool {
        self.pattern == other.pattern && self.negated == other.negated && self.rules == other.rules
    }
}

impl Eq for CompiledPerFileIgnore {}

impl LinterSettings {
    pub fn for_rule(rule: Rule) -> Self {
        Self {
            rules: RuleSet::from_iter([rule]),
            ..Self::default()
        }
    }

    pub fn for_rules(rules: impl IntoIterator<Item = Rule>) -> Self {
        Self {
            rules: rules.into_iter().collect(),
            ..Self::default()
        }
    }

    pub fn default_rules() -> RuleSet {
        Rule::iter()
            .filter(|rule| !matches!(rule.category(), Category::Style))
            .collect::<RuleSet>()
            .subtract(&default_disabled_non_style_rules())
    }

    pub fn from_selectors(select: &[RuleSelector], ignore: &[RuleSelector]) -> Self {
        let mut rules = RuleSet::EMPTY;
        for selector in select {
            rules = rules.union(&selector.into_rule_set());
        }
        for selector in ignore {
            rules = rules.subtract(&selector.into_rule_set());
        }

        Self {
            rules,
            ..Self::default()
        }
    }

    pub fn with_shell(mut self, shell: ShellDialect) -> Self {
        self.shell = shell;
        self
    }

    pub fn with_ambient_shell_options(
        mut self,
        ambient_shell_options: AmbientShellOptions,
    ) -> Self {
        self.ambient_shell_options = ambient_shell_options;
        self
    }

    pub fn analyzed_path_set(paths: impl IntoIterator<Item = PathBuf>) -> Arc<FxHashSet<PathBuf>> {
        Arc::new(
            paths
                .into_iter()
                .map(|path| std::fs::canonicalize(&path).unwrap_or(path))
                .collect(),
        )
    }

    pub fn with_analyzed_path_set(mut self, paths: Arc<FxHashSet<PathBuf>>) -> Self {
        self.analyzed_paths = Some(paths);
        self
    }

    pub fn with_analyzed_paths(self, paths: impl IntoIterator<Item = PathBuf>) -> Self {
        self.with_analyzed_path_set(Self::analyzed_path_set(paths))
    }

    pub fn with_per_file_ignores(mut self, per_file_ignores: CompiledPerFileIgnoreList) -> Self {
        self.per_file_ignores = Arc::new(per_file_ignores);
        self
    }

    pub fn with_c001_treat_indirect_expansion_targets_as_used(mut self, value: bool) -> Self {
        self.rule_options
            .c001
            .treat_indirect_expansion_targets_as_used = value;
        self
    }

    pub fn with_resolve_source_closure(mut self, value: bool) -> Self {
        self.resolve_source_closure = value;
        self
    }

    pub fn with_c063_report_unreached_nested_definitions(mut self, value: bool) -> Self {
        self.rule_options.c063.report_unreached_nested_definitions = value;
        self
    }

    pub fn per_file_ignored_rules(&self, path: Option<&Path>) -> RuleSet {
        path.map_or(RuleSet::EMPTY, |path| {
            self.per_file_ignores.ignored_rules(path)
        })
    }
}

fn default_disabled_non_style_rules() -> RuleSet {
    DEFAULT_DISABLED_NON_STYLE_RULES.iter().copied().collect()
}

impl CompiledPerFileIgnoreList {
    pub fn resolve(
        project_root: impl Into<PathBuf>,
        per_file_ignores: impl IntoIterator<Item = PerFileIgnore>,
    ) -> Result<Self> {
        let project_root = project_root.into();
        let entries = per_file_ignores
            .into_iter()
            .map(|per_file_ignore| {
                let mut pattern = per_file_ignore.pattern().to_owned();
                let negated = pattern.starts_with('!');
                if negated {
                    pattern.drain(..1);
                }

                let basename_matcher = Glob::new(&pattern)
                    .with_context(|| format!("invalid glob {:?}", per_file_ignore.pattern()))?
                    .compile_matcher();
                let relative_matcher = Glob::new(&pattern)
                    .with_context(|| format!("invalid glob {:?}", per_file_ignore.pattern()))?
                    .compile_matcher();
                let absolute_matcher = Glob::new(&pattern)
                    .with_context(|| format!("invalid glob {:?}", per_file_ignore.pattern()))?
                    .compile_matcher();

                Ok(CompiledPerFileIgnore {
                    pattern: per_file_ignore.pattern().to_owned(),
                    basename_matcher,
                    relative_matcher,
                    absolute_matcher,
                    negated,
                    rules: per_file_ignore.rules(),
                })
            })
            .collect::<Result<Vec<_>>>()?;

        Ok(Self {
            project_root,
            entries,
        })
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn ignored_rules(&self, path: &Path) -> RuleSet {
        let relative_path = path.strip_prefix(&self.project_root).unwrap_or(path);
        let file_name = relative_path.file_name().or_else(|| path.file_name());
        let Some(file_name) = file_name else {
            return RuleSet::EMPTY;
        };

        self.entries.iter().fold(RuleSet::EMPTY, |ignored, entry| {
            let matches = entry.basename_matcher.is_match(file_name)
                || entry.relative_matcher.is_match(relative_path)
                || matches_absolute_path(&entry.absolute_matcher, path);
            let applies = if entry.negated { !matches } else { matches };

            if applies {
                ignored.union(&entry.rules)
            } else {
                ignored
            }
        })
    }
}

fn matches_absolute_path(matcher: &GlobMatcher, path: &Path) -> bool {
    matcher.is_match(path)
        || normalized_absolute_match_path(path)
            .as_deref()
            .is_some_and(|normalized| matcher.is_match(normalized))
}

fn normalized_absolute_match_path(path: &Path) -> Option<PathBuf> {
    let path = path.to_string_lossy();

    if let Some(stripped) = path.strip_prefix(r"\\?\UNC\") {
        return Some(PathBuf::from(format!(r"\\{stripped}")));
    }

    path.strip_prefix(r"\\?\").map(PathBuf::from)
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use tempfile::tempdir;

    use super::*;
    use crate::RuleSet;

    #[test]
    fn default_rules_exclude_all_style_rules() {
        let defaults = LinterSettings::default_rules();

        for rule in Rule::iter().filter(|rule| matches!(rule.category(), Category::Style)) {
            assert!(
                !defaults.contains(rule),
                "{rule:?} should be disabled by default"
            );
        }
    }

    #[test]
    fn default_rules_include_non_style_rules() {
        let defaults = LinterSettings::default_rules();

        assert!(defaults.contains(Rule::UndefinedVariable));
        assert!(defaults.contains(Rule::ConstantCaseSubject));
        assert!(defaults.contains(Rule::RmGlobOnVariablePath));
        assert!(!defaults.contains(Rule::AmpersandSemicolon));
    }

    #[test]
    fn default_rules_exclude_verified_default_disabled_non_style_rules() {
        let defaults = LinterSettings::default_rules();

        for rule in DEFAULT_DISABLED_NON_STYLE_RULES {
            assert!(
                !defaults.contains(*rule),
                "{rule:?} should be excluded from the native default baseline"
            );
            assert!(
                !matches!(rule.category(), Category::Style),
                "{rule:?} must stay in the non-style default-disabled set"
            );
        }
    }

    #[test]
    fn with_analyzed_path_set_reuses_shared_set() {
        let tempdir = tempdir().unwrap();
        let script_path = tempdir.path().join("script.sh");
        std::fs::write(&script_path, "echo hi\n").unwrap();

        let analyzed_paths = LinterSettings::analyzed_path_set([script_path.clone()]);
        let settings =
            LinterSettings::default().with_analyzed_path_set(Arc::clone(&analyzed_paths));

        let stored = settings.analyzed_paths.as_ref().unwrap();
        assert!(Arc::ptr_eq(stored, &analyzed_paths));
        assert!(stored.contains(&std::fs::canonicalize(script_path).unwrap()));
    }

    #[test]
    fn matches_absolute_per_file_ignore_patterns() {
        let tempdir = tempdir().unwrap();
        let project_root = tempdir.path().to_path_buf();
        let script_path = project_root.join("nested").join("script.sh");
        let absolute_pattern = script_path
            .parent()
            .unwrap()
            .join("*.sh")
            .to_string_lossy()
            .into_owned();
        let per_file_ignores = CompiledPerFileIgnoreList::resolve(
            project_root,
            [PerFileIgnore::new(
                absolute_pattern,
                RuleSet::from_iter([Rule::UnusedAssignment]),
            )],
        )
        .unwrap();

        let ignored_rules = per_file_ignores.ignored_rules(&script_path);

        assert!(ignored_rules.contains(Rule::UnusedAssignment));
    }

    #[test]
    fn strips_windows_verbatim_disk_prefixes_for_absolute_matching() {
        assert_eq!(
            normalized_absolute_match_path(Path::new(r"\\?\C:\repo\nested\script.sh")),
            Some(PathBuf::from(r"C:\repo\nested\script.sh"))
        );
    }

    #[test]
    fn strips_windows_verbatim_unc_prefixes_for_absolute_matching() {
        assert_eq!(
            normalized_absolute_match_path(Path::new(r"\\?\UNC\server\share\script.sh")),
            Some(PathBuf::from(r"\\server\share\script.sh"))
        );
    }
}