Skip to main content

uira_comment_checker/filters/
directive.rs

1use super::CommentFilter;
2use crate::models::CommentInfo;
3
4const DIRECTIVE_PREFIXES: &[&str] = &[
5    "type:",
6    "noqa",
7    "pyright:",
8    "ruff:",
9    "mypy:",
10    "pylint:",
11    "flake8:",
12    "pyre:",
13    "pytype:",
14    "eslint-disable",
15    "eslint-ignore",
16    "prettier-ignore",
17    "ts-ignore",
18    "ts-expect-error",
19    "clippy:",
20    "allow",
21    "deny",
22    "warn",
23    "forbid",
24];
25
26pub struct DirectiveFilter;
27
28impl CommentFilter for DirectiveFilter {
29    fn should_skip(&self, comment: &CommentInfo) -> bool {
30        let mut normalized = comment.text.trim().to_lowercase();
31
32        for prefix in &["#", "//", "/*", "--"] {
33            if normalized.starts_with(prefix) {
34                normalized = normalized[prefix.len()..].trim().to_string();
35                break;
36            }
37        }
38
39        if normalized.starts_with('@') {
40            normalized = normalized[1..].trim().to_string();
41        }
42
43        DIRECTIVE_PREFIXES.iter().any(|d| normalized.starts_with(d))
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use crate::models::CommentType;
51
52    #[test]
53    fn test_directive_filter() {
54        let filter = DirectiveFilter;
55
56        let ts_ignore = CommentInfo::new(
57            "// @ts-ignore".to_string(),
58            1,
59            "test.ts".to_string(),
60            CommentType::Line,
61        );
62        assert!(filter.should_skip(&ts_ignore));
63
64        let noqa = CommentInfo::new(
65            "# noqa: E501".to_string(),
66            1,
67            "test.py".to_string(),
68            CommentType::Line,
69        );
70        assert!(filter.should_skip(&noqa));
71
72        let clippy = CommentInfo::new(
73            "// clippy::allow".to_string(),
74            1,
75            "test.rs".to_string(),
76            CommentType::Line,
77        );
78        assert!(filter.should_skip(&clippy));
79
80        let normal = CommentInfo::new(
81            "// This is a normal comment".to_string(),
82            1,
83            "test.js".to_string(),
84            CommentType::Line,
85        );
86        assert!(!filter.should_skip(&normal));
87    }
88}