Skip to content

Commit

Permalink
feat(Automattic#306): configurable lint severity by linter
Browse files Browse the repository at this point in the history
  • Loading branch information
grantlemons committed Dec 11, 2024
1 parent df33c5f commit 08b6490
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 126 deletions.
2 changes: 1 addition & 1 deletion harper-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() -> anyhow::Result<()> {
let (doc, source) = load_file(&file)?;

let mut linter = LintGroup::new(LintGroupConfig::default(), FstDictionary::curated());
let mut lints = linter.lint(&doc);
let mut lints = linter.lint(&doc, None);

if count {
println!("{}", lints.len());
Expand Down
2 changes: 1 addition & 1 deletion harper-comments/tests/language_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ macro_rules! create_test {
LintGroupConfig::default(),
dict
);
let lints = linter.lint(&document);
let lints = linter.lint(&document, None);

dbg!(&lints);
assert_eq!(lints.len(), $correct_expected);
Expand Down
4 changes: 2 additions & 2 deletions harper-core/benches/parse_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn lint_demo(c: &mut Criterion) {
let document = Document::new_markdown_curated(black_box(DEMO));

c.bench_function("lint_demo", |b| {
b.iter(|| lint_set.lint(&document));
b.iter(|| lint_set.lint(&document, None));
});
}

Expand All @@ -26,7 +26,7 @@ fn lint_demo_uncached(c: &mut Criterion) {
let dictionary = FstDictionary::curated();
let mut lint_set = LintGroup::new(LintGroupConfig::default(), dictionary.clone());
let document = Document::new_markdown(black_box(DEMO), &dictionary);
lint_set.lint(&document)
lint_set.lint(&document, None)
})
});
}
Expand Down
14 changes: 10 additions & 4 deletions harper-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn remove_overlaps(lints: &mut Vec<Lint>) {
#[cfg(test)]
mod tests {
use crate::{
linting::{LintGroup, LintGroupConfig, Linter},
linting::{LintConfig, LintGroup, LintGroupConfig, Linter},
remove_overlaps, Document, FstDictionary,
};

Expand All @@ -74,13 +74,19 @@ mod tests {
let doc = Document::new_plain_english_curated("Ths tet");

let lint_config = LintGroupConfig {
spell_check: Some(true),
spaces: Some(true),
spell_check: Some(LintConfig {
enabled: Some(true),
..LintConfig::default()
}),
spaces: Some(LintConfig {
enabled: Some(true),
..LintConfig::default()
}),
..LintGroupConfig::none()
};
let mut linter = LintGroup::new(lint_config, FstDictionary::curated());

let mut lints = linter.lint(&doc);
let mut lints = linter.lint(&doc, None);

dbg!(&lints);
remove_overlaps(&mut lints);
Expand Down
5 changes: 4 additions & 1 deletion harper-core/src/linting/an_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use itertools::Itertools;
use crate::linting::{Lint, LintKind, Linter, Suggestion};
use crate::{Document, TokenStringExt};

use super::LintSeverity;

#[derive(Debug, Default)]
pub struct AnA;

impl Linter for AnA {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
fn lint(&mut self, document: &Document, severity: Option<LintSeverity>) -> Vec<Lint> {
let mut lints = Vec::new();

for chunk in document.iter_chunks() {
Expand Down Expand Up @@ -55,6 +57,7 @@ impl Linter for AnA {
suggestions: vec![Suggestion::ReplaceWith(replacement)],
message: "Incorrect indefinite article.".to_string(),
priority: 31,
severity,
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions harper-core/src/linting/avoid_curses.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{Lint, LintKind, Linter};
use super::{Lint, LintKind, LintSeverity, Linter};
use crate::{Document, TokenStringExt};

#[derive(Debug, Default)]
pub struct AvoidCurses;

impl Linter for AvoidCurses {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
fn lint(&mut self, document: &Document, severity: Option<LintSeverity>) -> Vec<Lint> {
document
.iter_words()
.filter(|t| t.kind.is_swear())
Expand All @@ -15,6 +15,7 @@ impl Linter for AvoidCurses {
suggestions: vec![],
message: "Try to avoid offensive language.".to_string(),
priority: 63,
severity,
})
.collect()
}
Expand Down
10 changes: 8 additions & 2 deletions harper-core/src/linting/boring_words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
Token, TokenStringExt,
};

use super::{Lint, LintKind, PatternLinter};
use super::{Lint, LintKind, LintSeverity, PatternLinter};

pub struct BoringWords {
pattern: Box<dyn Pattern>,
Expand All @@ -27,7 +27,12 @@ impl PatternLinter for BoringWords {
self.pattern.as_ref()
}

fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
fn match_to_lint(
&self,
matched_tokens: &[Token],
source: &[char],
severity: Option<LintSeverity>,
) -> Lint {
let matched_word = matched_tokens.span().unwrap().get_content_string(source);

Lint {
Expand All @@ -39,6 +44,7 @@ impl PatternLinter for BoringWords {
matched_word
),
priority: 127,
severity,
}
}
}
5 changes: 3 additions & 2 deletions harper-core/src/linting/correct_number_suffix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Lint, LintKind, Linter, Suggestion};
use super::{Lint, LintKind, LintSeverity, Linter, Suggestion};
use crate::token::{NumberSuffix, TokenStringExt};
use crate::{Document, Span, TokenKind};

Expand All @@ -7,7 +7,7 @@ use crate::{Document, Span, TokenKind};
pub struct CorrectNumberSuffix;

impl Linter for CorrectNumberSuffix {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
fn lint(&mut self, document: &Document, severity: Option<LintSeverity>) -> Vec<Lint> {
let mut output = Vec::new();

for number_tok in document.iter_numbers() {
Expand All @@ -22,6 +22,7 @@ impl Linter for CorrectNumberSuffix {
message: "This number needs a different suffix to sound right."
.to_string(),
suggestions: vec![Suggestion::ReplaceWith(correct_suffix.to_chars())],
severity,
..Default::default()
})
}
Expand Down
10 changes: 8 additions & 2 deletions harper-core/src/linting/dot_initialisms.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hashbrown::HashMap;

use super::{Lint, LintKind, PatternLinter, Suggestion};
use super::{Lint, LintKind, LintSeverity, PatternLinter, Suggestion};
use crate::patterns::{Pattern, SequencePattern, WordPatternGroup};
use crate::{Token, TokenStringExt};

Expand Down Expand Up @@ -37,7 +37,12 @@ impl PatternLinter for DotInitialisms {
self.pattern.as_ref()
}

fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
fn match_to_lint(
&self,
matched_tokens: &[Token],
source: &[char],
severity: Option<LintSeverity>,
) -> Lint {
let found_word_tok = matched_tokens.first().unwrap();
let found_word = found_word_tok.span.get_content_string(source);

Expand All @@ -49,6 +54,7 @@ impl PatternLinter for DotInitialisms {
suggestions: vec![Suggestion::ReplaceWith(correction.chars().collect())],
message: "Initialisms should have dot-separated letters.".to_owned(),
priority: 63,
severity,
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions harper-core/src/linting/ellipsis_length.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use itertools::Itertools;

use super::{Lint, LintKind, Linter, Suggestion};
use super::{Lint, LintKind, LintSeverity, Linter, Suggestion};
use crate::TokenStringExt;

/// A linter that checks that an ellipsis doesn't contain too many periods (or
Expand All @@ -9,7 +9,11 @@ use crate::TokenStringExt;
pub struct EllipsisLength;

impl Linter for EllipsisLength {
fn lint(&mut self, document: &crate::Document) -> Vec<super::Lint> {
fn lint(
&mut self,
document: &crate::Document,
severity: Option<LintSeverity>,
) -> Vec<super::Lint> {
let mut lints = Vec::new();

for tok in document.iter_ellipsiss() {
Expand All @@ -29,6 +33,7 @@ impl Linter for EllipsisLength {
suggestions: vec![Suggestion::ReplaceWith(vec!['.', '.', '.'])],
message: "Horizontal ellipsis must have 3 dots.".to_string(),
priority: 31,
severity,
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions harper-core/src/linting/linking_verbs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Lint, LintKind, Linter};
use super::{Lint, LintKind, LintSeverity, Linter};
use crate::token::TokenStringExt;
use crate::Document;

Expand All @@ -7,7 +7,7 @@ use crate::Document;
pub struct LinkingVerbs;

impl Linter for LinkingVerbs {
fn lint(&mut self, document: &Document) -> Vec<Lint> {
fn lint(&mut self, document: &Document, severity: Option<LintSeverity>) -> Vec<Lint> {
let mut output = Vec::new();

for chunk in document.iter_chunks() {
Expand All @@ -25,6 +25,7 @@ impl Linter for LinkingVerbs {
"Linking verbs like “{}” must be preceded by a noun.",
linking_verb_text
),
severity,
..Default::default()
})
}
Expand Down
4 changes: 4 additions & 0 deletions harper-core/src/linting/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize};

use crate::Span;

use super::LintSeverity;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lint {
pub span: Span,
Expand All @@ -14,6 +16,7 @@ pub struct Lint {
/// A numerical value for the importance of a lint.
/// Lower = more important.
pub priority: u8,
pub severity: Option<LintSeverity>,
}

impl Default for Lint {
Expand All @@ -24,6 +27,7 @@ impl Default for Lint {
suggestions: Default::default(),
message: Default::default(),
priority: 127,
severity: None,
}
}
}
Expand Down
Loading

0 comments on commit 08b6490

Please sign in to comment.