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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
use crate::{
    CharStringExt, Token,
    expr::{All, Expr, OwnedExprExt, SequenceExpr},
    linting::{
        ExprLinter, Lint, LintGroup, LintKind, Suggestion,
        expr_linter::{Chunk, followed_by_hyphen, followed_by_word, preceded_by_word},
    },
    patterns::{Word, WordSet},
};

/// A linter that handles "be + verb → be + adjective" confusions
struct BeAdjectiveLinter {
    expr: All,
    verb: &'static str,
    adjective: &'static str,
    not_if_followed_by: &'static [&'static str],
    message: &'static str,
}

impl BeAdjectiveLinter {
    fn new(
        verb: &'static str,
        adjective: &'static str,
        exception_words: &'static [&'static str],
        message: &'static str,
    ) -> Self {
        Self {
            expr: SequenceExpr::default()
                .then_any_of(vec![
                    Box::new(
                        SequenceExpr::default()
                            .then_subject_pronoun()
                            .t_ws()
                            .t_set(&["am", "are", "is", "was", "were"]),
                    ),
                    Box::new(WordSet::new(&[
                        // Correct contractions
                        "i'm", "we're", "you're", "he's", "she's", "they're",
                        // Incorrect contractions missing their apostrophes that should not cause problems
                        "im",
                        "theyre",
                        // Note that "were" is already included as a form of "be" above
                    ])),
                ])
                .t_ws()
                .t_aco(verb)
                // So far we prohibit "it" as a possible subject.
                // "it is worry/concern" is often grammatically legitimate
                // and would cause false positives.
                // Future improvements could add "it" support with
                // more sophisticated context analysis.
                .and_not(Word::new("it")),
            verb,
            adjective,
            message,
            not_if_followed_by: exception_words,
        }
    }
}

impl ExprLinter for BeAdjectiveLinter {
    type Unit = Chunk;

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

    fn match_to_lint_with_context(
        &self,
        toks: &[Token],
        src: &[char],
        ctx: Option<(&[Token], &[Token])>,
    ) -> Option<Lint> {
        let wtok = toks.last()?;

        if followed_by_hyphen(ctx)
            || preceded_by_word(ctx, |w| w.get_ch(src).eq_str("there"))
            || followed_by_word(ctx, |w| {
                w.get_ch(src)
                    .eq_any_ignore_ascii_case_str(self.not_if_followed_by)
            })
        {
            return None;
        }

        Some(Lint {
            span: wtok.span,
            lint_kind: LintKind::Usage,
            suggestions: vec![Suggestion::replace_with_match_case_str(
                self.adjective,
                wtok.span.get_content(src),
            )],
            message: self.message.to_string(),
            ..Default::default()
        })
    }

    fn description(&self) -> &'static str {
        "Detects incorrect use of 'be + verb' instead of `be + adjective`."
    }
}

