harper-core 3.2.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
use harper_brill::UPOS;

use crate::expr::{Expr, FirstMatchOf, FixedPhrase, SequenceExpr};
use crate::patterns::UPOSSet;
use crate::{Document, Token, TokenStringExt};

use super::{Lint, LintKind, Linter, Suggestion};

const ALSO_PREPOSITIONS: &[&str] = &["besides", "notwithstanding"];

pub struct DiscourseMarkers {
    expr: SequenceExpr,
}

impl DiscourseMarkers {
    pub fn new() -> Self {
        let phrases = &[
            "accordingly",
            "actually",
            "additionally",
            "also",
            "alternatively",
            "besides",
            "by the way",
            "consequently",
            "conversely",
            "even so",
            "for example",
            "frankly",
            "furthermore",
            "hence",
            "honestly",
            "however",
            "in addition",
            "in other words",
            "incidentally",
            "indeed",
            "instead",
            "meanwhile",
            "moreover",
            "nevertheless",
            "notwithstanding",
            "nowadays",
            "now a days",
            "now-a-days",
            "on the other hand",
            "otherwise",
            "subsequently",
            "that said",
            "therefore",
            "thus",
        ];

        let phrases_expr = FirstMatchOf::new(
            phrases
                .iter()
                .map(|text: &&str| Box::new(FixedPhrase::from_phrase(text)) as Box<dyn Expr>),
        );

        Self {
            // Skip markers followed by a modifier or a verb
            expr: SequenceExpr::with(phrases_expr)
                .t_ws()
                .then_unless(UPOSSet::new(&[
                    UPOS::ADJ,
                    UPOS::ADV,
                    UPOS::ADP,
                    UPOS::CCONJ,
                    UPOS::VERB,
                ])),
        }
    }

    fn lint_sentence(&self, sent: &[Token], source: &[char]) -> Option<Lint> {
        let first_word_idx = sent.iter_word_indices().next()?;
        let matched_phrase = self.expr.run(first_word_idx, sent, source)?;

        let marker_end = matched_phrase.end - 2;
        let marker = &sent[matched_phrase.start..marker_end];

        if self.is_prepositional(marker, &sent[marker_end..], source) {
            return None;
        }

        Some(Lint {
            span: marker.span()?,
            lint_kind: LintKind::Punctuation,
            suggestions: vec![Suggestion::InsertAfter(vec![','])],
            message:
                "Discourse markers at the beginning of a sentence should be followed by a comma."
                    .into(),
            priority: 31,
        })
    }

    fn is_prepositional(&self, marker: &[Token], rest: &[Token], source: &[char]) -> bool {
        let (Some(span), Some(next)) = (marker.span(), rest.first_non_whitespace()) else {
            return false;
        };

        ALSO_PREPOSITIONS.contains(&span.get_content_string(source).to_lowercase().as_str())
            && (next.kind.is_upos(UPOS::DET)
                || !rest
                    .iter()
                    .take_while(|token| !token.kind.is_comma())
                    .any(|token| token.kind.is_upos(UPOS::VERB)))
    }
}

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

impl Linter for DiscourseMarkers {
    fn lint(&mut self, document: &Document) -> Vec<Lint> {
        document
            .iter_sentences()
            .flat_map(|sent| self.lint_sentence(sent, document.get_source()))
            .collect()
    }

