Skip to content

Commit eaf7a9f

Browse files
committed
feat(Automattic#306): configurable lint severity by linter
1 parent 77903b0 commit eaf7a9f

35 files changed

+235
-126
lines changed

harper-cli/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn main() -> anyhow::Result<()> {
3838
let (doc, source) = load_file(&file)?;
3939

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

4343
if count {
4444
println!("{}", lints.len());

harper-comments/tests/language_support.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ macro_rules! create_test {
2828
LintGroupConfig::default(),
2929
dict
3030
);
31-
let lints = linter.lint(&document);
31+
let lints = linter.lint(&document, None);
3232

3333
dbg!(&lints);
3434
assert_eq!(lints.len(), $correct_expected);

harper-core/benches/parse_demo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn lint_demo(c: &mut Criterion) {
1616
let document = Document::new_markdown_curated(black_box(DEMO));
1717

1818
c.bench_function("lint_demo", |b| {
19-
b.iter(|| lint_set.lint(&document));
19+
b.iter(|| lint_set.lint(&document, None));
2020
});
2121
}
2222

@@ -26,7 +26,7 @@ fn lint_demo_uncached(c: &mut Criterion) {
2626
let dictionary = FstDictionary::curated();
2727
let mut lint_set = LintGroup::new(LintGroupConfig::default(), dictionary.clone());
2828
let document = Document::new_markdown(black_box(DEMO), &dictionary);
29-
lint_set.lint(&document)
29+
lint_set.lint(&document, None)
3030
})
3131
});
3232
}

harper-core/src/lib.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn remove_overlaps(lints: &mut Vec<Lint>) {
6565
#[cfg(test)]
6666
mod tests {
6767
use crate::{
68-
linting::{LintGroup, LintGroupConfig, Linter},
68+
linting::{LintConfig, LintGroup, LintGroupConfig, Linter},
6969
remove_overlaps, Document, FstDictionary,
7070
};
7171

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

7676
let lint_config = LintGroupConfig {
77-
spell_check: Some(true),
78-
spaces: Some(true),
77+
spell_check: Some(LintConfig {
78+
enabled: Some(true),
79+
..LintConfig::default()
80+
}),
81+
spaces: Some(LintConfig {
82+
enabled: Some(true),
83+
..LintConfig::default()
84+
}),
7985
..LintGroupConfig::none()
8086
};
8187
let mut linter = LintGroup::new(lint_config, FstDictionary::curated());
8288

83-
let mut lints = linter.lint(&doc);
89+
let mut lints = linter.lint(&doc, None);
8490

8591
dbg!(&lints);
8692
remove_overlaps(&mut lints);

harper-core/src/linting/an_a.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ use itertools::Itertools;
55
use crate::linting::{Lint, LintKind, Linter, Suggestion};
66
use crate::{Document, TokenStringExt};
77

8+
use super::LintSeverity;
9+
810
#[derive(Debug, Default)]
911
pub struct AnA;
1012

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

1517
for chunk in document.iter_chunks() {
@@ -55,6 +57,7 @@ impl Linter for AnA {
5557
suggestions: vec![Suggestion::ReplaceWith(replacement)],
5658
message: "Incorrect indefinite article.".to_string(),
5759
priority: 31,
60+
severity,
5861
})
5962
}
6063
}

harper-core/src/linting/avoid_curses.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use super::{Lint, LintKind, Linter};
1+
use super::{Lint, LintKind, LintSeverity, Linter};
22
use crate::{Document, TokenStringExt};
33

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

77
impl Linter for AvoidCurses {
8-
fn lint(&mut self, document: &Document) -> Vec<Lint> {
8+
fn lint(&mut self, document: &Document, severity: Option<LintSeverity>) -> Vec<Lint> {
99
document
1010
.iter_words()
1111
.filter(|t| t.kind.is_swear())
@@ -15,6 +15,7 @@ impl Linter for AvoidCurses {
1515
suggestions: vec![],
1616
message: "Try to avoid offensive language.".to_string(),
1717
priority: 63,
18+
severity,
1819
})
1920
.collect()
2021
}

harper-core/src/linting/boring_words.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
Token, TokenStringExt,
44
};
55

6-
use super::{Lint, LintKind, PatternLinter};
6+
use super::{Lint, LintKind, LintSeverity, PatternLinter};
77

88
pub struct BoringWords {
99
pattern: Box<dyn Pattern>,
@@ -27,7 +27,12 @@ impl PatternLinter for BoringWords {
2727
self.pattern.as_ref()
2828
}
2929

30-
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
30+
fn match_to_lint(
31+
&self,
32+
matched_tokens: &[Token],
33+
source: &[char],
34+
severity: Option<LintSeverity>,
35+
) -> Lint {
3136
let matched_word = matched_tokens.span().unwrap().get_content_string(source);
3237

3338
Lint {
@@ -39,6 +44,7 @@ impl PatternLinter for BoringWords {
3944
matched_word
4045
),
4146
priority: 127,
47+
severity,
4248
}
4349
}
4450
}

