harper_core/linting/
widely_accepted.rs1use crate::{
2 Token,
3 linting::{Lint, LintKind, PatternLinter, Suggestion},
4 patterns::{Pattern, SequencePattern, Word, WordSet},
5};
6
7pub struct WidelyAccepted {
8 pattern: SequencePattern,
9}
10
11impl Default for WidelyAccepted {
12 fn default() -> Self {
13 let pattern = SequencePattern::default()
14 .then(Word::new("wide"))
15 .then_whitespace()
16 .then(WordSet::new(&["accepted", "acceptable", "used"]));
17
18 Self { pattern }
19 }
20}
21
22impl PatternLinter for WidelyAccepted {
23 fn pattern(&self) -> &dyn Pattern {
24 &self.pattern
25 }
26
27 fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
28 let wide_token = matched_tokens.first()?;
30 let wide_chars = wide_token.span.get_content(source);
31
32 Some(Lint {
33 span: wide_token.span,
34 lint_kind: LintKind::Miscellaneous,
35 message: "Use the adverb `widely` in this context. For example, `widely accepted` or `widely used` is standard usage."
36 .to_owned(),
37 suggestions: vec![Suggestion::replace_with_match_case_str("widely", wide_chars)],
38 priority: 31,
39 })
40 }
41
42 fn description(&self) -> &str {
43 "Flags `wide accepted`, `wide acceptable`, or `wide used` and recommends switching `wide` to the adverb `widely`."
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::WidelyAccepted;
50 use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
51
52 #[test]
53 fn wide_accepted_lowercase() {
54 assert_suggestion_result(
55 "It is wide accepted that exercise improves health.",
56 WidelyAccepted::default(),
57 "It is widely accepted that exercise improves health.",
58 );
59 }
60
61 #[test]
62 fn wide_acceptable_mixed_case() {
63 assert_suggestion_result(
64 "Wide acceptable standards are used in the design.",
65 WidelyAccepted::default(),
66 "Widely acceptable standards are used in the design.",
67 );
68 }
69
70 #[test]
71 fn widely_already_correct() {
72 assert_lint_count(
73 "It is widely accepted that sunlight is beneficial in moderation.",
74 WidelyAccepted::default(),
75 0,
76 );
77 }
78
79 #[test]
80 fn no_false_positive() {
81 assert_lint_count(
82 "The house had wide open windows during the renovation.",
83 WidelyAccepted::default(),
84 0,
85 );
86 }
87
88 #[test]
89 fn wide_accepted_in_long_text() {
90 assert_suggestion_result(
91 "This is an example paragraph, and it is wide accepted that these changes will improve performance. In fact, widely used frameworks have already adopted them.",
92 WidelyAccepted::default(),
93 "This is an example paragraph, and it is widely accepted that these changes will improve performance. In fact, widely used frameworks have already adopted them.",
94 );
95 }
96
97 #[test]
98 fn wide_twice_in_one_sentence() {
99 assert_suggestion_result(
100 "It is wide accepted and wide used by many professionals.",
101 WidelyAccepted::default(),
102 "It is widely accepted and widely used by many professionals.",
103 );
104 }
105}