harper-core 2.7.0

The language checker for developers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use std::sync::Arc;

use hashbrown::HashSet;

use crate::expr::Expr;
use crate::linting::{
    ExprLinter, LintKind, Suggestion,
    expr_linter::{Chunk, at_start_of_sentence, preceded_by_word},
    informal_laughter::is_informal_laughter,
};
use crate::spell::{Dictionary, FstDictionary, TrieDictionary};
use crate::{Lint, Token};

pub struct SplitWords {
    dict: Arc<TrieDictionary<Arc<FstDictionary>>>,
    expr: Box<dyn Expr>,
}

impl SplitWords {
    pub fn new() -> Self {
        Self {
            dict: TrieDictionary::curated(),
            expr: Box::new(|tok: &Token, _: &[char]| tok.kind.is_word()),
        }
    }
}

impl Default for SplitWords {
    fn default() -> Self {
        Self::new()
    }
}

impl ExprLinter for SplitWords {
    type Unit = Chunk;

    fn description(&self) -> &str {
        "Finds missing spaces in improper compound words."
    }

    fn expr(&self) -> &dyn Expr {
        self.expr.as_ref()
    }

    fn match_to_lint_with_context(
        &self,
        matched_tokens: &[Token],
        source: &[char],
        context: Option<(&[Token], &[Token])>,
    ) -> Option<Lint> {
        let word = &matched_tokens[0];

        // If it's a recognized word, we don't care about it.
        if word.kind.as_word().unwrap().is_some() {
            return None;
        }

        let chars = &word.get_ch(source);
        if is_informal_laughter(chars) {
            return None;
        }

        // Get all possible prefix candidates from trie and extract valid split positions
        let candidates = self.dict.find_words_with_common_prefix(chars);
        let len = chars.len();
        let mut valid_positions: HashSet<usize> = HashSet::new();

        for candidate in candidates {
            if candidate.len() >= len {
                continue;
            }
            valid_positions.insert(candidate.len());
        }

        // Generate middle-outward position order based on heuristic from PR #885:
        // Missing spaces are more likely near the middle of a word
        let mid = len / 2;
        let mut positions: Vec<usize> = Vec::new();
        positions.push(mid);

        for offset in 1..len {
            if mid >= offset {
                positions.push(mid - offset);
            }
            if mid + offset < len {
                positions.push(mid + offset);
            }
        }

        let mut suggestions = Vec::new();
        let mut has_anchor_split = false;
        let mut message: Option<String> = None;

        // Check positions in middle-outward order
        for split_pos in positions {
            if split_pos == 0 || split_pos >= len || !valid_positions.contains(&split_pos) {
                continue;
            }

            let candidate = &chars[..split_pos];
            let remainder = &chars[split_pos..];

            // Both parts must be valid common words
            let Some(cand_meta) = self.dict.get_word_metadata(candidate) else {
                continue;
            };
            if !cand_meta.common {
                continue;
            }

            let Some(rem_meta) = self.dict.get_word_metadata(remainder) else {
                continue;
            };
            if !rem_meta.common {
                continue;
            }

            if is_anchor_split(&cand_meta, candidate) || is_anchor_split(&rem_meta, remainder) {
                has_anchor_split = true;
            }

            // Valid split found
            let mut suggestion = Vec::new();
            suggestion.extend(candidate.iter());
            suggestion.push(' ');
            suggestion.extend(remainder.iter());

            suggestions.push(Suggestion::ReplaceWith(suggestion));
            if suggestions.len() == 1 {
                let certainty = if candidate.len() == 1 || remainder.len() == 1 {
                    "possibly"
                } else {
                    "probably"
                };
                message = Some(format!(
                    "`{}` should {certainty} be written as `{} {}`.",
                    chars.iter().collect::<String>(),
                    candidate.iter().collect::<String>(),
                    remainder.iter().collect::<String>()
                ));
            }
        }

        if !suggestions.is_empty() {
            let original_word: String = chars.iter().collect();

            if should_defer_to_spellcheck(&self.dict, chars, has_anchor_split, context) {
                return None;
            }

            if suggestions.len() != 1 {
                message = Some(format!(
                    "`{original_word}` has a missing space between words."
                ));
            }

            return Some(Lint {
                span: word.span,
                lint_kind: LintKind::Typo,
                suggestions,
                message: message?,
                priority: 31,
            });
        }

        None
    }
}

fn is_anchor_split(meta: &crate::DictWordMetadata, word: &[char]) -> bool {
    meta.preposition
        || meta.is_determiner()
        || meta.is_conjunction()
        || meta.is_pronoun()
        || meta.is_adverb()
        || word.len() <= 2
}

