Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
Merge branch 'master' into modal-of-pr
Elijah Potteref9526f2d7e9
diff --cc harper-core/src/linting/lint_group.rsindex 3bf72e01d,d741f577d..b8416397e--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@@ -78,271 -53,234 +54,235 @@@ use super::was_aloud::WasAlouduse super::whereas::Whereas;use super::wordpress_dotcom::WordPressDotcom;use super::wrong_quotes::WrongQuotes;- use super::{CurrencyPlacement, Lint, Linter, NoOxfordComma, OxfordComma};- use crate::{Dictionary, Document};-- macro_rules! create_lint_group_config {- ($($linter:ident => $default:expr),* $(,)?) => {- paste! {- /// A collection of all the descriptions from the composing linters.- #[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]- pub struct LintGroupDescriptions<'a> {- $(- #[doc = "The description for the [`" $linter "`] linter."]- pub [<$linter:snake>]: &'a str,- )*- pub spell_check: &'a str- }+ use super::{CurrencyPlacement, Linter, NoOxfordComma, OxfordComma};+ use crate::Document;+ use crate::linting::{closed_compounds, phrase_corrections};+ use crate::{Dictionary, MutableDictionary};+ #[derive(Debug, Serialize, Deserialize, Default, Clone)]+ #[serde(transparent)]+ pub struct LintGroupConfig {+ inner: HashMap<String, Option<bool>>,+ }- impl<'a> LintGroupDescriptions<'a> {- /// Create a [`Vec`] containing the key-value pairs of this struct.- pub fn to_vec_pairs(self) -> Vec<(&'static str, &'a str)>{- vec![$((stringify!([<$linter:snake>]), self.[<$linter:snake>],),)* ("spell_check", self.spell_check)]- }- }+ #[cached]+ fn curated_config() -> LintGroupConfig {+ // Dictionary does not matter, we're just after the config.+ let group = LintGroup::new_curated(MutableDictionary::new().into());+ group.config+ }- /// A collection of all officially supported- #[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]- pub struct LintGroupConfig {- $(- #[doc = "Configures the use of the [`" $linter "`] linter.- If set to [`None`], the default configuration will be used."]- pub [<$linter:snake>]: Option<bool>,- )*- pub spell_check: Option<bool>- }+ impl LintGroupConfig {+ pub fn set_rule_enabled(&mut self, key: impl ToString, val: bool) {+ self.inner.insert(key.to_string(), Some(val));+ }- impl LintGroupConfig {- /// Creates a config with all lints disabled.- pub fn none() -> Self{- Self {- $(- [<$linter:snake>]: Some(false),- )*- spell_check: Some(false)- }- }-- /// Fills the [`None`] values in the configuration with the default values.- pub fn fill_default_values(&mut self){- $(- if self.[<$linter:snake>].is_none() {- self.[<$linter:snake>] = Some($default);- }- )*-- if self.spell_check.is_none() {- self.spell_check = Some(true);- }- }- }+ /// Remove any configuration attached to a rule.+ /// This allows it to assume its default (curated) state.+ pub fn unset_rule_enabled(&mut self, key: impl AsRef<str>) {+ self.inner.remove_entry(key.as_ref());+ }- /// A wrapper that combines all built-in Harper linters- /// into a single, configurable [`Linter`].- pub struct LintGroup<T: Dictionary> {- $(- [<$linter:snake>]: $linter,- )*- spell_check: SpellCheck<T>,- pub config: LintGroupConfig- }+ pub fn set_rule_enabled_if_unset(&mut self, key: impl AsRef<str>, val: bool) {+ if self.inner.get(key.as_ref()).is_none() {+ self.set_rule_enabled(key.as_ref().to_string(), val);+ }+ }++ pub fn is_rule_enabled(&self, key: &str) -> bool {+ self.inner.get(key).cloned().flatten().unwrap_or(false)+ }+ /// Clear all config options.+ /// This will reset them all to disabled.+ pub fn clear(&mut self) {+ for val in self.inner.values_mut() {+ *val = None+ }+ }- impl<T: Dictionary> LintGroup<T> {- pub fn new(config: LintGroupConfig, dictionary: T) -> Self {- Self {- $(- [<$linter:snake>]: $linter::default(),- )*- spell_check: SpellCheck::new(dictionary),- config,- }- }-- pub fn all_descriptions(&self) -> LintGroupDescriptions<'_> {- LintGroupDescriptions {- $(- [<$linter:snake>]: self.[<$linter:snake>].description(),- )*- spell_check: self.spell_check.description(),- }- }+ /// Merge the contents of another [`LintGroupConfig`] into this one.+ /// The other config will be left empty after this operation.+ ///+ /// Conflicting keys will be overridden by the value in the other group.+ pub fn merge_from(&mut self, other: &mut LintGroupConfig) {+ for (key, val) in other.inner.drain() {+ if val.is_none() {+ continue;}- impl<T: Dictionary> Linter for LintGroup<T> {- fn lint(&mut self, document: &Document) -> Vec<Lint> {- let mut lints = Vec::new();+ self.inner.insert(key, val);+ }+ }++ /// Fill the group with the values for the curated lint group.+ pub fn fill_with_curated(&mut self) {+ let mut temp = Self::new_curated();+ mem::swap(self, &mut temp);+ self.merge_from(&mut temp);+ }- let mut config = self.config.clone();- config.fill_default_values();+ pub fn new_curated() -> Self {+ curated_config()+ }+ }++ #[derive(Default)]+ pub struct LintGroup {+ pub config: LintGroupConfig,+ /// We use a binary map here so the ordering is stable.+ inner: BTreeMap<String, Box<dyn Linter>>,+ }++ impl LintGroup {+ pub fn empty() -> Self {+ Self {+ config: LintGroupConfig::default(),+ inner: BTreeMap::new(),+ }+ }- $(- if config.[<$linter:snake>].unwrap() {- lints.append(&mut self.[<$linter:snake>].lint(document));- }- )*+ /// Add a [`Linter`] to the group, returning whether the operation was successful.+ /// If it returns `false`, it is because a linter with that key already existed in the group.+ pub fn add(&mut self, name: impl AsRef<str>, linter: Box<dyn Linter>) -> bool {+ if self.inner.contains_key(name.as_ref()) {+ false+ } else {+ self.inner.insert(name.as_ref().to_string(), linter);+ true+ }+ }- if config.spell_check.unwrap() {- lints.append(&mut self.spell_check.lint(document));- }+ /// Merge the contents of another [`LintGroup`] into this one.+ /// The other lint group will be left empty after this operation.+ pub fn merge_from(&mut self, other: &mut LintGroup) {+ self.config.merge_from(&mut other.config);+ let other_map = std::mem::take(&mut other.inner);- lints- }+ self.inner.extend(other_map);+ }- fn description(&self) -> &'static str {- "A collection of linters that can be run as one."- }+ /// Set all contained rules to a specific value.+ /// Passing `None` will unset that rule, allowing it to assume its default state.+ pub fn set_all_rules_to(&mut self, enabled: Option<bool>) {+ for key in self.inner.keys() {+ match enabled {+ Some(v) => self.config.set_rule_enabled(key, v),+ None => self.config.unset_rule_enabled(key),}}- };+ }++ pub fn all_descriptions(&self) -> HashMap<&str, &str> {+ self.inner+ .iter()+ .map(|(key, value)| (key.as_str(), value.description()))+ .collect()+ }++ /// Swap out [`Self::config`] with another [`LintGroupConfig`].+ pub fn with_lint_config(mut self, config: LintGroupConfig) -> Self {+ self.config = config;+ self+ }++ pub fn new_curated(dictionary: Arc<impl Dictionary + 'static>) -> Self {+ let mut out = Self::empty();++ macro_rules! insert_struct_rule {+ ($rule:ident, $default_config:expr) => {+ out.add(stringify!($rule), Box::new($rule::default()));+ out.config+ .set_rule_enabled(stringify!($rule), $default_config);+ };+ }++ out.merge_from(&mut phrase_corrections::lint_group());+ out.merge_from(&mut proper_noun_capitalization_linters::lint_group(+ dictionary.clone(),+ ));+ out.merge_from(&mut closed_compounds::lint_group());++ // Add all the more complex rules to the group.+ insert_struct_rule!(BackInTheDay, true);+ insert_struct_rule!(WordPressDotcom, true);+ insert_struct_rule!(OutOfDate, true);+ insert_struct_rule!(ThenThan, true);+ insert_struct_rule!(PiqueInterest, true);+ insert_struct_rule!(WasAloud, true);+ insert_struct_rule!(HyphenateNumberDay, true);+ insert_struct_rule!(LeftRightHand, true);+ insert_struct_rule!(HopHope, true);+ insert_struct_rule!(Hereby, true);+ insert_struct_rule!(Likewise, true);+ insert_struct_rule!(CompoundNouns, true);+ insert_struct_rule!(Nobody, true);+ insert_struct_rule!(Whereas, true);+ insert_struct_rule!(PossessiveYour, true);+ insert_struct_rule!(SpelledNumbers, false);+ insert_struct_rule!(AnA, true);+ insert_struct_rule!(SentenceCapitalization, true);+ insert_struct_rule!(UnclosedQuotes, true);+ insert_struct_rule!(WrongQuotes, false);+ insert_struct_rule!(LongSentences, true);+ insert_struct_rule!(RepeatedWords, true);+ insert_struct_rule!(Spaces, true);+ insert_struct_rule!(Matcher, true);+ insert_struct_rule!(CorrectNumberSuffix, true);+ insert_struct_rule!(NumberSuffixCapitalization, true);+ insert_struct_rule!(MultipleSequentialPronouns, true);+ insert_struct_rule!(LinkingVerbs, false);+ insert_struct_rule!(AvoidCurses, true);+ insert_struct_rule!(TerminatingConjunctions, true);+ insert_struct_rule!(EllipsisLength, true);+ insert_struct_rule!(DotInitialisms, true);+ insert_struct_rule!(BoringWords, false);+ insert_struct_rule!(UseGenitive, false);+ insert_struct_rule!(ThatWhich, true);+ insert_struct_rule!(CapitalizePersonalPronouns, true);+ insert_struct_rule!(MergeWords, true);+ insert_struct_rule!(PluralConjugate, false);+ insert_struct_rule!(OxfordComma, true);+ insert_struct_rule!(NoOxfordComma, false);+ insert_struct_rule!(PronounContraction, true);+ insert_struct_rule!(CurrencyPlacement, true);+ insert_struct_rule!(SomewhatSomething, true);+ insert_struct_rule!(LetsConfusion, true);+ insert_struct_rule!(DespiteOf, true);+ insert_struct_rule!(ChockFull, true);+ insert_struct_rule!(ExpandTimeShorthands, true);++ insert_struct_rule!(ModalOf, true);++ out.add("SpellCheck", Box::new(SpellCheck::new(dictionary)));+ out.config.set_rule_enabled("SpellCheck", true);++ out+ }++ /// Create a new curated group with all config values cleared out.+ pub fn new_curated_empty_config(dictionary: Arc<impl Dictionary + 'static>) -> Self {+ let mut group = Self::new_curated(dictionary);+ group.config.clear();+ group+ }}- create_lint_group_config!(- BackInTheDay => true,- WordPressDotcom => true,- DayOneNames => true,- PocketCastsNames => true,- TumblrNames => true,- JetpackNames => true,- OutOfDate => true,- Desktop => true,- Laptop => true,- ThenThan => true,- MutePoint => true,- PiqueInterest => true,- BareInMind => true,- BaitedBreath => true,- EludedTo => true,- WasAloud => true,- HyphenateNumberDay => true,- FaceFirst => true,- LeftRightHand => true,- FastPaste => true,- StateOfTheArt => true,- WantBe => true,- HopHope => true,- Furthermore => true,- Overnight => true,- Hereby => true,- Likewise => true,- CompoundNouns => true,- Regardless => true,- Henceforth => true,- Upward => true,- Whereupon => true,- Insofar => true,- Thereupon => true,- Nonetheless => true,- Anyhow => true,- Notwithstanding => true,- Widespread => true,- Multimedia => true,- Multicore => true,- Multithreading => true,- Devops => true,- Underclock => true,- Overload => true,- Backplane => true,- Overclocking => true,- Middleware => true,- Somewhere => true,- Instead => true,- Anywhere => true,- Nothing => true,- Anybody => true,- Somebody => true,- Nobody => true,- Into => true,- Proofread => true,- Somehow => true,- Intact => true,- Upset => true,- Misunderstood => true,- However => true,- Overall => true,- Worldwide => true,- Postpone => true,- Misused => true,- Misuse => true,- Misunderstand => true,- Therefore => true,- Myself => true,- Itself => true,- Whereas => true,- PossessiveYour => true,- SpelledNumbers => false,- AnA => true,- SentenceCapitalization => true,- UnclosedQuotes => true,- WrongQuotes => false,- LongSentences => true,- RepeatedWords => true,- Spaces => true,- Matcher => true,- CorrectNumberSuffix => true,- NumberSuffixCapitalization => true,- MultipleSequentialPronouns => true,- LinkingVerbs => false,- AvoidCurses => true,- TerminatingConjunctions => true,- EllipsisLength => true,- DotInitialisms => true,- BoringWords => false,- UseGenitive => false,- ThatWhich => true,- CapitalizePersonalPronouns => true,- Americas => true,- Australia => true,- Canada => true,- Koreas => true,- Malaysia => true,- ChineseCommunistParty => true,- UnitedOrganizations => true,- Holidays => true,- AmazonNames => true,- GoogleNames => true,- MetaNames => true,- MicrosoftNames => true,- AppleNames => true,- AzureNames => true,- MergeWords => true,- PluralConjugate => false,- OxfordComma => true,- NoOxfordComma => false,- PronounContraction => true,- CurrencyPlacement => true,- SomewhatSomething => true,- LetsConfusion => true,- DespiteOf => true,- ChockFull => true,- HumanLife => true,- NeedHelp => true,- NoLonger => true,- ThatChallenged => true,- TurnItOff => true,- OfCourse => true,- AndTheLike => true,- BadRap => true,- BatedBreath => true,- BeckAndCall => true,- ChangeTack => true,- HungerPang => true,- EnMasse => true,- LetAlone => true,- LoAndBehold => true,- SneakingSuspicion => true,- SpecialAttention => true,- Everywhere => true,- ThanOthers => true,- SupposedTo => true,- ModalOf => true,- );-- impl<T: Dictionary + Default> Default for LintGroup<T> {- fn default() -> Self {- Self::new(LintGroupConfig::default(), T::default())+ impl Linter for LintGroup {+ fn lint(&mut self, document: &Document) -> Vec<Lint> {+ let mut results = Vec::new();++ for (key, linter) in &mut self.inner {+ if self.config.is_rule_enabled(key) {+ results.extend(linter.lint(document));+ }+ }++ results+ }++ fn description(&self) -> &str {+ "A collection of linters that can be run as one."}}diff --cc harper-core/src/linting/mod.rsindex ef3cdce74,e04df095b..5a8f66d1b--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@@ -95,9 -86,9 +87,10 @@@ pub use lint::Lintpub use lint_group::{LintGroup, LintGroupConfig};pub use lint_kind::LintKind;pub use long_sentences::LongSentences;+ pub use map_phrase_linter::MapPhraseLinter;pub use matcher::Matcher;pub use merge_words::MergeWords;+pub use modal_of::ModalOf;pub use multiple_sequential_pronouns::MultipleSequentialPronouns;pub use no_oxford_comma::NoOxfordComma;pub use nobody::Nobody;diff --cc harper-core/src/linting/modal_of.rsindex e9e1a01e0,000000000..15d7bc844mode 100644,000000..100644--- a/harper-core/src/linting/modal_of.rs+++ b/harper-core/src/linting/modal_of.rs@@@ -1,226 -1,0 +1,230 @@@+use crate::{- patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},+ Lrc, Token, TokenStringExt,++ patterns::{OwnedPatternExt, Pattern, SequencePattern, WordSet},+};++use super::{Lint, LintKind, PatternLinter, Suggestion};++pub struct ModalOf {+ pattern: Box<dyn Pattern>,+}++impl Default for ModalOf {+ fn default() -> Self {+ // Note 1. "shan't of" is plausible but very unlikely+ // Note 2. "had of" has trickier false positives and is less common anyway+ // "The only other report we've had of this kind of problem ..."+ // "The code I had of this used to work fine ..."+ let modals = ["could", "might", "must", "should", "would"];- let mut words = WordSet::all(&modals);++ let mut words = WordSet::new(&modals);+ modals.iter().for_each(|word| {+ words.add(&format!("{}n't", word));+ });++ let modal_of = Lrc::new(+ SequencePattern::default()- .then(Box::new(words))++ .then(words)+ .then_whitespace()+ .then_exact_word("of"),+ );++ let ws_course = Lrc::new(+ SequencePattern::default()+ .then_whitespace()+ .then_exact_word("course"),+ );++ Self {+ pattern: Box::new(+ SequencePattern::default()- .then(Box::new(modal_of.clone()))- .then(Box::new(ws_course.clone()))++ .then(modal_of.clone())++ .then(ws_course.clone())+ .or(Box::new(modal_of.clone())),+ ),+ }+ }+}++impl PatternLinter for ModalOf {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_toks: &[Token], source_chars: &[char]) -> Option<Lint> {+ if matched_toks.len() != 3 {+ return None;+ }++ let span_modal_of = matched_toks[0..3].span().unwrap();+ let span_modal = matched_toks[0].span;++ let modal_have = format!("{} have", span_modal.get_content_string(source_chars))+ .chars()+ .collect();+ let modal_ws_of = span_modal_of.get_content(source_chars);++ Some(Lint {+ span: span_modal_of,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(modal_have, modal_ws_of)],+ message: "Use `have` rather than `of` here.".to_string(),+ priority: 126,+ })+ }++ fn description(&self) -> &'static str {+ "Detects `of` mistakenly used with `would`, `could`, `should`, etc."+ }+}++#[cfg(test)]+mod tests {+ use super::ModalOf;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ // atomic unit tests++ #[test]+ fn test_lowercase() {+ assert_suggestion_result("could of", ModalOf::default(), "could have");+ }++ #[test]+ fn test_negative() {+ assert_suggestion_result("mightn't of", ModalOf::default(), "mightn't have");+ }++ #[test]+ fn test_uppercase_negative() {+ assert_suggestion_result("Mustn't of", ModalOf::default(), "Mustn't have");+ }++ #[test]+ fn test_false_positive() {+ assert_lint_count("should of course", ModalOf::default(), 0);+ }++ #[test]+ fn test_false_positive_capital_negative() {+ assert_lint_count("Wouldn't of course", ModalOf::default(), 0);+ }++ // real-world tests++ #[test]+ fn test_buggy_implementation() {+ assert_lint_count(+ "... could of just been a buggy implementation",+ ModalOf::default(),+ 1,+ );+ }++ #[test]+ fn test_missed_one() {+ assert_lint_count(+ "We already have a function ... that nedb can understand so we might of missed one.",+ ModalOf::default(),+ 1,+ );+ }++ #[test]+ fn test_user_option() {+ assert_lint_count(+ "im more likely to believe you might of left in the 'user' option",+ ModalOf::default(),+ 1,+ );+ }++ #[test]+ fn catches_must_of() {+ assert_suggestion_result(+ "Ah I must of missed that part.",+ ModalOf::default(),+ "Ah I must have missed that part.",+ );+ }++ #[test]+ fn catches_should_of() {+ assert_lint_count(+ "Yeah I should of just mentioned it should of been a for of.",+ ModalOf::default(),+ 2,+ );+ }++ #[test]+ fn catches_would_of() {+ assert_suggestion_result(+ "now this issue would of caused hundreds of thousands of extra lines",+ ModalOf::default(),+ "now this issue would have caused hundreds of thousands of extra lines",+ );+ }++ #[test]+ fn doesnt_catch_you_could_of_course() {+ assert_lint_count(+ "You could of course explicit the else with each possibility",+ ModalOf::default(),+ 0,+ );+ }++ #[test]+ fn doesnt_catch_compiler_could_of_course() {+ assert_lint_count(+ "The compiler could of course detect this too",+ ModalOf::default(),+ 0,+ );+ }++ #[test]+ fn doesnt_catch_might_of_course_be() {- assert_lint_count("There might of course be other places where not implementing the IMemberSource might break ...", ModalOf::default(), 0);++ assert_lint_count(++ "There might of course be other places where not implementing the IMemberSource might break ...",++ ModalOf::default(),++ 0,++ );+ }++ #[test]+ fn doesnt_catch_not_a_must_of_course() {+ assert_lint_count(+ "Not a must of course if the convention should be .ts",+ ModalOf::default(),+ 0,+ );+ }++ #[test]+ fn doesnt_catch_must_of_course_also() {+ assert_lint_count(+ "the schedular must of course also have run through",+ ModalOf::default(),+ 0,+ );+ }++ #[test]+ fn doesnt_catch_should_of_course_not() {+ assert_lint_count(+ "not being local should of course not be supported",+ ModalOf::default(),+ 0,+ );+ }++ #[test]+ fn doesnt_catch_would_of_course_just() {+ assert_lint_count(+ "I would of course just test this by compiling with MATX_MULTI_GPU=ON",+ ModalOf::default(),+ 0,+ );+ }+}