Skip to main content

harper_core/linting/lint_group/
mod.rs

1mod flat_config;
2mod structured_config;
3
4use std::collections::BTreeMap;
5use std::hash::BuildHasher;
6use std::num::NonZero;
7use std::sync::Arc;
8
9use foldhash::quality::RandomState;
10use hashbrown::HashMap;
11use lru::LruCache;
12
13use super::a_part::APart;
14use super::a_while::AWhile;
15use super::addicting::Addicting;
16use super::adjective_double_degree::AdjectiveDoubleDegree;
17use super::adjective_of_a::AdjectiveOfA;
18use super::after_later::AfterLater;
19use super::all_hell_break_loose::AllHellBreakLoose;
20use super::all_intents_and_purposes::AllIntentsAndPurposes;
21use super::allow_to::AllowTo;
22use super::am_in_the_morning::AmInTheMorning;
23use super::amounts_for::AmountsFor;
24use super::an_a::AnA;
25use super::and_the_like::AndTheLike;
26use super::another_thing_coming::AnotherThingComing;
27use super::another_think_coming::AnotherThinkComing;
28use super::apart_from::ApartFrom;
29use super::arrive_to::ArriveTo;
30use super::ask_no_preposition::AskNoPreposition;
31use super::aspire_to::AspireTo;
32use super::avoid_curses::AvoidCurses;
33use super::back_in_the_day::BackInTheDay;
34use super::be_allowed::BeAllowed;
35use super::behind_the_scenes::BehindTheScenes;
36use super::best_of_all_time::BestOfAllTime;
37use super::boring_words::BoringWords;
38use super::bought::Bought;
39use super::brand_brandish::BrandBrandish;
40use super::by_accident::ByAccident;
41use super::cant::Cant;
42use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
43use super::cautionary_tale::CautionaryTale;
44use super::change_tack::ChangeTack;
45use super::chock_full::ChockFull;
46use super::comma_fixes::CommaFixes;
47use super::compound_nouns::CompoundNouns;
48use super::compound_subject_i::CompoundSubjectI;
49use super::confident::Confident;
50use super::correct_number_suffix::CorrectNumberSuffix;
51use super::criteria_phenomena::CriteriaPhenomena;
52use super::cure_for::CureFor;
53use super::currency_placement::CurrencyPlacement;
54use super::damages::Damages;
55use super::day_and_age::DayAndAge;
56use super::despite_it_is::DespiteItIs;
57use super::despite_of::DespiteOf;
58use super::did_past::DidPast;
59use super::didnt::Didnt;
60use super::discourse_markers::DiscourseMarkers;
61use super::disjoint_prefixes::DisjointPrefixes;
62use super::do_mistake::DoMistake;
63use super::dot_initialisms::DotInitialisms;
64use super::double_click::DoubleClick;
65use super::double_modal::DoubleModal;
66use super::ellipsis_length::EllipsisLength;
67use super::else_possessive::ElsePossessive;
68use super::ever_every::EverEvery;
69use super::everyday::Everyday;
70use super::except_of::ExceptOf;
71use super::expand_memory_shorthands::ExpandMemoryShorthands;
72use super::expand_people::ExpandPeople;
73use super::expand_time_shorthands::ExpandTimeShorthands;
74use super::expr_linter::run_on_chunk;
75use super::far_be_it::FarBeIt;
76use super::fascinated_by::FascinatedBy;
77use super::fed_up_with::FedUpWith;
78use super::feel_fell::FeelFell;
79use super::few_units_of_time_ago::FewUnitsOfTimeAgo;
80use super::filler_words::FillerWords;
81use super::find_fine::FindFine;
82use super::first_aid_kit::FirstAidKit;
83use super::flesh_out_vs_full_fledged::FleshOutVsFullFledged;
84use super::for_noun::ForNoun;
85use super::free_predicate::FreePredicate;
86use super::friend_of_me::FriendOfMe;
87use super::go_so_far_as_to::GoSoFarAsTo;
88use super::go_to_war::GoToWar;
89use super::good_at::GoodAt;
90use super::handful::Handful;
91use super::have_pronoun::HavePronoun;
92use super::have_take_a_look::HaveTakeALook;
93use super::hedging::Hedging;
94use super::hello_greeting::HelloGreeting;
95use super::hereby::Hereby;
96use super::hop_hope::HopHope;
97use super::how_to::HowTo;
98use super::hyphenate_number_day::HyphenateNumberDay;
99use super::i_am_agreement::IAmAgreement;
100use super::if_wouldve::IfWouldve;
101use super::in_on_the_cards::InOnTheCards;
102use super::in_time_from_now::InTimeFromNow;
103use super::inflected_verb_after_to::InflectedVerbAfterTo;
104use super::interested_in::InterestedIn;
105use super::it_looks_like_that::ItLooksLikeThat;
106use super::its_contraction::ItsContraction;
107use super::its_possessive::ItsPossessive;
108use super::jealous_of::JealousOf;
109use super::johns_hopkins::JohnsHopkins;
110use super::lead_rise_to::LeadRiseTo;
111use super::left_right_hand::LeftRightHand;
112use super::less_worse::LessWorse;
113use super::let_to_do::LetToDo;
114use super::lets_confusion::LetsConfusion;
115use super::likewise::Likewise;
116use super::long_sentences::LongSentences;
117use super::look_down_ones_nose::LookDownOnesNose;
118use super::looking_forward_to::LookingForwardTo;
119use super::mass_nouns::MassNouns;
120use super::means_a_lot_to::MeansALotTo;
121use super::merge_words::MergeWords;
122use super::missing_preposition::MissingPreposition;
123use super::missing_to::MissingTo;
124use super::misspell::Misspell;
125use super::mixed_bag::MixedBag;
126use super::modal_be_adjective::ModalBeAdjective;
127use super::modal_of::ModalOf;
128use super::modal_seem::ModalSeem;
129use super::months::Months;
130use super::more_adjective::MoreAdjective;
131use super::more_better::MoreBetter;
132use super::most_number::MostNumber;
133use super::most_of_the_times::MostOfTheTimes;
134use super::multiple_frequency_adverbs::MultipleFrequencyAdverbs;
135use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
136use super::nail_on_the_head::NailOnTheHead;
137use super::need_to_noun::NeedToNoun;
138use super::no_french_spaces::NoFrenchSpaces;
139use super::no_longer::NoLonger;
140use super::no_match_for::NoMatchFor;
141use super::no_oxford_comma::NoOxfordComma;
142use super::nobody::Nobody;
143use super::nominal_wants::NominalWants;
144use super::nor_modal_pronoun::NorModalPronoun;
145use super::not_only_inversion::NotOnlyInversion;
146use super::noun_verb_confusion::NounVerbConfusion;
147use super::number_suffix_capitalization::NumberSuffixCapitalization;
148use super::obsess_preposition::ObsessPreposition;
149use super::of_course::OfCourse;
150use super::oldest_in_the_book::OldestInTheBook;
151use super::on_floor::OnFloor;
152use super::once_or_twice::OnceOrTwice;
153use super::one_and_the_same::OneAndTheSame;
154use super::one_of_the_singular::OneOfTheSingular;
155use super::open_the_light::OpenTheLight;
156use super::orthographic_consistency::OrthographicConsistency;
157use super::ought_to_be::OughtToBe;
158use super::out_of_date::OutOfDate;
159use super::oxford_comma::OxfordComma;
160use super::oxymorons::Oxymorons;
161use super::phrasal_verb_as_compound_noun::PhrasalVerbAsCompoundNoun;
162use super::pique_interest::PiqueInterest;
163use super::plural_decades::PluralDecades;
164use super::plural_wrong_word_of_phrase::PluralWrongWordOfPhrase;
165use super::possessive_noun::PossessiveNoun;
166use super::possessive_your::PossessiveYour;
167use super::progressive_needs_be::ProgressiveNeedsBe;
168use super::pronoun_are::PronounAre;
169use super::pronoun_contraction::PronounContraction;
170use super::pronoun_inflection_be::PronounInflectionBe;
171use super::pronoun_knew::PronounKnew;
172use super::pronoun_verb_agreement::PronounVerbAgreement;
173use super::proper_noun_capitalization_linters;
174use super::quantifier_needs_of::QuantifierNeedsOf;
175use super::quantifier_numeral_conflict::QuantifierNumeralConflict;
176use super::quite_quiet::QuiteQuiet;
177use super::quote_spacing::QuoteSpacing;
178use super::reason_for_doing::ReasonForDoing;
179use super::redundant_acronyms::RedundantAcronyms;
180use super::redundant_additive_adverbs::RedundantAdditiveAdverbs;
181use super::redundant_progressive_comparative::RedundantProgressiveComparative;
182use super::regionalisms::Regionalisms;
183use super::regular_irregulars::RegularIrregulars;
184use super::repeated_words::RepeatedWords;
185use super::respond::Respond;
186use super::right_click::RightClick;
187use super::rise_the_ranks::RiseTheRanks;
188use super::roller_skated::RollerSkated;
189use super::safe_to_save::SafeToSave;
190use super::save_to_safe::SaveToSafe;
191use super::sentence_capitalization::SentenceCapitalization;
192use super::shoot_oneself_in_the_foot::ShootOneselfInTheFoot;
193use super::simple_past_to_past_participle::SimplePastToPastParticiple;
194use super::since_duration::SinceDuration;
195use super::single_be::SingleBe;
196use super::sneaked_snuck::SneakedSnuck;
197use super::some_without_article::SomeWithoutArticle;
198use super::something_is::SomethingIs;
199use super::somewhat_something::SomewhatSomething;
200use super::soon_to_be::SoonToBe;
201use super::sought_after::SoughtAfter;
202use super::spaces::Spaces;
203use super::spell_check::SpellCheck;
204use super::spelled_numbers::SpelledNumbers;
205use super::split_words::SplitWords;
206use super::subject_pronoun::SubjectPronoun;
207use super::take_a_look_to::TakeALookTo;
208use super::take_medicine::TakeMedicine;
209use super::that_than::ThatThan;
210use super::that_which::ThatWhich;
211use super::the_how_why::TheHowWhy;
212use super::the_my::TheMy;
213use super::the_point_for::ThePointFor;
214use super::the_proper_noun_possessive::TheProperNounPossessive;
215use super::then_than::ThenThan;
216use super::theres::Theres;
217use super::theses_these::ThesesThese;
218use super::theyre_confusions::TheyreConfusions;
219use super::thing_think::ThingThink;
220use super::this_type_of_thing::ThisTypeOfThing;
221use super::though_thought::ThoughThought;
222use super::thrive_on::ThriveOn;
223use super::throw_away::ThrowAway;
224use super::throw_rubbish::ThrowRubbish;
225use super::to_adverb::ToAdverb;
226use super::to_two_too::ToTwoToo;
227use super::touristic::Touristic;
228use super::transposed_space::TransposedSpace;
229use super::try_ones_hand_at::TryOnesHandAt;
230use super::try_ones_luck::TryOnesLuck;
231use super::unclosed_quotes::UnclosedQuotes;
232use super::update_place_names::UpdatePlaceNames;
233use super::use_ellipsis_character::UseEllipsisCharacter;
234use super::use_title_case::UseTitleCase;
235use super::verb_to_adjective::VerbToAdjective;
236use super::very_unique::VeryUnique;
237use super::vice_versa::ViceVersa;
238use super::vicious_loop::ViciousCircle;
239use super::vicious_loop::ViciousCircleOrCycle;
240use super::vicious_loop::ViciousCycle;
241use super::was_aloud::WasAloud;
242use super::way_too_adjective::WayTooAdjective;
243use super::web_scraping::WebScraping;
244use super::well_educated::WellEducated;
245use super::were_where::WereWhere;
246use super::whereas::Whereas;
247use super::whom_subject_of_verb::WhomSubjectOfVerb;
248use super::widely_accepted::WidelyAccepted;
249use super::will_non_lemma::WillNonLemma;
250use super::win_prize::WinPrize;
251use super::wish_could::WishCould;
252use super::wordpress_dotcom::WordPressDotcom;
253use super::worth_to_do::WorthToDo;
254use super::would_never_have::WouldNeverHave;
255use super::wrong_apostrophe::WrongApostrophe;
256
257use super::{ExprLinter, Lint};
258use super::{HtmlDescriptionLinter, Linter};
259use crate::linting::dashes::Dashes;
260use crate::linting::expr_linter::Chunk;
261use crate::linting::open_compounds::OpenCompounds;
262use crate::linting::{
263    be_adjective_confusions, closed_compounds, initialisms, phrase_set_corrections, weir_rules,
264};
265use crate::spell::Dictionary;
266use crate::{Dialect, Document, Lrc, TokenStringExt};
267
268pub use flat_config::FlatConfig;
269pub use structured_config::{
270    HumanReadableSetting, HumanReadableStructuredConfig, StructuredConfig,
271};
272
273/// A struct for collecting the output of a number of individual [Linter]s.
274/// Each child can be toggled via the public, mutable `Self::config` object.
275pub struct LintGroup {
276    pub config: FlatConfig,
277    /// We use a binary map here so the ordering is stable.
278    linters: BTreeMap<String, Box<dyn Linter>>,
279    /// We use a binary map here so the ordering is stable.
280    chunk_expr_linters: BTreeMap<String, Box<dyn ExprLinter<Unit = Chunk>>>,
281    /// Since [`ExprLinter`]s operate on a chunk-basis, we can store a
282    /// mapping of `Chunk -> Lint` and only re-run the expr linters
283    /// when a chunk changes.
284    ///
285    /// Since the expr linter results also depend on the config, we hash it and pass it as part
286    /// of the key.
287    #[expect(clippy::complexity)]
288    chunk_expr_cache: LruCache<(u64, u64), Lrc<BTreeMap<String, Vec<Lint>>>>,
289    hasher_builder: RandomState,
290    clashing_linter_names: Option<Vec<String>>,
291}
292
293impl LintGroup {
294    // Constructor methods
295
296    pub fn empty() -> Self {
297        Self {
298            config: FlatConfig::default(),
299            linters: BTreeMap::new(),
300            chunk_expr_linters: BTreeMap::new(),
301            chunk_expr_cache: LruCache::new(NonZero::new(1000).unwrap()),
302            hasher_builder: RandomState::default(),
303            clashing_linter_names: None,
304        }
305    }
306
307    /// Check if the group already contains a linter with a given name.
308    pub fn contains_key(&self, name: impl AsRef<str>) -> bool {
309        self.linters.contains_key(name.as_ref())
310            || self.chunk_expr_linters.contains_key(name.as_ref())
311    }
312
313    /// Add a [`Linter`] to the group, returning whether the operation was successful.
314    /// If it returns `false`, it is because a linter with that key already existed in the group.
315    pub fn add(&mut self, name: impl AsRef<str>, linter: impl Linter + 'static) -> bool {
316        if self.contains_key(&name) {
317            if self.clashing_linter_names.is_none() {
318                self.clashing_linter_names = Some(vec![name.as_ref().to_string()]);
319            } else if let Some(clashing_names) = &mut self.clashing_linter_names {
320                clashing_names.push(name.as_ref().to_string());
321            }
322            false
323        } else {
324            self.linters
325                .insert(name.as_ref().to_string(), Box::new(linter));
326            true
327        }
328    }
329
330    /// Add a chunk-based [`ExprLinter`] to the group, returning whether the operation was successful.
331    /// If it returns `false`, it is because a linter with that key already existed in the group.
332    ///
333    /// This function is not significantly different from [`Self::add`], but allows us to take
334    /// advantage of some properties of chunk-based [`ExprLinter`]s for cache optimization.
335    pub fn add_chunk_expr_linter(
336        &mut self,
337        name: impl AsRef<str>,
338        // linter: impl ExprLinter + 'static,
339        linter: impl ExprLinter<Unit = Chunk> + 'static,
340    ) -> bool {
341        if self.contains_key(&name) {
342            if self.clashing_linter_names.is_none() {
343                self.clashing_linter_names = Some(vec![name.as_ref().to_string()]);
344            } else if let Some(clashing_names) = &mut self.clashing_linter_names {
345                clashing_names.push(name.as_ref().to_string());
346            }
347            false
348        } else {
349            self.chunk_expr_linters
350                .insert(name.as_ref().to_string(), Box::new(linter) as _);
351            true
352        }
353    }
354
355    /// Merge the contents of another [`LintGroup`] into this one.
356    pub fn merge_from(&mut self, other: LintGroup) {
357        self.config.merge_from(other.config);
358
359        if let Some((conflicting_key, _)) = other.linters.iter().find(|(k, _)| self.contains_key(k))
360        {
361            if self.clashing_linter_names.is_none() {
362                self.clashing_linter_names = Some(vec![conflicting_key.clone()]);
363            } else if let Some(clashing_names) = &mut self.clashing_linter_names {
364                clashing_names.push(conflicting_key.clone());
365            }
366        }
367        self.linters.extend(other.linters);
368
369        if let Some((conflicting_key, _)) = other
370            .chunk_expr_linters
371            .iter()
372            .find(|(k, _)| self.contains_key(k))
373        {
374            if self.clashing_linter_names.is_none() {
375                self.clashing_linter_names = Some(vec![conflicting_key.clone()]);
376            } else if let Some(clashing_names) = &mut self.clashing_linter_names {
377                clashing_names.push(conflicting_key.clone());
378            }
379        }
380        self.chunk_expr_linters.extend(other.chunk_expr_linters);
381    }
382
383    pub fn iter_keys(&self) -> impl Iterator<Item = &str> {
384        self.linters
385            .keys()
386            .chain(self.chunk_expr_linters.keys())
387            .map(|v| v.as_str())
388    }
389
390    /// Set all contained rules to a specific value.
391    /// Passing `None` will unset that rule, allowing it to assume its default state.
392    pub fn set_all_rules_to(&mut self, enabled: Option<bool>) {
393        let keys = self.iter_keys().map(|v| v.to_string()).collect::<Vec<_>>();
394
395        for key in keys {
396            match enabled {
397                Some(v) => self.config.set_rule_enabled(key, v),
398                None => self.config.unset_rule_enabled(key),
399            }
400        }
401    }
402
403    /// Get map from each contained linter's name to its associated description.
404    pub fn all_descriptions(&self) -> HashMap<&str, &str> {
405        self.linters
406            .iter()
407            .map(|(key, value)| (key.as_str(), value.description()))
408            .chain(
409                self.chunk_expr_linters
410                    .iter()
411                    .map(|(key, value)| (key.as_str(), ExprLinter::description(value))),
412            )
413            .collect()
414    }
415
416    /// Get map from each contained linter's name to its associated description, rendered to HTML.
417    pub fn all_descriptions_html(&self) -> HashMap<&str, String> {
418        self.linters
419            .iter()
420            .map(|(key, value)| (key.as_str(), value.description_html()))
421            .chain(
422                self.chunk_expr_linters
423                    .iter()
424                    .map(|(key, value)| (key.as_str(), value.description_html())),
425            )
426            .collect()
427    }
428
429    /// Swap out [`Self::config`] with another [`FlatConfig`].
430    pub fn with_lint_config(mut self, config: FlatConfig) -> Self {
431        self.config = config;
432        self
433    }
434
435    pub fn new_curated(dictionary: Arc<impl Dictionary + 'static>, dialect: Dialect) -> Self {
436        let mut out = Self::empty();
437
438        /// Add a `Linter` to the group, setting it to be enabled or disabled.
439        macro_rules! insert_struct_rule {
440            ($rule:ident, $default_config:expr) => {
441                out.add(stringify!($rule), $rule::default());
442                out.config
443                    .set_rule_enabled(stringify!($rule), $default_config);
444            };
445        }
446
447        /// Add a `Linter` that requires a `Dictionary` to the group, setting it to be enabled or disabled.
448        macro_rules! insert_struct_rule_with_dict {
449            ($rule:ident, $default_config:expr) => {
450                out.add(stringify!($rule), $rule::new(dictionary.clone()));
451                out.config
452                    .set_rule_enabled(stringify!($rule), $default_config);
453            };
454        }
455
456        /// Add a `Linter` that requires a `Dialect` to the group, setting it to be enabled or disabled.
457        macro_rules! insert_struct_rule_with_dialect {
458            ($rule:ident, $default_config:expr) => {
459                out.add(stringify!($rule), $rule::new(dialect));
460                out.config
461                    .set_rule_enabled(stringify!($rule), $default_config);
462            };
463        }
464
465        /// Add a chunk-based `ExprLinter` to the group, setting it to be enabled or disabled.
466        /// While you _can_ pass an `ExprLinter` to `insert_struct_rule`, using this macro instead
467        /// will allow it to use more aggressive caching strategies.
468        macro_rules! insert_expr_rule {
469            ($rule:ident, $default_config:expr) => {
470                out.add_chunk_expr_linter(stringify!($rule), $rule::default());
471                out.config
472                    .set_rule_enabled(stringify!($rule), $default_config);
473            };
474        }
475
476        /// Add a chunk-based `ExprLinter` that requires a `Dictionary` to the group, setting it to be enabled or disabled.
477        macro_rules! insert_expr_rule_with_dict {
478            ($rule:ident, $default_config:expr) => {
479                out.add_chunk_expr_linter(stringify!($rule), $rule::new(dictionary.clone()));
480                out.config
481                    .set_rule_enabled(stringify!($rule), $default_config);
482            };
483        }
484
485        /// Add a chunk-based `ExprLinter` that requires a `Dialect` to the group, setting it to be enabled or disabled.
486        macro_rules! insert_expr_rule_with_dialect {
487            ($rule:ident, $default_config:expr) => {
488                out.add_chunk_expr_linter(stringify!($rule), $rule::new(dialect));
489                out.config
490                    .set_rule_enabled(stringify!($rule), $default_config);
491            };
492        }
493
494        out.merge_from(weir_rules::lint_group());
495        out.merge_from(phrase_set_corrections::lint_group());
496        out.merge_from(proper_noun_capitalization_linters::lint_group(
497            dictionary.clone(),
498        ));
499        out.merge_from(closed_compounds::lint_group());
500        out.merge_from(initialisms::lint_group());
501        out.merge_from(be_adjective_confusions::lint_group());
502
503        // Add all the more complex rules to the group.
504        // Please maintain alphabetical order.
505        // On *nix you can maintain sort order with `sort -t'(' -k2`
506        insert_expr_rule!(APart, true);
507        insert_expr_rule!(AWhile, true);
508        insert_expr_rule!(Addicting, true);
509        insert_expr_rule!(AdjectiveDoubleDegree, true);
510        insert_struct_rule!(AdjectiveOfA, true);
511        insert_expr_rule!(AfterLater, true);
512        insert_expr_rule!(AllHellBreakLoose, true);
513        insert_expr_rule!(AllIntentsAndPurposes, true);
514        insert_expr_rule!(AllowTo, true);
515        insert_expr_rule!(AmInTheMorning, true);
516        insert_expr_rule!(AmountsFor, true);
517        insert_struct_rule_with_dialect!(AnA, true);
518        insert_expr_rule!(AndTheLike, true);
519        insert_expr_rule!(AnotherThingComing, true);
520        insert_expr_rule!(AnotherThinkComing, false);
521        insert_expr_rule!(ApartFrom, true);
522        insert_expr_rule!(ArriveTo, true);
523        insert_expr_rule!(AskNoPreposition, true);
524        insert_expr_rule!(AvoidCurses, true);
525        insert_expr_rule!(BackInTheDay, true);
526        insert_expr_rule!(BeAllowed, true);
527        insert_expr_rule!(BehindTheScenes, true);
528        insert_struct_rule!(BestOfAllTime, true);
529        insert_expr_rule!(BoringWords, false);
530        insert_expr_rule!(Bought, true);
531        insert_expr_rule!(BrandBrandish, true);
532        insert_expr_rule!(ByAccident, true);
533        insert_expr_rule!(Cant, true);
534        insert_struct_rule!(CapitalizePersonalPronouns, true);
535        insert_expr_rule!(CautionaryTale, true);
536        insert_expr_rule!(ChangeTack, true);
537        insert_expr_rule!(ChockFull, true);
538        insert_struct_rule!(CommaFixes, true);
539        insert_struct_rule!(CompoundNouns, true);
540        insert_expr_rule!(CompoundSubjectI, true);
541        insert_expr_rule!(Confident, true);
542        insert_struct_rule!(CorrectNumberSuffix, true);
543        insert_expr_rule!(CriteriaPhenomena, true);
544        insert_expr_rule!(CureFor, true);
545        insert_struct_rule!(CurrencyPlacement, true);
546        insert_expr_rule!(Dashes, true);
547        insert_expr_rule!(DayAndAge, true);
548        insert_expr_rule!(DespiteItIs, true);
549        insert_expr_rule!(DespiteOf, true);
550        insert_expr_rule_with_dict!(DidPast, true);
551        insert_expr_rule!(Didnt, true);
552        insert_struct_rule!(DiscourseMarkers, true);
553        insert_expr_rule_with_dict!(DisjointPrefixes, true);
554        insert_expr_rule!(DoMistake, true);
555        insert_expr_rule!(DotInitialisms, true);
556        insert_expr_rule!(DoubleClick, true);
557        insert_expr_rule!(DoubleModal, true);
558        insert_struct_rule!(EllipsisLength, true);
559        insert_struct_rule!(UseEllipsisCharacter, true);
560        insert_expr_rule!(ElsePossessive, true);
561        insert_expr_rule!(EverEvery, true);
562        insert_expr_rule!(Everyday, true);
563        insert_expr_rule!(ExceptOf, true);
564        insert_expr_rule!(ExpandMemoryShorthands, true);
565        insert_expr_rule!(ExpandPeople, true);
566        insert_expr_rule!(ExpandTimeShorthands, true);
567        insert_expr_rule!(FarBeIt, true);
568        insert_expr_rule!(FascinatedBy, true);
569        insert_expr_rule_with_dialect!(FedUpWith, true);
570        insert_expr_rule!(FeelFell, true);
571        insert_expr_rule!(FewUnitsOfTimeAgo, true);
572        insert_expr_rule!(FillerWords, true);
573        insert_struct_rule!(FindFine, true);
574        insert_expr_rule!(FirstAidKit, true);
575        insert_expr_rule!(FleshOutVsFullFledged, true);
576        insert_expr_rule!(ForNoun, true);
577        insert_expr_rule!(FreePredicate, true);
578        insert_expr_rule!(FriendOfMe, true);
579        insert_expr_rule!(GoSoFarAsTo, true);
580        insert_expr_rule!(GoToWar, true);
581        insert_expr_rule!(GoodAt, true);
582        insert_expr_rule!(Handful, true);
583        insert_expr_rule!(HavePronoun, true);
584        insert_struct_rule_with_dialect!(HaveTakeALook, true);
585        insert_expr_rule!(Hedging, true);
586        insert_expr_rule!(HelloGreeting, true);
587        insert_expr_rule!(Hereby, true);
588        insert_struct_rule!(HopHope, true);
589        insert_expr_rule!(HowTo, true);
590        insert_expr_rule!(HyphenateNumberDay, true);
591        insert_expr_rule!(IAmAgreement, true);
592        insert_expr_rule!(IfWouldve, true);
593        insert_struct_rule_with_dialect!(InOnTheCards, true);
594        insert_expr_rule!(InTimeFromNow, true);
595        insert_struct_rule_with_dict!(InflectedVerbAfterTo, true);
596        insert_expr_rule!(InterestedIn, true);
597        insert_expr_rule!(ItLooksLikeThat, true);
598        insert_struct_rule!(ItsContraction, true);
599        insert_expr_rule!(ItsPossessive, true);
600        insert_expr_rule!(JealousOf, true);
601        insert_expr_rule!(JohnsHopkins, true);
602        insert_expr_rule!(LeadRiseTo, true);
603        insert_expr_rule!(LeftRightHand, true);
604        insert_expr_rule!(LessWorse, true);
605        insert_expr_rule!(LetToDo, true);
606        insert_struct_rule!(LetsConfusion, true);
607        insert_expr_rule!(Likewise, true);
608        insert_struct_rule!(LongSentences, true);
609        insert_expr_rule!(LookDownOnesNose, true);
610        insert_expr_rule!(LookingForwardTo, true);
611        insert_struct_rule_with_dict!(MassNouns, true);
612        insert_expr_rule!(MeansALotTo, true);
613        insert_struct_rule!(MergeWords, true);
614        insert_expr_rule!(MissingPreposition, true);
615        insert_expr_rule!(MissingTo, true);
616        insert_expr_rule!(Misspell, true);
617        insert_expr_rule!(MixedBag, true);
618        insert_expr_rule!(ModalBeAdjective, true);
619        insert_expr_rule!(ModalOf, true);
620        insert_expr_rule!(ModalSeem, true);
621        insert_expr_rule!(Months, true);
622        insert_expr_rule_with_dict!(MoreAdjective, true);
623        insert_expr_rule!(MoreBetter, true);
624        insert_expr_rule!(MostNumber, true);
625        insert_expr_rule!(MostOfTheTimes, true);
626        insert_expr_rule!(MultipleSequentialPronouns, true);
627        insert_expr_rule!(NailOnTheHead, true);
628        insert_expr_rule!(NeedToNoun, true);
629        insert_struct_rule!(NoFrenchSpaces, true);
630        insert_expr_rule!(NoLonger, true);
631        insert_expr_rule!(NoMatchFor, true);
632        insert_struct_rule!(NoOxfordComma, false);
633        insert_expr_rule!(Nobody, true);
634        insert_expr_rule!(NominalWants, true);
635        insert_expr_rule!(NorModalPronoun, true);
636        insert_expr_rule!(NotOnlyInversion, true);
637        insert_struct_rule!(NounVerbConfusion, true);
638        insert_struct_rule!(NumberSuffixCapitalization, true);
639        insert_expr_rule!(ObsessPreposition, true);
640        insert_expr_rule!(OfCourse, true);
641        insert_expr_rule!(OldestInTheBook, true);
642        insert_expr_rule!(OnFloor, true);
643        insert_expr_rule!(OnceOrTwice, true);
644        insert_expr_rule!(OneAndTheSame, true);
645        insert_expr_rule_with_dict!(OneOfTheSingular, true);
646        insert_expr_rule!(OpenCompounds, true);
647        insert_expr_rule!(OpenTheLight, true);
648        insert_expr_rule!(OrthographicConsistency, true);
649        insert_expr_rule!(OughtToBe, true);
650        insert_expr_rule!(OutOfDate, true);
651        insert_struct_rule!(OxfordComma, true);
652        insert_expr_rule!(Oxymorons, true);
653        insert_struct_rule!(PhrasalVerbAsCompoundNoun, true);
654        insert_expr_rule!(PiqueInterest, true);
655        insert_expr_rule!(PluralWrongWordOfPhrase, true);
656        insert_struct_rule_with_dict!(PossessiveNoun, false);
657        insert_expr_rule!(PossessiveYour, true);
658        insert_expr_rule!(ProgressiveNeedsBe, true);
659        insert_expr_rule!(PronounAre, true);
660        insert_struct_rule!(PronounContraction, true);
661        insert_expr_rule!(PronounInflectionBe, true);
662        insert_expr_rule!(PronounKnew, true);
663        insert_expr_rule_with_dict!(PronounVerbAgreement, true);
664        insert_expr_rule!(QuantifierNeedsOf, true);
665        insert_expr_rule!(QuantifierNumeralConflict, true);
666        insert_expr_rule!(QuiteQuiet, true);
667        insert_struct_rule!(QuoteSpacing, true);
668        insert_expr_rule!(ReasonForDoing, true);
669        insert_expr_rule!(RedundantAcronyms, true);
670        insert_expr_rule!(RedundantAdditiveAdverbs, true);
671        insert_expr_rule!(RedundantProgressiveComparative, true);
672        insert_struct_rule_with_dialect!(Regionalisms, true);
673        insert_expr_rule_with_dict!(RegularIrregulars, true);
674        insert_struct_rule!(RepeatedWords, true);
675        insert_expr_rule!(Respond, true);
676        insert_expr_rule!(RightClick, true);
677        insert_expr_rule!(RiseTheRanks, true);
678        insert_expr_rule!(RollerSkated, true);
679        insert_expr_rule!(SafeToSave, true);
680        insert_expr_rule!(SaveToSafe, true);
681        insert_struct_rule_with_dict!(SentenceCapitalization, true);
682        insert_expr_rule!(ShootOneselfInTheFoot, true);
683        insert_expr_rule!(SimplePastToPastParticiple, true);
684        insert_expr_rule!(SinceDuration, true);
685        insert_expr_rule!(SingleBe, true);
686        insert_struct_rule!(SneakedSnuck, true);
687        insert_expr_rule!(SomeWithoutArticle, true);
688        insert_expr_rule!(SomethingIs, true);
689        insert_expr_rule!(SomewhatSomething, true);
690        insert_expr_rule!(SoonToBe, true);
691        insert_expr_rule!(SoughtAfter, true);
692        insert_struct_rule!(Spaces, true);
693        insert_struct_rule!(SpelledNumbers, false);
694        insert_expr_rule!(SplitWords, true);
695        insert_struct_rule!(SubjectPronoun, true);
696        insert_expr_rule!(TakeALookTo, true);
697        insert_expr_rule!(TakeMedicine, true);
698        insert_expr_rule!(ThatThan, true);
699        insert_expr_rule!(ThatWhich, true);
700        insert_expr_rule!(TheHowWhy, true);
701        insert_expr_rule!(TheMy, true);
702        insert_expr_rule!(ThePointFor, true);
703        insert_expr_rule!(TheProperNounPossessive, true);
704        insert_expr_rule!(ThenThan, true);
705        insert_expr_rule!(Theres, true);
706        insert_expr_rule!(ThesesThese, true);
707        insert_struct_rule!(TheyreConfusions, true);
708        insert_expr_rule!(ThingThink, true);
709        insert_expr_rule!(ThisTypeOfThing, true);
710        insert_expr_rule!(ThoughThought, true);
711        insert_expr_rule!(ThriveOn, true);
712        insert_expr_rule!(ThrowAway, true);
713        insert_struct_rule!(ThrowRubbish, true);
714        insert_expr_rule!(ToAdverb, true);
715        insert_struct_rule!(ToTwoToo, true);
716        insert_expr_rule!(Touristic, true);
717        insert_expr_rule_with_dict!(TransposedSpace, true);
718        insert_expr_rule!(TryOnesHandAt, true);
719        insert_expr_rule!(TryOnesLuck, true);
720        insert_struct_rule!(UnclosedQuotes, true);
721        insert_expr_rule!(UpdatePlaceNames, true);
722        insert_struct_rule_with_dict!(UseTitleCase, true);
723        insert_expr_rule!(VerbToAdjective, true);
724        insert_expr_rule!(VeryUnique, true);
725        insert_expr_rule!(ViceVersa, true);
726        insert_expr_rule!(ViciousCircle, true);
727        insert_expr_rule!(ViciousCircleOrCycle, false);
728        insert_expr_rule!(ViciousCycle, false);
729        insert_expr_rule!(WasAloud, true);
730        insert_expr_rule!(WayTooAdjective, true);
731        insert_expr_rule!(WellEducated, true);
732        insert_expr_rule!(WereWhere, true);
733        insert_expr_rule!(Whereas, true);
734        insert_expr_rule!(WhomSubjectOfVerb, true);
735        insert_expr_rule!(WidelyAccepted, true);
736        insert_expr_rule_with_dict!(WillNonLemma, true);
737        insert_expr_rule!(WinPrize, true);
738        insert_expr_rule!(WishCould, true);
739        insert_struct_rule!(WordPressDotcom, true);
740        insert_expr_rule_with_dict!(WorthToDo, true);
741        insert_expr_rule!(WouldNeverHave, true);
742        insert_expr_rule!(WrongApostrophe, true);
743
744        // Uses Sentence rather than Chunk
745        out.add("AspireTo", AspireTo::default());
746        out.config.set_rule_enabled("AspireTo", true);
747
748        // Uses Sentence rather than Chunk
749        out.add("Damages", Damages::default());
750        out.config.set_rule_enabled("Damages", true);
751
752        // Uses Sentence rather than Chunk
753        out.add(
754            "MultipleFrequencyAdverbs",
755            MultipleFrequencyAdverbs::default(),
756        );
757        out.config
758            .set_rule_enabled("MultipleFrequencyAdverbs", true);
759
760        // Uses Sentence rather than Chunk
761        out.add("PluralDecades", PluralDecades::default());
762        out.config.set_rule_enabled("PluralDecades", true);
763
764        // Uses Dictionary and Dialect
765        out.add("SpellCheck", SpellCheck::new(dictionary.clone(), dialect));
766        out.config.set_rule_enabled("SpellCheck", true);
767
768        // Uses Sentence rather than Chunk
769        out.add("WebScraping", WebScraping::default());
770        out.config.set_rule_enabled("WebScraping", true);
771
772        out
773    }
774
775    /// Create a new curated group with all config values cleared out.
776    pub fn new_curated_empty_config(
777        dictionary: Arc<impl Dictionary + 'static>,
778        dialect: Dialect,
779    ) -> Self {
780        let mut group = Self::new_curated(dictionary, dialect);
781        group.config.clear();
782        group
783    }
784
785    pub fn organized_lints(&mut self, document: &Document) -> BTreeMap<String, Vec<Lint>> {
786        let mut results = BTreeMap::new();
787
788        // Normal linters
789        for (key, linter) in &mut self.linters {
790            if self.config.is_rule_enabled(key) {
791                results.insert(key.to_owned(), linter.lint(document));
792            }
793        }
794
795        // Expr linters
796        for chunk in document.iter_chunks() {
797            let Some(chunk_span) = chunk.span() else {
798                continue;
799            };
800
801            let chunk_chars = document.get_span_content(&chunk_span);
802            let config_hash = self.hasher_builder.hash_one(&self.config);
803            let char_hash = self.hasher_builder.hash_one(chunk_chars);
804            let cache_key = (char_hash, config_hash);
805
806            let chunk_results = if let Some(hit) = self.chunk_expr_cache.get(&cache_key) {
807                hit.clone()
808            } else {
809                let mut pattern_lints = BTreeMap::new();
810
811                for (key, linter) in &mut self.chunk_expr_linters {
812                    if self.config.is_rule_enabled(key) {
813                        let lints =
814                            run_on_chunk(linter, chunk, document.get_source()).map(|mut l| {
815                                l.span.pull_by(chunk_span.start);
816                                l
817                            });
818
819                        pattern_lints.insert(key.clone(), lints.collect());
820                    }
821                }
822
823                let pattern_lints = Lrc::new(pattern_lints);
824
825                self.chunk_expr_cache.put(cache_key, pattern_lints.clone());
826                pattern_lints
827            };
828
829            for (key, vec) in chunk_results.iter() {
830                results
831                    .entry(key.to_owned())
832                    .or_default()
833                    .extend(vec.iter().cloned().map(|mut lint| {
834                        // Bring the spans back into document-space
835                        lint.span.push_by(chunk_span.start);
836                        lint
837                    }));
838            }
839        }
840
841        results
842    }
843}
844
845impl Default for LintGroup {
846    fn default() -> Self {
847        Self::empty()
848    }
849}
850
851impl Linter for LintGroup {
852    fn lint(&mut self, document: &Document) -> Vec<Lint> {
853        self.organized_lints(document)
854            .into_values()
855            .flatten()
856            .collect()
857    }
858
859    fn description(&self) -> &str {
860        "A collection of linters that can be run as one."
861    }
862}
863
864#[cfg(test)]
865mod tests {
866    use std::sync::Arc;
867
868    use super::{FlatConfig, LintGroup};
869    use crate::linting::LintKind;
870    use crate::linting::tests::assert_no_lints;
871    use crate::spell::{FstDictionary, MutableDictionary};
872    use crate::{Dialect, Document, linting::Linter};
873
874    fn test_group() -> LintGroup {
875        LintGroup::new_curated(Arc::new(MutableDictionary::curated()), Dialect::American)
876    }
877
878    #[test]
879    fn clean_interjection() {
880        assert_no_lints(
881            "Although I only saw the need to interject once, I still saw it.",
882            test_group(),
883        );
884    }
885
886    #[test]
887    fn clean_consensus() {
888        assert_no_lints("But there is less consensus on this.", test_group());
889    }
890
891    #[test]
892    fn can_get_all_descriptions() {
893        let group =
894            LintGroup::new_curated(Arc::new(MutableDictionary::default()), Dialect::American);
895        group.all_descriptions();
896    }
897
898    #[test]
899    fn can_get_all_descriptions_as_html() {
900        let group =
901            LintGroup::new_curated(Arc::new(MutableDictionary::default()), Dialect::American);
902        group.all_descriptions_html();
903    }
904
905    #[test]
906    fn dont_flag_low_hanging_fruit_msg() {
907        assert_no_lints(
908            "The standard form is low-hanging fruit with a hyphen and singular form.",
909            test_group(),
910        );
911    }
912
913    #[test]
914    fn dont_flag_low_hanging_fruit_desc() {
915        assert_no_lints(
916            "Corrects nonstandard variants of low-hanging fruit.",
917            test_group(),
918        );
919    }
920
921    /// Tests that no linters' descriptions contain errors handled by other linters.
922    ///
923    /// This test verifies that the description of each linter (which is written in natural language)
924    /// doesn't trigger any other linter's rules, with the exception of certain linters that
925    /// suggest mere alternatives rather than flagging actual errors.
926    ///
927    /// For example, we disable the "MoreAdjective" linter since some comparative and superlative
928    /// adjectives can be more awkward than their two-word counterparts, even if technically correct.
929    ///
930    /// If this test fails, it means either:
931    /// 1. A linter's description contains an actual error that should be fixed, or
932    /// 2. A linter is being too aggressive in flagging text that is actually correct English
933    ///    in the context of another linter's description.
934    #[test]
935    fn lint_descriptions_are_clean() {
936        let lints_to_check = LintGroup::new_curated(FstDictionary::curated(), Dialect::American);
937
938        let enforcer_config = FlatConfig::new_curated();
939        let mut lints_to_enforce =
940            LintGroup::new_curated(FstDictionary::curated(), Dialect::American)
941                .with_lint_config(enforcer_config);
942
943        let name_description_pairs: Vec<_> = lints_to_check
944            .all_descriptions()
945            .into_iter()
946            .map(|(n, d)| (n.to_string(), d.to_string()))
947            .collect();
948
949        for (lint_name, description) in name_description_pairs {
950            let doc = Document::new_markdown_default_curated(&description);
951            eprintln!("{lint_name}: {description}");
952
953            let mut lints = lints_to_enforce.lint(&doc);
954
955            // Remove ones related to style
956            lints.retain(|l| l.lint_kind != LintKind::Style);
957
958            if !lints.is_empty() {
959                dbg!(lints);
960                panic!();
961            }
962        }
963    }
964
965    #[test]
966    fn no_linter_names_clash() {
967        let group =
968            LintGroup::new_curated(Arc::new(MutableDictionary::default()), Dialect::American);
969
970        if let Some(names) = &group.clashing_linter_names {
971            if !names.is_empty() {
972                panic!(
973                    "⚠️ Found {} clashing linter names: {}",
974                    names.len(),
975                    names.join(", ")
976                );
977            }
978        }
979    }
980}