fn should_defer_to_spellcheck(
    dict: &TrieDictionary<Arc<FstDictionary>>,
    chars: &[char],
    has_anchor_split: bool,
    context: Option<(&[Token], &[Token])>,
) -> bool {
    if has_anchor_split {
        return false;
    }

    let nounish_context = context.is_some_and(|_| {
        at_start_of_sentence(context)
            || preceded_by_word(context, |tok| {
                tok.kind.is_determiner()
                    || tok.kind.is_pronoun()
                    || tok.kind.is_adjective()
                    || tok.kind.is_possessive_determiner()
            })
    });

    if !nounish_context {
        return false;
    }

    // If the whole word has a strong one-word correction, prefer that over a content-word split.
    dict.fuzzy_match(chars, 1, 1)
        .first()
        .is_some_and(|suggestion| suggestion.edit_distance == 1)
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;

    use crate::Document;
    use crate::linting::tests::{
        assert_good_and_bad_suggestions, assert_lint_message, assert_no_lints,
        assert_suggestion_result,
    };
    use crate::linting::{Linter, Suggestion};

    use super::SplitWords;

    #[test]
    fn issue_1905() {
        assert_suggestion_result(
            "I want to try this insteadof that.",
            SplitWords::default(),
            "I want to try this instead of that.",
        );
    }

    /// Same as above, but with the longer component word at the end.
    #[test]
    fn issue_1905_rev() {
        assert_suggestion_result(
            "I want to try thisinstead of that.",
            SplitWords::default(),
            "I want to try this instead of that.",
        );
    }

    #[test]
    fn split_common() {
        assert_suggestion_result(
            "This is notnot a problem.",
            SplitWords::default(),
            "This is not not a problem.",
        );
    }

    #[test]
    fn splits_multiple_compound_words() {
        assert_suggestion_result(
            "We stared intothe darkness and kindof panicked about sortof everything.",
            SplitWords::default(),
            "We stared into the darkness and kind of panicked about sort of everything.",
        );
    }

    #[test]
    fn splits_word_with_longer_prefix() {
        assert_suggestion_result(
            "The astronauts waited on the landingpad for hours.",
            SplitWords::default(),
            "The astronauts waited on the landing pad for hours.",
        );
    }

    #[test]
    fn splits_before_punctuation() {
        assert_suggestion_result(
            "This was kindof, actually, hilarious.",
            SplitWords::default(),
            "This was kind of, actually, hilarious.",
        );
    }

    #[test]
    fn ignores_known_compound_words() {
        assert_no_lints("Someone left early.", SplitWords::default());
    }

    #[test]
    fn ignores_prefix_without_valid_remainder() {
        assert_no_lints("The monkeyxyz escaped unnoticed.", SplitWords::default());
    }

    #[test]
    fn ignores_single_word_misspelling_with_split_like_halves() {
        assert_no_lints("I love this extention!", SplitWords::default());
    }

    #[test]
    fn corrects_doesthe() {
        assert_suggestion_result("doesthe", SplitWords::default(), "does the");
    }

    #[test]
    fn corrects_splitwords() {
        assert_suggestion_result("splitwords", SplitWords::default(), "split words");
    }

    #[test]
    fn test_atall_to_at_all() {
        assert_suggestion_result(
            "don't seem to support symbolic links atall.",
            SplitWords::default(),
            "don't seem to support symbolic links at all.",
        );
    }

    #[test]
    fn test_atall_to_a_tall() {
        assert_suggestion_result("atall", SplitWords::default(), "a tall");
    }

    #[test]
    fn atall_should_split_to_a_tall_and_at_all() {
        assert_good_and_bad_suggestions("atall", SplitWords::default(), &["a tall", "at all"], &[]);
    }

    #[test]
    fn issue_2763_leaves() {
        assert_suggestion_result(
            "I love to eat cornleaves.",
            SplitWords::default(),
            "I love to eat corn leaves.",
        );
    }

    #[test]
    fn issue_2763_husks() {
        assert_suggestion_result(
            "I love to eat cornhusks.",
            SplitWords::default(),
            "I love to eat corn husks.",
        );
    }

    #[test]
    fn issue_2763_singular() {
        assert_suggestion_result(
            "I would love to eat a cornleaf.",
            SplitWords::default(),
            "I would love to eat a corn leaf.",
        );
    }

    #[test]
    fn not_confident_proc_should_be_pro_c() {
        assert_lint_message(
            "proc",
            SplitWords::default(),
            "`proc` should possibly be written as `pro c`.",
        );
    }

    #[test]
    fn confident_thankyou_should_be_thank_you() {
        assert_lint_message(
            "thankyou",
            SplitWords::default(),
            "`thankyou` should probably be written as `thank you`.",
        );
    }

    #[test]
    fn allows_informal_laughter() {
        for source in ["hah", "haha", "hahah", "hahaha", "Hahahah"] {
            assert_no_lints(source, SplitWords::default());
        }
    }

    #[test]
    fn does_not_split_iff() {
        assert_no_lints("iff", SplitWords::default());
    }

    /// Checks for a condition where the SplitWords rule would correct a word to be composed of a
    /// single letter (which is not a word), followed by a valid word.
    ///
    /// For example, `comitted` -> `c omitted`.
    #[test]
    fn never_corrects_to_invalid_single_letter_words() {
        let triggers = [
            "comitted", "testc", "testh", "testb", "testq", "testx", "testg", "teste", "testj",
            "shes",
        ];
        let relevant_letters = ['c', 'd', 't', 'h', 'b', 'x', 'e', 'j', 's'];

        for trigger in triggers {
            let mut rule = SplitWords::default();

            let doc = Document::new_plain_english_curated(trigger);
            let lints = rule.lint(&doc);

            for lint in lints {
                dbg!(&lint);

                for sug in lint.suggestions {
                    match sug {
                        Suggestion::ReplaceWith(items) => {
                            // Words created by the rule.
                            let created_words = items.split(|c| c == &' ');

                            for word in created_words {
                                if word.len() == 1
                                    && relevant_letters.iter().contains(&word.first().unwrap())
                                {
                                    panic!("Encountered bad output {word:?}")
                                }
                            }
                        }
                        _ => (),
                    }
                }
            }
        }
    }
}