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