Skip to content

Commit

Permalink
Support omnilinter: ignore markers (fixes #34)
Browse files Browse the repository at this point in the history
Omnilinter won't match lines with these markers, allowing to tune its
behavior from the code being checked.
  • Loading branch information
AMDmi3 committed Feb 12, 2024
1 parent 9e3b739 commit 1e5c678
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions .omnilinter.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ rules:
files: "src/*.rs"
nomatch: "^//!"

- title: TODO items
- title: TODO items # omnilinter: ignore
files: "*.rs *.yml .omnilinter.conf"
match: TODO
- title: FIXME items
match: TODO # omnilinter: ignore
- title: FIXME items # omnilinter: ignore
files: "*.rs *.yml .omnilinter.conf"
match: FIXME
match: FIXME # omnilinter: ignore

- title: omniparser does not exist # I tend to make this misspelling occasionally for some reason
files: "*.rs /[A-Z]*"
Expand Down
4 changes: 3 additions & 1 deletion src/applier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::fs;
use std::path::Path;
use walkdir::WalkDir;

const IGNORE_MARKER: &str = "omnilinter: ignore";

pub struct ApplierOptions {
pub required_tags: HashSet<String>,
pub ignored_tags: HashSet<String>,
Expand Down Expand Up @@ -45,7 +47,7 @@ fn apply_rule_to_path(loc: &FileMatchLocation, rule: &Rule, reporter: &mut dyn R

if let Some(regex) = &rule.regex {
for (nline, line) in text.lines().enumerate() {
if regex.is_match(line) {
if regex.is_match(line) && !line.contains(IGNORE_MARKER) {
reporter.report(
&MatchLocation::Line(LineMatchLocation::from_file(loc, nline + 1)),
&rule.title,
Expand Down
14 changes: 14 additions & 0 deletions tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,17 @@ fn tags() {
)
.assert_matches(vec![]);
}

#[test]
fn ignore_marker() {
TestCase::new()
.add_file("a.py", "foo\nbar # omnilinter: ignore")
.run_with_rule(
"
- title: test
files: '*.py'
match: 'foo|bar'
",
)
.assert_matches(vec!["a.py:1"]);
}

0 comments on commit 1e5c678

Please sign in to comment.