    fn description(&self) -> &str {
        "Flags sentences that begin with a discourse marker but omit the required following comma."
    }
}

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

    use super::DiscourseMarkers;

    #[test]
    fn corrects_frankly() {
        assert_suggestion_result(
            "Frankly I think he is wrong.",
            DiscourseMarkers::default(),
            "Frankly, I think he is wrong.",
        );
    }

    #[test]
    fn corrects_however() {
        assert_suggestion_result(
            "However I disagree with your conclusion.",
            DiscourseMarkers::default(),
            "However, I disagree with your conclusion.",
        );
    }

    #[test]
    fn corrects_therefore() {
        assert_suggestion_result(
            "Therefore we must act now.",
            DiscourseMarkers::default(),
            "Therefore, we must act now.",
        );
    }

    #[test]
    fn corrects_meanwhile() {
        assert_suggestion_result(
            "Meanwhile preparations continued in the background.",
            DiscourseMarkers::default(),
            "Meanwhile, preparations continued in the background.",
        );
    }

    #[test]
    fn corrects_furthermore() {
        assert_suggestion_result(
            "Furthermore this approach reduces complexity.",
            DiscourseMarkers::default(),
            "Furthermore, this approach reduces complexity.",
        );
    }

    #[test]
    fn corrects_nevertheless() {
        assert_suggestion_result(
            "Nevertheless we persevered despite the odds.",
            DiscourseMarkers::default(),
            "Nevertheless, we persevered despite the odds.",
        );
    }

    #[test]
    fn corrects_consequently() {
        assert_suggestion_result(
            "Consequently the system halted unexpectedly.",
            DiscourseMarkers::default(),
            "Consequently, the system halted unexpectedly.",
        );
    }

    #[test]
    fn corrects_thus() {
        assert_suggestion_result(
            "Thus we arrive at the final verdict.",
            DiscourseMarkers::default(),
            "Thus, we arrive at the final verdict.",
        );
    }

    #[test]
    fn allows_thus_far() {
        assert_no_lints(
            "Thus far there have been no problems.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_instead() {
        assert_suggestion_result(
            "Instead he chose a different path.",
            DiscourseMarkers::default(),
            "Instead, he chose a different path.",
        );
    }

    #[test]
    fn corrects_moreover() {
        assert_suggestion_result(
            "Moreover this solution is more efficient.",
            DiscourseMarkers::default(),
            "Moreover, this solution is more efficient.",
        );
    }

    #[test]
    fn corrects_alternatively() {
        assert_suggestion_result(
            "Alternatively we could defer the decision.",
            DiscourseMarkers::default(),
            "Alternatively, we could defer the decision.",
        );
    }

    #[test]
    fn no_suggestion_if_comma_present() {
        assert_no_lints(
            "However, I disagree with your point.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn no_lint_for_mid_sentence_marker() {
        assert_no_lints(
            "I said however I would consider it.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn preserves_whitespace() {
        assert_suggestion_result(
            "However   I disagree.",
            DiscourseMarkers::default(),
            "However,   I disagree.",
        );
    }

    #[test]
    fn corrects_semicolon_case() {
        assert_suggestion_result(
            "However I disagree.",
            DiscourseMarkers::default(),
            "However, I disagree.",
        );
    }

    #[test]
    fn corrects_multiple_sentences() {
        assert_suggestion_result(
            "However I disagree. Therefore I propose an alternative.",
            DiscourseMarkers::default(),
            "However, I disagree. Therefore, I propose an alternative.",
        );
    }

    #[test]
    fn allows_single_word_sentence() {
        assert_no_lints("Thus", DiscourseMarkers::default());
    }

    #[test]
    fn corrects_for_example() {
        assert_suggestion_result(
            "For example I recommend updating the configuration.",
            DiscourseMarkers::default(),
            "For example, I recommend updating the configuration.",
        );
    }

    #[test]
    fn no_suggestion_if_comma_after_for_example() {
        assert_no_lints(
            "For example, I recommend updating the configuration.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn preserves_whitespace_for_example() {
        assert_suggestion_result(
            "For example   the outcome was unexpected.",
            DiscourseMarkers::default(),
            "For example,   the outcome was unexpected.",
        );
    }

    #[test]
    fn corrects_on_the_other_hand() {
        assert_suggestion_result(
            "On the other hand we could delay the deployment.",
            DiscourseMarkers::default(),
            "On the other hand, we could delay the deployment.",
        );
    }

    #[test]
    fn no_lint_for_mid_sentence_on_the_other_hand() {
        assert_no_lints(
            "We might postpone, on the other hand this introduces risk.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn check_2966_is_avoided() {
        assert_no_lints(
            "Honestly and graciously convince someone of something.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_nowadays() {
        assert_suggestion_result(
            "Nowadays E-Commerce is one of the hot topics.",
            DiscourseMarkers::default(),
            "Nowadays, E-Commerce is one of the hot topics.",
        );
    }

    #[test]
    fn no_suggestion_if_nowadays_not_at_start() {
        assert_no_lints(
            "You are the reason the tech state is awful nowadays",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_now_a_days() {
        assert_suggestion_result(
            "Now a days Automation plays a crucial role in mankind.",
            DiscourseMarkers::default(),
            "Now a days, Automation plays a crucial role in mankind.",
        );
    }

    #[test]
    fn no_suggestion_if_comma_after_nowadays() {
        assert_no_lints(
            "Nowadays, data has become the most valuable part of any individual or organization.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn no_suggestion_if_comma_after_now_a_days() {
        assert_no_lints(
            "Now a days, movie recommendation systems are well developed and are user focused.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_also() {
        assert_suggestion_result(
            "Also I think we should go.",
            DiscourseMarkers::default(),
            "Also, I think we should go.",
        );
    }

    #[test]
    fn no_suggestion_if_comma_after_also() {
        assert_no_lints("Also, I think we should go.", DiscourseMarkers::default());
    }

    #[test]
    fn no_lint_for_mid_sentence_also() {
        assert_no_lints("She also said hi.", DiscourseMarkers::default());
    }

    #[test]
    fn allows_also_known_as() {
        assert_no_lints("Also known as the big one.", DiscourseMarkers::default());
    }

    #[test]
    fn allows_also_called() {
        assert_no_lints("Also called the big one.", DiscourseMarkers::default());
    }

    #[test]
    fn allows_also_see() {
        assert_no_lints("Also see chapter two.", DiscourseMarkers::default());
    }

    #[test]
    fn allows_also_available() {
        assert_no_lints("Also available in blue.", DiscourseMarkers::default());
    }

    #[test]
    fn corrects_actually() {
        assert_suggestion_result(
            "Actually I don't know.",
            DiscourseMarkers::default(),
            "Actually, I don't know.",
        );
    }

    #[test]
    fn no_suggestion_if_comma_after_actually() {
        assert_no_lints("Actually, I don't know.", DiscourseMarkers::default());
    }

    #[test]
    fn no_lint_for_mid_sentence_actually() {
        assert_no_lints("She is actually right.", DiscourseMarkers::default());
    }

    #[test]
    fn allows_actually_existing() {
        assert_no_lints(
            "Actually existing socialism was a term.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_also_and_actually_in_one_document() {
        assert_suggestion_result(
            "Actually I don't know. Also I forgot.",
            DiscourseMarkers::default(),
            "Actually, I don't know. Also, I forgot.",
        );
    }

    #[test]
    fn corrects_in_addition() {
        assert_suggestion_result(
            "In addition we need to test this.",
            DiscourseMarkers::default(),
            "In addition, we need to test this.",
        );
    }

    #[test]
    fn allows_in_addition_to() {
        assert_no_lints(
            "In addition to that we need to test this.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_even_so() {
        assert_suggestion_result(
            "Even so the plan failed.",
            DiscourseMarkers::default(),
            "Even so, the plan failed.",
        );
    }

    #[test]
    fn allows_even_so_much_as() {
        assert_no_lints(
            "Even so much as a whisper would wake him.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_that_said() {
        assert_suggestion_result(
            "That said we should wait.",
            DiscourseMarkers::default(),
            "That said, we should wait.",
        );
    }

    #[test]
    fn corrects_in_other_words() {
        assert_suggestion_result(
            "In other words we should go.",
            DiscourseMarkers::default(),
            "In other words, we should go.",
        );
    }

    #[test]
    fn allows_in_other_words_and_phrases() {
        assert_no_lints(
            "In other words and phrases, keep it short.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_by_the_way() {
        assert_suggestion_result(
            "By the way I saw him yesterday.",
            DiscourseMarkers::default(),
            "By the way, I saw him yesterday.",
        );
    }

    #[test]
    fn allows_by_way_of() {
        assert_no_lints(
            "By way of introduction he waved.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_besides() {
        assert_suggestion_result(
            "Besides we have no time.",
            DiscourseMarkers::default(),
            "Besides, we have no time.",
        );
    }

    #[test]
    fn corrects_besides_before_list() {
        assert_suggestion_result(
            "Besides we need milk, eggs, and bread.",
            DiscourseMarkers::default(),
            "Besides, we need milk, eggs, and bread.",
        );
    }

    #[test]
    fn allows_besides_as_preposition() {
        assert_no_lints(
            "Besides me, nobody knew the answer.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_notwithstanding() {
        assert_suggestion_result(
            "Notwithstanding they still went out.",
            DiscourseMarkers::default(),
            "Notwithstanding, they still went out.",
        );
    }

    #[test]
    fn allows_notwithstanding_as_preposition() {
        assert_no_lints(
            "Notwithstanding the rain, we went out.",
            DiscourseMarkers::default(),
        );
    }

    #[test]
    fn corrects_other_markers_before_determiner() {
        assert_suggestion_result(
            "However the rain held off.",
            DiscourseMarkers::default(),
            "However, the rain held off.",
        );
    }
}