pub fn lint_group() -> LintGroup {
    let mut group = LintGroup::empty();

    macro_rules! add_be_adjective_mappings {
        ($group:expr, { $($name:expr => ($verb:expr, $adjective:expr, $exceptions:expr, $message:expr)),+ $(,)? }) => {
            $(
                $group.add(
                    $name,
                    Box::new(BeAdjectiveLinter::new($verb, $adjective, $exceptions, $message)),
                );
            )+
        };
    }

    add_be_adjective_mappings!(group, {
        "BeBiased" => ("bias", "biased", &["adjusted", "corrected", "correcting", "field", "free", "values"], "Use the adjective 'biased' instead of the noun 'bias'."),
        "BeConcerned" => ("concern", "concerned", &[], "Use the adjective 'concerned' instead of the noun 'concern'."),
        "BePrejudiced" => ("prejudice", "prejudiced", &[], "Use the adjective 'prejudiced' instead of the noun 'prejudice'."),
        "BeShocked" => ("shock", "shocked", &["absorbers", "jocks", "resistant", "sensitive"], "Use the adjective 'shocked' instead of the noun 'shock'."),
        "BeWorried" => ("worry", "worried", &["free", "wart", "warts"], "Use the adjective 'worried' instead of the verb 'worry'."),
    });

    group.set_all_rules_to(Some(true));
    group
}

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

        use super::super::lint_group;

        #[test]
        fn he_s_bias() {
            assert_suggestion_result(
                "he's just on a rush to write a rejection letter probably because he's bias toward the origin or affiliation of the author",
                lint_group(),
                "he's just on a rush to write a rejection letter probably because he's biased toward the origin or affiliation of the author",
            );
        }

        #[test]
        fn i_am_bias() {
            assert_suggestion_result(
                "I guess I am bias/prefer udd-style fragments",
                lint_group(),
                "I guess I am biased/prefer udd-style fragments",
            );
        }

        #[test]
        fn i_m_bias() {
            assert_suggestion_result(
                "I'm bias towards vunit as it's by far the most user friendly HDL test runner I know of.",
                lint_group(),
                "I'm biased towards vunit as it's by far the most user friendly HDL test runner I know of.",
            );
        }

        #[test]
        fn im_bias() {
            assert_suggestion_result(
                "im bias, cause thats all im here for",
                lint_group(),
                "im biased, cause thats all im here for",
            );
        }

        #[test]
        fn they_re_bias() {
            assert_suggestion_result(
                "See, I told you they’re bias because they flagged my comment!",
                lint_group(),
                "See, I told you they’re biased because they flagged my comment!",
            );
        }

        // This one could also be corrected by a they're->their rule: "to admit their bias upfront"
        #[test]
        fn they_re_bias_could_also_be_their_bias() {
            assert_suggestion_result(
                "As a general rule I try to steer clear of the major news media outlets because of their outright refusal to admit they're bias upfront.",
                lint_group(),
                "As a general rule I try to steer clear of the major news media outlets because of their outright refusal to admit they're biased upfront.",
            );
        }

        #[test]
        #[ignore = "The linter does not yet support the subject being a noun phrase"]
        fn were_bias() {
            assert_suggestion_result(
                "the generated sample were bias in presence of negative cross-section",
                lint_group(),
                "the generated sample were biased in presence of negative cross-section",
            );
        }

        // Negative tests for "bias"

        #[test]
        fn dont_flag_bias_hyphen_corrected() {
            assert_no_lints("I gather they are bias-corrected estimates.", lint_group());
        }

        #[test]
        fn dont_flag_bias_space_corrected() {
            assert_no_lints(
                "All the images were bias corrected using N4 algorithm with a threshold value of 0.001.",
                lint_group(),
            );
        }

        #[test]
        fn dont_flag_its_bias_free() {
            assert_no_lints(
                "Both the original model, and its bias-free counterpart, will be loaded.",
                lint_group(),
            );
        }

        #[test]
        fn dont_flag_if_there_were_bias() {
            assert_no_lints(
                "which depend on several variables: if there were bias, the average size of the classes, as well as the number of teachers at the school",
                lint_group(),
            );
        }
    }

    mod concerned {
        use crate::linting::tests::{assert_no_lints, assert_suggestion_result};

        use super::super::lint_group;

        #[test]
        fn hes_concern() {
            assert_suggestion_result(
                "He's concern about my mental health because I'm alone in this city",
                lint_group(),
                "He's concerned about my mental health because I'm alone in this city",
            );
        }

        #[test]
        fn i_am_concern() {
            assert_suggestion_result(
                "I am learning to use this, but I am concern on data privacy and sharing issues.",
                lint_group(),
                "I am learning to use this, but I am concerned on data privacy and sharing issues.",
            );
        }

        #[test]
        fn im_concern() {
            assert_suggestion_result(
                "I'm concern if there is missing CVEs.",
                lint_group(),
                "I'm concerned if there is missing CVEs.",
            );
        }

        #[test]
        fn im_no_apostrophe_concern() {
            assert_suggestion_result(
                "The only thing im concern about is the crash.",
                lint_group(),
                "The only thing im concerned about is the crash.",
            );
        }

        #[test]
        #[ignore = "`Its` is currently not flagged because it's not always an error."]
        fn its_no_apostrophe_concern() {
            assert_suggestion_result(
                "yes no one there answered and I thought its concern with this repo too so I clone it here!",
                lint_group(),
                "yes no one there answered and I thought its concerned with this repo too so I cloned it here!",
            );
        }

        #[test]
        fn shes_concern() {
            assert_suggestion_result(
                "she often courier food to her 40 year old son living in Jeju island because she’s concern about her son’s well being",
                lint_group(),
                "she often courier food to her 40 year old son living in Jeju island because she’s concerned about her son’s well being",
            );
        }

        #[test]
        fn were_concern() {
            assert_suggestion_result(
                "As far as we're concern, this is a very simple try catch on your end",
                lint_group(),
                "As far as we're concerned, this is a very simple try catch on your end",
            );
        }

        #[test]
        fn we_are_concern() {
            assert_suggestion_result(
                "We are concern and prioritize users' feedback and maintainability.",
                lint_group(),
                "We are concerned and prioritize users' feedback and maintainability.",
            );
        }

        #[test]
        fn you_are_concern() {
            assert_suggestion_result(
                "you are concern only in logging a more appropriate message error, but continue to throw the opaque assertion error.",
                lint_group(),
                "you are concerned only in logging a more appropriate message error, but continue to throw the opaque assertion error.",
            );
        }

        // Negative tests for "concern"

        #[test]
        fn dont_flag_noun_concern() {
            assert_no_lints(
                "My main concern is the data that could be lost using this deletion.",
                lint_group(),
            );
        }

        #[test]
        fn dont_flag_verb_concern() {
            assert_no_lints("This doesn't concern me at all.", lint_group());
        }

        #[test]
        fn dont_flag_concern_free() {
            assert_no_lints("They don't pretend they're concern-free.", lint_group());
        }

        #[test]
        fn dont_flag_legit_its_concern() {
            assert_no_lints(
                "The ByteBlockPool loses its reference to the buffers it's just zeroed, so it seems outside its concern to zero the buffers.",
                lint_group(),
            );
        }

        #[test]
        #[ignore = "This is a different error that should be caught by `ShouldContract`"]
        fn dont_flag_youre_your_error() {
            assert_no_lints(
                "You're concern then is literally the \"-like\" in the message?",
                lint_group(),
            );
        }
    }

    mod prejudiced {
        use crate::linting::tests::assert_suggestion_result;

        use super::super::lint_group;

        #[test]
        fn i_was_prejudice() {
            assert_suggestion_result(
                "I didn't know I was prejudice against some of my own people until I went looking for web framework documentation and was met with a BLM banner.",
                lint_group(),
                "I didn't know I was prejudiced against some of my own people until I went looking for web framework documentation and was met with a BLM banner.",
            );
        }

        #[test]
        fn shes_prejudice() {
            assert_suggestion_result(
                "She's prejudice against muslims.",
                lint_group(),
                "She's prejudiced against muslims.",
            );
        }

        #[test]
        fn theyre_prejudice() {
            assert_suggestion_result(
                "The people who manage the law are all about lying and tricking people, theyre prejudice and trying to make people look stupid",
                lint_group(),
                "The people who manage the law are all about lying and tricking people, theyre prejudiced and trying to make people look stupid",
            );
        }
    }

    mod shocked {
        use crate::linting::tests::{assert_no_lints, assert_suggestion_result};

        use super::super::lint_group;

        #[test]
        fn we_were_shock() {
            assert_suggestion_result(
                "We were shock because its working fine on 1st of April just after 3 days its not working with the same settings, that is our major concern.",
                lint_group(),
                "We were shocked because its working fine on 1st of April just after 3 days its not working with the same settings, that is our major concern.",
            );
        }

        // Negative tests for "shock"

        #[test]
        fn dont_flag_shock_jocks() {
            assert_no_lints(
                "They're not commentators/anaylsts, they're shock jocks.",
                lint_group(),
            );
        }

        #[test]
        #[ignore = "Legitimate use of noun that the linter cannot yet distinguish."]
        fn dont_flag_were_shock() {
            assert_no_lints(
                "My first reactions when the topic of cryonics came up (early in our relationship) were shock, a bit of revulsion, and a lot of confusion.",
                lint_group(),
            );
        }
    }

    mod worried {
        use crate::linting::tests::{
            assert_good_and_bad_suggestions, assert_no_lints, assert_suggestion_result,
        };

        use super::super::lint_group;

        #[test]
        fn he_is_worry() {
            assert_suggestion_result(
                "I guess he is worry about \" * user * \" tag.",
                lint_group(),
                "I guess he is worried about \" * user * \" tag.",
            );
        }

        #[test]
        fn he_was() {
            assert_suggestion_result(
                "So he was worry about her. Especially, when he got no response by calling her on her phone nor ranging her doorbell.",
                lint_group(),
                "So he was worried about her. Especially, when he got no response by calling her on her phone nor ranging her doorbell.",
            );
        }

        #[test]
        fn i_am_worry() {
            assert_suggestion_result(
                "I didn't see any section dedicated to this so I am worry about:",
                lint_group(),
                "I didn't see any section dedicated to this so I am worried about:",
            );
        }

        #[test]
        fn im_worry() {
            assert_suggestion_result(
                "but I'm worry about memory leak caused by that long delay",
                lint_group(),
                "but I'm worried about memory leak caused by that long delay",
            );
        }

        #[test]
        fn im_no_apostrophe_worry() {
            assert_suggestion_result(
                "im worry now that if this inbound was dangerous and maybe it does something to my pc",
                lint_group(),
                "im worried now that if this inbound was dangerous and maybe it does something to my pc",
            );
        }

        #[test]
        fn i_was() {
            assert_suggestion_result(
                "So that's why I was worry.",
                lint_group(),
                "So that's why I was worried.",
            );
        }

        #[test]
        fn i_were() {
            assert_suggestion_result(
                "The only things that I were worry about is the data that could be lost using this deletion.",
                lint_group(),
                "The only things that I were worried about is the data that could be lost using this deletion.",
            );
        }

        #[test]
        fn they_are_worry() {
            assert_suggestion_result(
                "at the same time they are worry about the price for the upgrade each 3 years",
                lint_group(),
                "at the same time they are worried about the price for the upgrade each 3 years",
            );
        }

        #[test]
        fn theyre_worry() {
            assert_suggestion_result(
                "Because they're worry this link is spam or they scare have to pay more money.",
                lint_group(),
                "Because they're worried this link is spam or they scare have to pay more money.",
            );
        }

        #[test]
        fn we_are() {
            assert_suggestion_result(
                "We are analised this and we are worry because when our platform go to market",
                lint_group(),
                "We are analised this and we are worried because when our platform go to market",
            );
        }

        #[test]
        fn were() {
            assert_suggestion_result(
                "We're worry about all kinds of minority representation in TV.",
                lint_group(),
                "We're worried about all kinds of minority representation in TV.",
            );
        }

        #[test]
        fn you_are() {
            assert_suggestion_result(
                "You are worry because we are not annotating view interface itself, right?",
                lint_group(),
                "You are worried because we are not annotating view interface itself, right?",
            );
        }

        #[test]
        fn youre() {
            assert_suggestion_result(
                "You're worry about memory usage and wanna be sure that a Sequence-class won't hold your activity against GC — declare this class as static",
                lint_group(),
                "You're worried about memory usage and wanna be sure that a Sequence-class won't hold your activity against GC — declare this class as static",
            );
        }

        // Negative tests for "worry"

        #[test]
        fn dont_flag_it_is() {
            assert_no_lints(
                "Part of it is worry that my bosses will get angry and fire me.",
                lint_group(),
            );
        }

        #[test]
        fn dont_flag_it_was() {
            assert_no_lints(
                "Because what followed wasn't indifference, it was worry.",
                lint_group(),
            );
        }

        #[test]
        fn dont_flag_she_was_worry_free() {
            assert_no_lints("Finally, she was worry-free.", lint_group());
        }

        #[test]
        fn dont_flag_theyre_worry_free() {
            assert_no_lints("They don't pretend they're worry-free.", lint_group());
        }

        #[test]
        fn dont_flag_worry_warts() {
            assert_no_lints("They’re worry warts", lint_group());
        }

        #[test]
        fn dont_flag_were_worry_space_free() {
            assert_no_lints(
                "Thanks to jQuery, we're worry free from browser compatibility.",
                lint_group(),
            );
        }

        #[test]
        #[ignore = "different error case not handled by this linter"]
        fn different_case_not_handled() {
            assert_good_and_bad_suggestions(
                "Myself along with others are using it on an iPad successfully, so it is worry to hear that is broken for you.",
                lint_group(),
                &[
                    "Myself along with others are using it on an iPad successfully, so it is worrying to hear that is broken for you.",
                    "Myself along with others are using it on an iPad successfully, so it is a worry to hear that is broken for you.",
                ],
                &[],
            );
        }

        #[test]
        #[ignore = "This is a different error that should be caught by `ShouldContract`"]
        fn dont_flag_youre_youre_error() {
            assert_no_lints("I think you're worry is not really an issue.", lint_group());
        }
    }
}