1use std::collections::BTreeMap;
2use std::hash::Hash;
3use std::hash::{BuildHasher, Hasher};
4use std::mem;
5use std::num::NonZero;
6use std::sync::Arc;
7
8use cached::proc_macro::cached;
9use foldhash::quality::RandomState;
10use hashbrown::HashMap;
11use lru::LruCache;
12use serde::{Deserialize, Deserializer, Serialize, Serializer};
13
14use super::a_part::APart;
15use super::addicting::Addicting;
16use super::adjective_double_degree::AdjectiveDoubleDegree;
17use super::adjective_of_a::AdjectiveOfA;
18use super::after_later::AfterLater;
19use super::all_intents_and_purposes::AllIntentsAndPurposes;
20use super::allow_to::AllowTo;
21use super::am_in_the_morning::AmInTheMorning;
22use super::amounts_for::AmountsFor;
23use super::an_a::AnA;
24use super::another_thing_coming::AnotherThingComing;
25use super::another_think_coming::AnotherThinkComing;
26use super::ask_no_preposition::AskNoPreposition;
27use super::avoid_curses::AvoidCurses;
28use super::back_in_the_day::BackInTheDay;
29use super::be_allowed::BeAllowed;
30use super::best_of_all_time::BestOfAllTime;
31use super::boring_words::BoringWords;
32use super::bought::Bought;
33use super::cant::Cant;
34use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
35use super::cautionary_tale::CautionaryTale;
36use super::change_tack::ChangeTack;
37use super::chock_full::ChockFull;
38use super::comma_fixes::CommaFixes;
39use super::compound_nouns::CompoundNouns;
40use super::compound_subject_i::CompoundSubjectI;
41use super::confident::Confident;
42use super::correct_number_suffix::CorrectNumberSuffix;
43use super::criteria_phenomena::CriteriaPhenomena;
44use super::despite_of::DespiteOf;
45use super::didnt::Didnt;
46use super::discourse_markers::DiscourseMarkers;
47use super::dot_initialisms::DotInitialisms;
48use super::double_click::DoubleClick;
49use super::double_modal::DoubleModal;
50use super::ellipsis_length::EllipsisLength;
51use super::else_possessive::ElsePossessive;
52use super::everyday::Everyday;
53use super::expand_memory_shorthands::ExpandMemoryShorthands;
54use super::expand_time_shorthands::ExpandTimeShorthands;
55use super::expr_linter::run_on_chunk;
56use super::far_be_it::FarBeIt;
57use super::feel_fell::FeelFell;
58use super::few_units_of_time_ago::FewUnitsOfTimeAgo;
59use super::filler_words::FillerWords;
60use super::first_aid_kit::FirstAidKit;
61use super::for_noun::ForNoun;
62use super::free_predicate::FreePredicate;
63use super::friend_of_me::FriendOfMe;
64use super::have_pronoun::HavePronoun;
65use super::have_take_a_look::HaveTakeALook;
66use super::hedging::Hedging;
67use super::hello_greeting::HelloGreeting;
68use super::hereby::Hereby;
69use super::hop_hope::HopHope;
70use super::how_to::HowTo;
71use super::hyphenate_number_day::HyphenateNumberDay;
72use super::i_am_agreement::IAmAgreement;
73use super::in_on_the_cards::InOnTheCards;
74use super::inflected_verb_after_to::InflectedVerbAfterTo;
75use super::interested_in::InterestedIn;
76use super::it_looks_like_that::ItLooksLikeThat;
77use super::its_contraction::ItsContraction;
78use super::its_possessive::ItsPossessive;
79use super::left_right_hand::LeftRightHand;
80use super::less_worse::LessWorse;
81use super::let_to_do::LetToDo;
82use super::lets_confusion::LetsConfusion;
83use super::likewise::Likewise;
84use super::long_sentences::LongSentences;
85use super::looking_forward_to::LookingForwardTo;
86use super::merge_words::MergeWords;
87use super::missing_preposition::MissingPreposition;
88use super::missing_to::MissingTo;
89use super::misspell::Misspell;
90use super::mixed_bag::MixedBag;
91use super::modal_of::ModalOf;
92use super::modal_seem::ModalSeem;
93use super::months::Months;
94use super::more_better::MoreBetter;
95use super::most_number::MostNumber;
96use super::most_of_the_times::MostOfTheTimes;
97use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
98use super::nail_on_the_head::NailOnTheHead;
99use super::no_french_spaces::NoFrenchSpaces;
100use super::no_match_for::NoMatchFor;
101use super::nobody::Nobody;
102use super::nominal_wants::NominalWants;
103use super::noun_countability::NounCountability;
104use super::number_suffix_capitalization::NumberSuffixCapitalization;
105use super::of_course::OfCourse;
106use super::on_floor::OnFloor;
107use super::once_or_twice::OnceOrTwice;
108use super::one_and_the_same::OneAndTheSame;
109use super::open_the_light::OpenTheLight;
110use super::orthographic_consistency::OrthographicConsistency;
111use super::ought_to_be::OughtToBe;
112use super::out_of_date::OutOfDate;
113use super::oxymorons::Oxymorons;
114use super::phrasal_verb_as_compound_noun::PhrasalVerbAsCompoundNoun;
115use super::pique_interest::PiqueInterest;
116use super::possessive_noun::PossessiveNoun;
117use super::possessive_your::PossessiveYour;
118use super::progressive_needs_be::ProgressiveNeedsBe;
119use super::pronoun_are::PronounAre;
120use super::pronoun_contraction::PronounContraction;
121use super::pronoun_inflection_be::PronounInflectionBe;
122use super::pronoun_knew::PronounKnew;
123use super::proper_noun_capitalization_linters;
124use super::quantifier_needs_of::QuantifierNeedsOf;
125use super::quite_quiet::QuiteQuiet;
126use super::quote_spacing::QuoteSpacing;
127use super::redundant_additive_adverbs::RedundantAdditiveAdverbs;
128use super::regionalisms::Regionalisms;
129use super::repeated_words::RepeatedWords;
130use super::roller_skated::RollerSkated;
131use super::safe_to_save::SafeToSave;
132use super::save_to_safe::SaveToSafe;
133use super::semicolon_apostrophe::SemicolonApostrophe;
134use super::sentence_capitalization::SentenceCapitalization;
135use super::shoot_oneself_in_the_foot::ShootOneselfInTheFoot;
136use super::simple_past_to_past_participle::SimplePastToPastParticiple;
137use super::since_duration::SinceDuration;
138use super::some_without_article::SomeWithoutArticle;
139use super::something_is::SomethingIs;
140use super::somewhat_something::SomewhatSomething;
141use super::sought_after::SoughtAfter;
142use super::spaces::Spaces;
143use super::spell_check::SpellCheck;
144use super::spelled_numbers::SpelledNumbers;
145use super::split_words::SplitWords;
146use super::that_than::ThatThan;
147use super::that_which::ThatWhich;
148use super::the_how_why::TheHowWhy;
149use super::the_my::TheMy;
150use super::then_than::ThenThan;
151use super::theres::Theres;
152use super::thing_think::ThingThink;
153use super::though_thought::ThoughThought;
154use super::throw_away::ThrowAway;
155use super::throw_rubbish::ThrowRubbish;
156use super::to_adverb::ToAdverb;
157use super::to_two_too::ToTwoToo;
158use super::touristic::Touristic;
159use super::unclosed_quotes::UnclosedQuotes;
160use super::update_place_names::UpdatePlaceNames;
161use super::use_genitive::UseGenitive;
162use super::verb_to_adjective::VerbToAdjective;
163use super::very_unique::VeryUnique;
164use super::vice_versa::ViceVersa;
165use super::was_aloud::WasAloud;
166use super::way_too_adjective::WayTooAdjective;
167use super::well_educated::WellEducated;
168use super::whereas::Whereas;
169use super::widely_accepted::WidelyAccepted;
170use super::win_prize::WinPrize;
171use super::wordpress_dotcom::WordPressDotcom;
172use super::would_never_have::WouldNeverHave;
173use super::{CurrencyPlacement, HtmlDescriptionLinter, Linter, NoOxfordComma, OxfordComma};
174use super::{ExprLinter, Lint};
175use crate::linting::dashes::Dashes;
176use crate::linting::open_compounds::OpenCompounds;
177use crate::linting::{
178 MassPlurals, NounVerbConfusion, closed_compounds, initialisms, phrase_corrections,
179 phrase_set_corrections,
180};
181use crate::spell::{Dictionary, MutableDictionary};
182use crate::{CharString, Dialect, Document, TokenStringExt};
183
184fn ser_ordered<S>(map: &HashMap<String, Option<bool>>, ser: S) -> Result<S::Ok, S::Error>
185where
186 S: Serializer,
187{
188 let ordered: BTreeMap<_, _> = map.iter().map(|(k, v)| (k.clone(), *v)).collect();
189 ordered.serialize(ser)
190}
191
192fn de_hashbrown<'de, D>(de: D) -> Result<HashMap<String, Option<bool>>, D::Error>
193where
194 D: Deserializer<'de>,
195{
196 let ordered: BTreeMap<String, Option<bool>> = BTreeMap::deserialize(de)?;
197 Ok(ordered.into_iter().collect())
198}
199
200#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
203#[serde(transparent)]
204pub struct LintGroupConfig {
205 #[serde(serialize_with = "ser_ordered", deserialize_with = "de_hashbrown")]
207 inner: HashMap<String, Option<bool>>,
208}
209
210#[cached]
211fn curated_config() -> LintGroupConfig {
212 let group = LintGroup::new_curated(MutableDictionary::new().into(), Dialect::American);
214 group.config
215}
216
217impl LintGroupConfig {
218 pub fn set_rule_enabled(&mut self, key: impl ToString, val: bool) {
219 self.inner.insert(key.to_string(), Some(val));
220 }
221
222 pub fn unset_rule_enabled(&mut self, key: impl AsRef<str>) {
225 self.inner.remove(key.as_ref());
226 }
227
228 pub fn set_rule_enabled_if_unset(&mut self, key: impl AsRef<str>, val: bool) {
229 if !self.inner.contains_key(key.as_ref()) {
230 self.set_rule_enabled(key.as_ref().to_string(), val);
231 }
232 }
233
234 pub fn is_rule_enabled(&self, key: &str) -> bool {
235 self.inner.get(key).cloned().flatten().unwrap_or(false)
236 }
237
238 pub fn clear(&mut self) {
241 for val in self.inner.values_mut() {
242 *val = None
243 }
244 }
245
246 pub fn merge_from(&mut self, other: &mut LintGroupConfig) {
251 for (key, val) in other.inner.iter() {
252 if val.is_none() {
253 continue;
254 }
255
256 self.inner.insert(key.to_string(), *val);
257 }
258
259 other.clear();
260 }
261
262 pub fn fill_with_curated(&mut self) {
264 let mut temp = Self::new_curated();
265 mem::swap(self, &mut temp);
266 self.merge_from(&mut temp);
267 }
268
269 pub fn new_curated() -> Self {
270 curated_config()
271 }
272}
273
274impl Hash for LintGroupConfig {
275 fn hash<H: Hasher>(&self, hasher: &mut H) {
276 for (key, value) in &self.inner {
277 hasher.write(key.as_bytes());
278 if let Some(value) = value {
279 hasher.write_u8(1);
280 hasher.write_u8(*value as u8);
281 } else {
282 hasher.write_u8(0);
284 hasher.write_u8(0);
285 }
286 }
287 }
288}
289
290pub struct LintGroup {
293 pub config: LintGroupConfig,
294 linters: BTreeMap<String, Box<dyn Linter>>,
296 expr_linters: BTreeMap<String, Box<dyn ExprLinter>>,
298 chunk_expr_cache: LruCache<(CharString, u64), BTreeMap<String, Vec<Lint>>>,
305 hasher_builder: RandomState,
306}
307
308impl LintGroup {
309 pub fn empty() -> Self {
310 Self {
311 config: LintGroupConfig::default(),
312 linters: BTreeMap::new(),
313 expr_linters: BTreeMap::new(),
314 chunk_expr_cache: LruCache::new(NonZero::new(10000).unwrap()),
315 hasher_builder: RandomState::default(),
316 }
317 }
318
319 pub fn contains_key(&self, name: impl AsRef<str>) -> bool {
321 self.linters.contains_key(name.as_ref()) || self.expr_linters.contains_key(name.as_ref())
322 }
323
324 pub fn add(&mut self, name: impl AsRef<str>, linter: impl Linter + 'static) -> bool {
327 if self.contains_key(&name) {
328 false
329 } else {
330 self.linters
331 .insert(name.as_ref().to_string(), Box::new(linter));
332 true
333 }
334 }
335
336 pub fn add_expr_linter(
342 &mut self,
343 name: impl AsRef<str>,
344 linter: impl ExprLinter + 'static,
345 ) -> bool {
346 if self.contains_key(&name) {
347 false
348 } else {
349 self.expr_linters
350 .insert(name.as_ref().to_string(), Box::new(linter));
351 true
352 }
353 }
354
355 pub fn merge_from(&mut self, other: &mut LintGroup) {
358 self.config.merge_from(&mut other.config);
359
360 let other_linters = std::mem::take(&mut other.linters);
361 self.linters.extend(other_linters);
362
363 let other_pattern_linters = std::mem::take(&mut other.expr_linters);
364 self.expr_linters.extend(other_pattern_linters);
365 }
366
367 pub fn iter_keys(&self) -> impl Iterator<Item = &str> {
368 self.linters
369 .keys()
370 .chain(self.expr_linters.keys())
371 .map(|v| v.as_str())
372 }
373
374 pub fn set_all_rules_to(&mut self, enabled: Option<bool>) {
377 let keys = self.iter_keys().map(|v| v.to_string()).collect::<Vec<_>>();
378
379 for key in keys {
380 match enabled {
381 Some(v) => self.config.set_rule_enabled(key, v),
382 None => self.config.unset_rule_enabled(key),
383 }
384 }
385 }
386
387 pub fn all_descriptions(&self) -> HashMap<&str, &str> {
389 self.linters
390 .iter()
391 .map(|(key, value)| (key.as_str(), value.description()))
392 .chain(
393 self.expr_linters
394 .iter()
395 .map(|(key, value)| (key.as_str(), ExprLinter::description(value))),
396 )
397 .collect()
398 }
399
400 pub fn all_descriptions_html(&self) -> HashMap<&str, String> {
402 self.linters
403 .iter()
404 .map(|(key, value)| (key.as_str(), value.description_html()))
405 .chain(
406 self.expr_linters
407 .iter()
408 .map(|(key, value)| (key.as_str(), value.description_html())),
409 )
410 .collect()
411 }
412
413 pub fn with_lint_config(mut self, config: LintGroupConfig) -> Self {
415 self.config = config;
416 self
417 }
418
419 pub fn new_curated(dictionary: Arc<impl Dictionary + 'static>, dialect: Dialect) -> Self {
420 let mut out = Self::empty();
421
422 macro_rules! insert_struct_rule {
424 ($rule:ident, $default_config:expr) => {
425 out.add(stringify!($rule), $rule::default());
426 out.config
427 .set_rule_enabled(stringify!($rule), $default_config);
428 };
429 }
430
431 macro_rules! insert_expr_rule {
435 ($rule:ident, $default_config:expr) => {
436 out.add_expr_linter(stringify!($rule), $rule::default());
437 out.config
438 .set_rule_enabled(stringify!($rule), $default_config);
439 };
440 }
441
442 out.merge_from(&mut phrase_corrections::lint_group());
443 out.merge_from(&mut phrase_set_corrections::lint_group());
444 out.merge_from(&mut proper_noun_capitalization_linters::lint_group(
445 dictionary.clone(),
446 ));
447 out.merge_from(&mut closed_compounds::lint_group());
448 out.merge_from(&mut initialisms::lint_group());
449 insert_expr_rule!(APart, true);
455 insert_expr_rule!(Addicting, true);
456 insert_expr_rule!(AdjectiveDoubleDegree, true);
457 insert_struct_rule!(AdjectiveOfA, true);
458 insert_expr_rule!(AfterLater, true);
459 insert_expr_rule!(AllIntentsAndPurposes, true);
460 insert_expr_rule!(AllowTo, true);
461 insert_struct_rule!(AmInTheMorning, true);
462 insert_expr_rule!(AmountsFor, true);
463 insert_struct_rule!(AnA, true);
464 insert_expr_rule!(AnotherThingComing, true);
465 insert_expr_rule!(AnotherThinkComing, false);
466 insert_expr_rule!(AskNoPreposition, true);
467 insert_expr_rule!(AvoidCurses, true);
468 insert_expr_rule!(BackInTheDay, true);
469 insert_expr_rule!(BeAllowed, true);
470 insert_expr_rule!(BestOfAllTime, true);
471 insert_expr_rule!(BoringWords, false);
472 insert_expr_rule!(Bought, true);
473 insert_expr_rule!(Cant, true);
474 insert_struct_rule!(CapitalizePersonalPronouns, true);
475 insert_expr_rule!(CautionaryTale, true);
476 insert_expr_rule!(ChangeTack, true);
477 insert_expr_rule!(ChockFull, true);
478 insert_expr_rule!(VerbToAdjective, true);
479 insert_struct_rule!(CommaFixes, true);
480 insert_struct_rule!(CompoundNouns, true);
481 insert_expr_rule!(CompoundSubjectI, true);
482 insert_expr_rule!(Confident, true);
483 insert_struct_rule!(CorrectNumberSuffix, true);
484 insert_expr_rule!(CriteriaPhenomena, true);
485 insert_struct_rule!(CurrencyPlacement, true);
486 insert_expr_rule!(Dashes, true);
487 insert_expr_rule!(DespiteOf, true);
488 insert_expr_rule!(Didnt, true);
489 insert_struct_rule!(DiscourseMarkers, true);
490 insert_expr_rule!(DotInitialisms, true);
491 insert_expr_rule!(DoubleClick, true);
492 insert_expr_rule!(DoubleModal, true);
493 insert_struct_rule!(EllipsisLength, true);
494 insert_struct_rule!(ElsePossessive, true);
495 insert_struct_rule!(Everyday, true);
496 insert_expr_rule!(ExpandMemoryShorthands, true);
497 insert_expr_rule!(ExpandTimeShorthands, true);
498 insert_expr_rule!(FarBeIt, true);
499 insert_expr_rule!(FeelFell, true);
500 insert_expr_rule!(FewUnitsOfTimeAgo, true);
501 insert_expr_rule!(FillerWords, true);
502 insert_struct_rule!(FirstAidKit, true);
503 insert_struct_rule!(ForNoun, true);
504 insert_expr_rule!(FreePredicate, true);
505 insert_expr_rule!(FriendOfMe, true);
506 insert_expr_rule!(HavePronoun, true);
507 insert_expr_rule!(Hedging, true);
508 insert_expr_rule!(HelloGreeting, true);
509 insert_expr_rule!(Hereby, true);
510 insert_struct_rule!(HopHope, true);
511 insert_struct_rule!(HowTo, true);
512 insert_expr_rule!(HyphenateNumberDay, true);
513 insert_expr_rule!(IAmAgreement, true);
514 insert_expr_rule!(InterestedIn, true);
515 insert_expr_rule!(ItLooksLikeThat, true);
516 insert_struct_rule!(ItsContraction, true);
517 insert_struct_rule!(ItsPossessive, true);
518 insert_expr_rule!(LeftRightHand, true);
519 insert_expr_rule!(LessWorse, true);
520 insert_expr_rule!(LetToDo, true);
521 insert_struct_rule!(LetsConfusion, true);
522 insert_expr_rule!(Likewise, true);
523 insert_struct_rule!(LongSentences, true);
524 insert_expr_rule!(LookingForwardTo, true);
525 insert_struct_rule!(MergeWords, true);
526 insert_expr_rule!(MissingPreposition, true);
527 insert_expr_rule!(MissingTo, true);
528 insert_expr_rule!(Misspell, true);
529 insert_expr_rule!(MixedBag, true);
530 insert_expr_rule!(ModalOf, true);
531 insert_expr_rule!(ModalSeem, true);
532 insert_expr_rule!(Months, true);
533 insert_expr_rule!(MoreBetter, true);
534 insert_expr_rule!(MostNumber, true);
535 insert_expr_rule!(MostOfTheTimes, true);
536 insert_expr_rule!(MultipleSequentialPronouns, true);
537 insert_struct_rule!(NailOnTheHead, true);
538 insert_struct_rule!(NoFrenchSpaces, true);
539 insert_expr_rule!(NoMatchFor, true);
540 insert_struct_rule!(NoOxfordComma, false);
541 insert_expr_rule!(Nobody, true);
542 insert_struct_rule!(NominalWants, true);
543 insert_expr_rule!(NounCountability, true);
544 insert_struct_rule!(NounVerbConfusion, true);
545 insert_struct_rule!(NumberSuffixCapitalization, true);
546 insert_struct_rule!(OfCourse, true);
547 insert_expr_rule!(OnFloor, true);
548 insert_expr_rule!(OnceOrTwice, true);
549 insert_expr_rule!(OneAndTheSame, true);
550 insert_expr_rule!(OpenCompounds, true);
551 insert_expr_rule!(OpenTheLight, true);
552 insert_struct_rule!(OrthographicConsistency, true);
553 insert_struct_rule!(OughtToBe, true);
554 insert_expr_rule!(OutOfDate, true);
555 insert_struct_rule!(OxfordComma, true);
556 insert_expr_rule!(Oxymorons, true);
557 insert_struct_rule!(PhrasalVerbAsCompoundNoun, true);
558 insert_expr_rule!(PiqueInterest, true);
559 insert_expr_rule!(PossessiveYour, true);
560 insert_expr_rule!(ProgressiveNeedsBe, true);
561 insert_expr_rule!(PronounAre, true);
562 insert_struct_rule!(PronounContraction, true);
563 insert_expr_rule!(PronounInflectionBe, true);
564 insert_struct_rule!(PronounKnew, true);
565 insert_expr_rule!(QuantifierNeedsOf, true);
566 insert_expr_rule!(QuiteQuiet, true);
567 insert_struct_rule!(QuoteSpacing, true);
568 insert_expr_rule!(RedundantAdditiveAdverbs, true);
569 insert_struct_rule!(RepeatedWords, true);
570 insert_expr_rule!(RollerSkated, true);
571 insert_expr_rule!(SafeToSave, true);
572 insert_struct_rule!(SaveToSafe, true);
573 insert_expr_rule!(SemicolonApostrophe, true);
574 insert_expr_rule!(ShootOneselfInTheFoot, true);
575 insert_expr_rule!(SimplePastToPastParticiple, true);
576 insert_expr_rule!(SinceDuration, true);
577 insert_expr_rule!(SomeWithoutArticle, true);
578 insert_expr_rule!(SomethingIs, true);
579 insert_expr_rule!(SomewhatSomething, true);
580 insert_expr_rule!(SoughtAfter, true);
581 insert_struct_rule!(Spaces, true);
582 insert_struct_rule!(SpelledNumbers, false);
583 insert_expr_rule!(SplitWords, true);
584 insert_expr_rule!(ThatThan, true);
585 insert_expr_rule!(ThatWhich, true);
586 insert_expr_rule!(TheHowWhy, true);
587 insert_struct_rule!(TheMy, true);
588 insert_expr_rule!(ThenThan, true);
589 insert_expr_rule!(Theres, true);
590 insert_expr_rule!(ThingThink, true);
591 insert_expr_rule!(ThoughThought, true);
592 insert_expr_rule!(ThrowAway, true);
593 insert_struct_rule!(ThrowRubbish, true);
594 insert_expr_rule!(ToAdverb, true);
595 insert_struct_rule!(ToTwoToo, true);
596 insert_expr_rule!(Touristic, true);
597 insert_struct_rule!(UnclosedQuotes, true);
598 insert_expr_rule!(UpdatePlaceNames, true);
599 insert_expr_rule!(UseGenitive, false);
600 insert_expr_rule!(VeryUnique, true);
601 insert_expr_rule!(ViceVersa, true);
602 insert_expr_rule!(WasAloud, true);
603 insert_expr_rule!(WayTooAdjective, true);
604 insert_expr_rule!(WellEducated, true);
605 insert_expr_rule!(Whereas, true);
606 insert_expr_rule!(WidelyAccepted, true);
607 insert_expr_rule!(WinPrize, true);
608 insert_struct_rule!(WordPressDotcom, true);
609 insert_expr_rule!(WouldNeverHave, true);
610
611 out.add("SpellCheck", SpellCheck::new(dictionary.clone(), dialect));
612 out.config.set_rule_enabled("SpellCheck", true);
613
614 out.add(
615 "InflectedVerbAfterTo",
616 InflectedVerbAfterTo::new(dictionary.clone()),
617 );
618 out.config.set_rule_enabled("InflectedVerbAfterTo", true);
619
620 out.add("InOnTheCards", InOnTheCards::new(dialect));
621 out.config.set_rule_enabled("InOnTheCards", true);
622
623 out.add(
624 "SentenceCapitalization",
625 SentenceCapitalization::new(dictionary.clone()),
626 );
627 out.config.set_rule_enabled("SentenceCapitalization", true);
628
629 out.add("PossessiveNoun", PossessiveNoun::new(dictionary.clone()));
630 out.config.set_rule_enabled("PossessiveNoun", false);
631
632 out.add("Regionalisms", Regionalisms::new(dialect));
633 out.config.set_rule_enabled("Regionalisms", true);
634
635 out.add("HaveTakeALook", HaveTakeALook::new(dialect));
636 out.config.set_rule_enabled("HaveTakeALook", true);
637
638 out.add("MassPlurals", MassPlurals::new(dictionary.clone()));
639 out.config.set_rule_enabled("MassPlurals", true);
640
641 out
642 }
643
644 pub fn new_curated_empty_config(
646 dictionary: Arc<impl Dictionary + 'static>,
647 dialect: Dialect,
648 ) -> Self {
649 let mut group = Self::new_curated(dictionary, dialect);
650 group.config.clear();
651 group
652 }
653
654 pub fn organized_lints(&mut self, document: &Document) -> BTreeMap<String, Vec<Lint>> {
655 let mut results = BTreeMap::new();
656
657 for (key, linter) in &mut self.linters {
659 if self.config.is_rule_enabled(key) {
660 results.insert(key.clone(), linter.lint(document));
661 }
662 }
663
664 for chunk in document.iter_chunks() {
666 let Some(chunk_span) = chunk.span() else {
667 continue;
668 };
669
670 let chunk_chars = document.get_span_content(&chunk_span);
671 let config_hash = self.hasher_builder.hash_one(&self.config);
672 let cache_key = (chunk_chars.into(), config_hash);
673
674 let mut chunk_results = if let Some(hit) = self.chunk_expr_cache.get(&cache_key) {
675 hit.clone()
676 } else {
677 let mut pattern_lints = BTreeMap::new();
678
679 for (key, linter) in &mut self.expr_linters {
680 if self.config.is_rule_enabled(key) {
681 let lints =
682 run_on_chunk(linter, chunk, document.get_source()).map(|mut l| {
683 l.span.pull_by(chunk_span.start);
684 l
685 });
686
687 pattern_lints.insert(key.clone(), lints.collect());
688 }
689 }
690
691 self.chunk_expr_cache.put(cache_key, pattern_lints.clone());
692 pattern_lints
693 };
694
695 for value in chunk_results.values_mut() {
697 for lint in value {
698 lint.span.push_by(chunk_span.start);
699 }
700 }
701
702 for (key, mut vec) in chunk_results {
703 results.entry(key).or_default().append(&mut vec);
704 }
705 }
706
707 results
708 }
709}
710
711impl Default for LintGroup {
712 fn default() -> Self {
713 Self::empty()
714 }
715}
716
717impl Linter for LintGroup {
718 fn lint(&mut self, document: &Document) -> Vec<Lint> {
719 self.organized_lints(document)
720 .into_values()
721 .flatten()
722 .collect()
723 }
724
725 fn description(&self) -> &str {
726 "A collection of linters that can be run as one."
727 }
728}
729
730#[cfg(test)]
731mod tests {
732 use std::sync::Arc;
733
734 use super::LintGroup;
735 use crate::linting::tests::assert_no_lints;
736 use crate::spell::{FstDictionary, MutableDictionary};
737 use crate::{Dialect, Document, linting::Linter};
738
739 fn test_group() -> LintGroup {
740 LintGroup::new_curated(Arc::new(MutableDictionary::curated()), Dialect::American)
741 }
742
743 #[test]
744 fn clean_interjection() {
745 assert_no_lints(
746 "Although I only saw the need to interject once, I still saw it.",
747 test_group(),
748 );
749 }
750
751 #[test]
752 fn clean_consensus() {
753 assert_no_lints("But there is less consensus on this.", test_group());
754 }
755
756 #[test]
757 fn can_get_all_descriptions() {
758 let group =
759 LintGroup::new_curated(Arc::new(MutableDictionary::default()), Dialect::American);
760 group.all_descriptions();
761 }
762
763 #[test]
764 fn can_get_all_descriptions_as_html() {
765 let group =
766 LintGroup::new_curated(Arc::new(MutableDictionary::default()), Dialect::American);
767 group.all_descriptions_html();
768 }
769
770 #[test]
771 fn dont_flag_low_hanging_fruit_msg() {
772 assert_no_lints(
773 "The standard form is low-hanging fruit with a hyphen and singular form.",
774 test_group(),
775 );
776 }
777
778 #[test]
779 fn dont_flag_low_hanging_fruit_desc() {
780 assert_no_lints(
781 "Corrects non-standard variants of low-hanging fruit.",
782 test_group(),
783 );
784 }
785
786 #[test]
787 fn lint_descriptions_are_clean() {
788 let mut group = LintGroup::new_curated(FstDictionary::curated(), Dialect::American);
789 let pairs: Vec<_> = group
790 .all_descriptions()
791 .into_iter()
792 .map(|(a, b)| (a.to_string(), b.to_string()))
793 .collect();
794
795 for (key, value) in pairs {
796 let doc = Document::new_markdown_default_curated(&value);
797 eprintln!("{key}: {value}");
798
799 if !group.lint(&doc).is_empty() {
800 dbg!(&group.lint(&doc));
801 panic!();
802 }
803 }
804 }
805}