harper-core/src/linting/correct_number_suffix.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Lint, LintKind, Linter, Suggestion};
1+
use super::{Lint, LintKind, LintSeverity, Linter, Suggestion};
22
use crate::token::{NumberSuffix, TokenStringExt};
33
use crate::{Document, Span, TokenKind};
44

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

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

1313
for number_tok in document.iter_numbers() {
@@ -22,6 +22,7 @@ impl Linter for CorrectNumberSuffix {
2222
message: "This number needs a different suffix to sound right."
2323
.to_string(),
2424
suggestions: vec![Suggestion::ReplaceWith(correct_suffix.to_chars())],
25+
severity,
2526
..Default::default()
2627
})
2728
}

harper-core/src/linting/dot_initialisms.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hashbrown::HashMap;
22

3-
use super::{Lint, LintKind, PatternLinter, Suggestion};
3+
use super::{Lint, LintKind, LintSeverity, PatternLinter, Suggestion};
44
use crate::patterns::{Pattern, SequencePattern, WordPatternGroup};
55
use crate::{Token, TokenStringExt};
66

@@ -37,7 +37,12 @@ impl PatternLinter for DotInitialisms {
3737
self.pattern.as_ref()
3838
}
3939

40-
fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
40+
fn match_to_lint(
41+
&self,
42+
matched_tokens: &[Token],
43+
source: &[char],
44+
severity: Option<LintSeverity>,
45+
) -> Lint {
4146
let found_word_tok = matched_tokens.first().unwrap();
4247
let found_word = found_word_tok.span.get_content_string(source);
4348

@@ -49,6 +54,7 @@ impl PatternLinter for DotInitialisms {
4954
suggestions: vec![Suggestion::ReplaceWith(correction.chars().collect())],
5055
message: "Initialisms should have dot-separated letters.".to_owned(),
5156
priority: 63,
57+
severity,
5258
}
5359
}
5460
}

harper-core/src/linting/ellipsis_length.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use itertools::Itertools;
22

3-
use super::{Lint, LintKind, Linter, Suggestion};
3+
use super::{Lint, LintKind, LintSeverity, Linter, Suggestion};
44
use crate::TokenStringExt;
55

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

1111
impl Linter for EllipsisLength {
12-
fn lint(&mut self, document: &crate::Document) -> Vec<super::Lint> {
12+
fn lint(
13+
&mut self,
14+
document: &crate::Document,
15+
severity: Option<LintSeverity>,
16+
) -> Vec<super::Lint> {
1317
let mut lints = Vec::new();
1418

1519
for tok in document.iter_ellipsiss() {
@@ -29,6 +33,7 @@ impl Linter for EllipsisLength {
2933
suggestions: vec![Suggestion::ReplaceWith(vec!['.', '.', '.'])],
3034
message: "Horizontal ellipsis must have 3 dots.".to_string(),
3135
priority: 31,
36+
severity,
3237
})
3338
}
3439
}

harper-core/src/linting/linking_verbs.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Lint, LintKind, Linter};
1+
use super::{Lint, LintKind, LintSeverity, Linter};
22
use crate::token::TokenStringExt;
33
use crate::Document;
44

@@ -7,7 +7,7 @@ use crate::Document;
77
pub struct LinkingVerbs;
88

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

1313
for chunk in document.iter_chunks() {
@@ -25,6 +25,7 @@ impl Linter for LinkingVerbs {
2525
"Linking verbs like “{}” must be preceded by a noun.",
2626
linking_verb_text
2727
),
28+
severity,
2829
..Default::default()
2930
})
3031
}

harper-core/src/linting/lint.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize};
55

66
use crate::Span;
77

8+
use super::LintSeverity;
9+
810
#[derive(Debug, Clone, Serialize, Deserialize)]
911
pub struct Lint {
1012
pub span: Span,
@@ -14,6 +16,7 @@ pub struct Lint {
1416
/// A numerical value for the importance of a lint.
1517
/// Lower = more important.
1618
pub priority: u8,
19+
pub severity: Option<LintSeverity>,
1720
}
1821

1922
impl Default for Lint {
@@ -24,6 +27,7 @@ impl Default for Lint {
2427
suggestions: Default::default(),
2528
message: Default::default(),
2629
priority: 127,
30+
severity: None,
2731
}
2832
}
2933
}

0 commit comments

Comments
 (0)