harper-core 2.0.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
420
421
422
423
424
425
426
427
428
429
430
use harper_brill::UPOS;

use crate::expr::{All, AnchorStart, Expr, ExprMap, SequenceExpr};
use crate::patterns::{NominalPhrase, UPOSSet};
use crate::{Lrc, Token, TokenKind, TokenStringExt};

use super::Suggestion;
use super::{ExprLinter, Lint, LintKind};
use crate::linting::expr_linter::Chunk;

pub struct PronounInflectionBe {
    expr: All,
    map: Lrc<ExprMap<&'static str>>,
}

impl PronounInflectionBe {
    pub fn new() -> Self {
        let mod_term = Lrc::new(
            SequenceExpr::default()
                .t_ws()
                .then(UPOSSet::new(&[UPOS::ADJ, UPOS::ADV])),
        );

        let mut map = ExprMap::default();

        let are = SequenceExpr::default()
            .then_third_person_singular_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("are")
            .t_any()
            .then_unless(NominalPhrase);
        map.insert(are, "is");

        let are_at_start = SequenceExpr::with(AnchorStart)
            .then_third_person_singular_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("are")
            .t_any()
            .t_any();
        map.insert(are_at_start, "is");

        let arent = SequenceExpr::default()
            .then_third_person_singular_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("aren't")
            .t_any()
            .t_any();
        map.insert(arent, "isn't");

        let is = SequenceExpr::default()
            .then_kind_where(|kind| {
                kind.as_word()
                    .as_ref()
                    .and_then(|m| m.as_ref().and_then(|m| m.np_member))
                    .unwrap_or_default()
            })
            .then_whitespace()
            .then_third_person_plural_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("is")
            .t_any()
            .t_any();
        map.insert(is, "are");

        let is_at_start = SequenceExpr::with(AnchorStart)
            .then_third_person_plural_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("is")
            .t_any()
            .t_any();
        map.insert(is_at_start, "are");

        let isnt = SequenceExpr::default()
            .then_third_person_plural_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("isn't")
            .t_any()
            .t_any();
        map.insert(isnt, "aren't");

        let was = SequenceExpr::default()
            .then_first_person_plural_pronoun()
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("was")
            .t_any()
            .t_any();
        map.insert(was, "were");

        // Special case for second and third-person
        let was_third = SequenceExpr::with(AnchorStart)
            .then_kind_either(
                TokenKind::is_third_person_plural_pronoun,
                TokenKind::is_second_person_pronoun,
            )
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("was")
            .t_any()
            .t_any();
        map.insert(was_third, "were");

        let were = SequenceExpr::with(AnchorStart)
            .then_kind_either(
                TokenKind::is_first_person_singular_pronoun,
                TokenKind::is_third_person_singular_pronoun,
            )
            .then_optional(mod_term.clone())
            .t_ws()
            .t_aco("were")
            .t_any()
            .t_any();

        map.insert(were, "was");

        let map = Lrc::new(map);

        let mut all = All::default();
        all.add(map.clone());
        all.add(|tok: &Token, _: &[char]| tok.kind.is_upos(UPOS::PRON));

        Self { expr: all, map }
    }
}

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

impl ExprLinter for PronounInflectionBe {
    type Unit = Chunk;

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

    fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
        let span = matched_tokens.get_rel(-3)?.span;

        // Determine the correct inflection of "be".
        let correct = self.map.lookup(0, matched_tokens, source)?;

        Some(Lint {
            span,
            lint_kind: LintKind::Agreement,
            suggestions: vec![Suggestion::replace_with_match_case_str(
                correct,
                span.get_content(source),
            )],
            message: "Make the verb agree with its subject.".to_owned(),
            priority: 30,
        })
    }
    fn description(&self) -> &str {
        "Checks subject–verb agreement for the verb `be`. Third-person singular \
         pronouns (`he`, `she`, `it`) require the singular form `is`, while the \
         plural pronoun `they` takes `are`. The linter flags mismatches such as \
         `He are` or `They is` and offers the correct concord."
    }
}

#[cfg(test)]
mod tests {
    use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};

    use super::PronounInflectionBe;

    #[test]
    fn corrects_he_are() {
        assert_suggestion_result(
            "He are my best friend.",
            PronounInflectionBe::default(),
            "He is my best friend.",
        );
    }

    #[test]
    fn corrects_she_are() {
        assert_suggestion_result(
            "She are my best friend.",
            PronounInflectionBe::default(),
            "She is my best friend.",
        );
    }

    #[test]
    fn corrects_they_is() {
        assert_suggestion_result(
            "They is my best friend.",
            PronounInflectionBe::default(),
            "They are my best friend.",
        );
    }

    #[test]
    fn allows_they_are() {
        assert_lint_count(
            "They are my best friend.",
            PronounInflectionBe::default(),
            0,
        );
    }

    #[test]
    fn corrects_it_are() {
        assert_suggestion_result(
            "It are on the table.",
            PronounInflectionBe::default(),
            "It is on the table.",
        );
    }

    #[test]
    fn corrects_he_are_negation() {
        assert_suggestion_result(
            "He are not amused.",
            PronounInflectionBe::default(),
            "He is not amused.",
        );
    }

    #[test]
    fn corrects_she_are_progressive() {
        assert_suggestion_result(
            "She are going to win.",
            PronounInflectionBe::default(),
            "She is going to win.",
        );
    }

    #[test]
    fn corrects_they_is_negation() {
        assert_suggestion_result(
            "They is not ready.",
            PronounInflectionBe::default(),
            "They are not ready.",
        );
    }

    #[test]
    fn corrects_they_is_progressive() {
        assert_suggestion_result(
            "They is planning a trip.",
            PronounInflectionBe::default(),
            "They are planning a trip.",
        );
    }

    #[test]
    fn allows_he_is() {
        assert_lint_count("He is my best friend.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_she_is_lowercase() {
        assert_lint_count("she is excited to go.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_it_is() {
        assert_lint_count("It is what it is.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_they_are_negation() {
        assert_lint_count(
            "They are not interested.",
            PronounInflectionBe::default(),
            0,
        );
    }

    #[test]
    fn allows_they_were() {
        assert_lint_count("They were already here.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_asdf_is() {
        assert_lint_count("asdf is not a word", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn no_subject() {
        assert_lint_count("is set", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn corrects_i_were() {
        assert_suggestion_result(
            "I were the best player on the field.",
            PronounInflectionBe::default(),
            "I was the best player on the field.",
        );
    }

    #[test]
    fn corrects_we_was() {
        assert_suggestion_result(
            "We was the best players on the field.",
            PronounInflectionBe::default(),
            "We were the best players on the field.",
        );
    }

    #[test]
    fn corrects_you_was() {
        assert_suggestion_result(
            "You was my best friend.",
            PronounInflectionBe::default(),
            "You were my best friend.",
        );
    }

    #[test]
    fn allows_you_were() {
        assert_lint_count(
            "You were my best friend.",
            PronounInflectionBe::default(),
            0,
        );
    }

    #[test]
    fn corrects_he_were() {
        assert_suggestion_result(
            "He were late.",
            PronounInflectionBe::default(),
            "He was late.",
        );
    }

    #[test]
    fn corrects_they_was() {
        assert_suggestion_result(
            "They was on time.",
            PronounInflectionBe::default(),
            "They were on time.",
        );
    }

    #[test]
    fn allows_he_was() {
        assert_lint_count("He was here.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_we_were() {
        assert_lint_count("We were excited.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn corrects_he_arent() {
        assert_suggestion_result(
            "He aren't ready.",
            PronounInflectionBe::default(),
            "He isn't ready.",
        );
    }

    #[test]
    fn corrects_they_isnt() {
        assert_suggestion_result(
            "They isn't coming.",
            PronounInflectionBe::default(),
            "They aren't coming.",
        );
    }

    #[test]
    fn allows_he_isnt() {
        assert_lint_count("He isn't ready.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn allows_they_arent() {
        assert_lint_count("They aren't coming.", PronounInflectionBe::default(), 0);
    }

    #[test]
    fn corrects_she_really_are() {
        assert_suggestion_result(
            "She really are talented.",
            PronounInflectionBe::default(),
            "She really is talented.",
        );
    }

    #[test]
    fn corrects_they_often_is() {
        assert_suggestion_result(
            "They often is late.",
            PronounInflectionBe::default(),
            "They often are late.",
        );
    }

    #[test]
    fn corrects_because_he_are() {
        assert_suggestion_result(
            "because he are tired.",
            PronounInflectionBe::default(),
            "because he is tired.",
        );
    }

    #[test]
    fn allow_behind_him() {
        assert_no_lints(
            "Behind him are new shadows.",
            PronounInflectionBe::default(),
        );
    }

    #[test]
    fn issue_1682() {
        assert_no_lints(
            "Understanding them is significant",
            PronounInflectionBe::default(),
        );
    }
}