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

use crate::linting::expr_linter::Sentence;
use crate::{
    CharStringExt, Token, TokenKind,
    expr::{Expr, SequenceExpr},
    linting::{ExprLinter, Lint, LintKind, Suggestion},
    patterns::UPOSSet,
};

pub struct WereWhere {
    expr: SequenceExpr,
}

impl Default for WereWhere {
    fn default() -> Self {
        // === where → were ===

        // "they/we" are unambiguous plural subject pronouns — "where" directly after
        // them is almost certainly a typo for "were".
        // e.g. "they where going" → "they were going"
        let unambiguous_pronoun_where = SequenceExpr::word_set(&["they", "we"])
            .t_ws()
            .t_aco("where");

        // "you where" alone is ambiguous ("I'll show you where to go"), so only flag
        // it when followed by a verb, auxiliary, or adjective — confirming a verb slot.
        // e.g. "you where going" → "you were going"
        let you_where_verb = SequenceExpr::aco("you")
            .t_ws()
            .t_aco("where")
            .t_ws()
            .then(UPOSSet::new(&[UPOS::VERB, UPOS::AUX, UPOS::ADJ]));

        // "where you ..." can be a typo for "were you ..." when it starts a question.
        let where_you_verb = SequenceExpr::aco("where")
            .t_ws()
            .t_aco("you")
            .t_ws()
            .then(UPOSSet::new(&[UPOS::VERB, UPOS::AUX, UPOS::ADJ]));

        // === were → where ===

        // A verb of cognition or motion followed directly by "were" and then a
        // pronoun, determiner, or proper noun indicates the start of a relative or
        // indirect question — where "were" should be "where".
        // e.g. "I know were they went"  → "I know where they went"
        // e.g. "I found were the book was" → "I found where the book was"
        //
        // "they were going" does NOT match: "they" (PRON) precedes "were", not VERB.
        // "I think they were going" does NOT match: "they" sits between "think" and "were".
        let verb_were_clause =
            SequenceExpr::with(|tok: &Token, _: &[char]| tok.kind.is_upos(UPOS::VERB))
                .t_ws()
                .t_aco("were")
                .t_ws()
                .then(UPOSSet::new(&[UPOS::PRON, UPOS::DET, UPOS::PROPN]));

        Self {
            expr: SequenceExpr::any_of(vec![
                Box::new(unambiguous_pronoun_where),
                Box::new(you_where_verb),
                Box::new(where_you_verb),
                Box::new(verb_were_clause),
            ]),
        }
    }
}

impl ExprLinter for WereWhere {
    type Unit = Sentence;

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

    fn match_to_lint_with_context(
        &self,
        toks: &[Token],
        src: &[char],
        context: Option<(&[Token], &[Token])>,
    ) -> Option<Lint> {
        const WHERE: &[char] = &['w', 'h', 'e', 'r', 'e'];
        const WERE: &[char] = &['w', 'e', 'r', 'e'];

        // Check if "where" appears in the match (where → were case)
        let where_tok = toks.iter().find(|tok| {
            matches!(tok.kind, TokenKind::Word(_)) && tok.span.get_content(src).eq_ch(WHERE)
        });

        // Check if "were" appears in the match (were → where case)
        let were_tok = toks.iter().find(|tok| {
            matches!(tok.kind, TokenKind::Word(_)) && tok.span.get_content(src).eq_ch(WERE)
        });

        if let Some(tok) = where_tok {
            if !crate::linting::expr_linter::at_start_of_sentence(context) {
                return None;
            }
            Some(Lint {
                span: tok.span,
                lint_kind: LintKind::Typo,
                suggestions: vec![Suggestion::replace_with_match_case_str(
                    "were",
                    tok.span.get_content(src),
                )],
                message: "It looks like this is a typo, did you mean `were`?".to_string(),
                ..Default::default()
            })
        } else {
            were_tok.map(|tok| Lint {
                span: tok.span,
                lint_kind: LintKind::Typo,
                suggestions: vec![Suggestion::replace_with_match_case_str(
                    "where",
                    tok.span.get_content(src),
                )],
                message: "It looks like this is a typo, did you mean `where`?".to_string(),
                ..Default::default()
            })
        }
    }

    fn description(&self) -> &'static str {
        "Detects mixing up `were` and `where`."
    }
}

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

    // ── where → were: unambiguous pronouns ──────────────────────────────────

    #[test]
    fn fix_they_where() {
        assert_suggestion_result(
            "They where going to the store.",
            WereWhere::default(),
            "They were going to the store.",
        );
    }

    #[test]
    fn fix_we_where() {
        assert_suggestion_result(
            "We where right about that.",
            WereWhere::default(),
            "We were right about that.",
        );
    }

    #[test]
    fn fix_they_where_happy() {
        assert_suggestion_result(
            "They where happy with the result.",
            WereWhere::default(),
            "They were happy with the result.",
        );
    }

    // ── where → were: "you where" with a following verb ─────────────────────

    #[test]
    fn fix_you_where_going() {
        assert_suggestion_result(
            "you where going in the right direction.",
            WereWhere::default(),
            "you were going in the right direction.",
        );
    }

    #[test]
    fn fix_you_where_right() {
        assert_suggestion_result(
            "you where right about that.",
            WereWhere::default(),
            "you were right about that.",
        );
    }

    // ── were → where: verb + were + pronoun/determiner ──────────────────────

    #[test]
    fn fix_know_were_they() {
        assert_suggestion_result(
            "Do you know were they went?",
            WereWhere::default(),
            "Do you know where they went?",
        );
    }

    #[test]
    fn fix_forgot_were_i() {
        assert_suggestion_result(
            "I forgot were I put my keys.",
            WereWhere::default(),
            "I forgot where I put my keys.",
        );
    }

    #[test]
    fn fix_found_were_the() {
        assert_suggestion_result(
            "I found were the book was.",
            WereWhere::default(),
            "I found where the book was.",
        );
    }

    #[test]
    fn fix_go_were_they() {
        assert_suggestion_result(
            "Go were they tell you.",
            WereWhere::default(),
            "Go where they tell you.",
        );
    }

    // ── where → were: more they/we variants ─────────────────────────────────

    #[test]
    fn fix_we_where_almost_done() {
        // No following-word check needed for "we/they" — the pair alone is enough
        assert_suggestion_result(
            "We where almost done with the task.",
            WereWhere::default(),
            "We were almost done with the task.",
        );
    }

    #[test]
    fn fix_they_where_able() {
        assert_suggestion_result(
            "They where able to fix the issue in time.",
            WereWhere::default(),
            "They were able to fix the issue in time.",
        );
    }

    #[test]
    fn fix_we_where_told() {
        assert_suggestion_result(
            "We where told about the change last week.",
            WereWhere::default(),
            "We were told about the change last week.",
        );
    }

    #[test]
    fn fix_they_where_supposed() {
        assert_suggestion_result(
            "They where supposed to be here by now.",
            WereWhere::default(),
            "They were supposed to be here by now.",
        );
    }

    // ── where → were: more "you where" variants ──────────────────────────────

    #[test]
    fn fix_you_where_supposed() {
        // "supposed" is ADJ — confirms verb slot
        assert_suggestion_result(
            "You where supposed to call me.",
            WereWhere::default(),
            "You were supposed to call me.",
        );
    }

    #[test]
    fn fix_you_where_asked() {
        // "asked" past participle used as VERB
        assert_suggestion_result(
            "you where asked to leave the room.",
            WereWhere::default(),
            "you were asked to leave the room.",
        );
    }

    #[test]
    fn fix_where_you_able() {
        assert_suggestion_result(
            "Where you able to make forward progress here?",
            WereWhere::default(),
            "Were you able to make forward progress here?",
        );
    }

    // ── were → where: more verbs and pronouns ────────────────────────────────

    #[test]
    fn fix_remember_were_i() {
        assert_suggestion_result(
            "Do you remember were I left the keys?",
            WereWhere::default(),
            "Do you remember where I left the keys?",
        );
    }

    #[test]
    fn fix_check_were_the() {
        assert_suggestion_result(
            "Check were the error occurred.",
            WereWhere::default(),
            "Check where the error occurred.",
        );
    }

    #[test]
    fn fix_asked_were_he() {
        assert_suggestion_result(
            "She asked were he lived.",
            WereWhere::default(),
            "She asked where he lived.",
        );
    }

    #[test]
    fn fix_know_were_the_bug() {
        assert_suggestion_result(
            "I know were the bug is.",
            WereWhere::default(),
            "I know where the bug is.",
        );
    }

    #[test]
    fn fix_find_were_it() {
        assert_suggestion_result(
            "Find were it crashed.",
            WereWhere::default(),
            "Find where it crashed.",
        );
    }

    // ── no false positives ───────────────────────────────────────────────────

    #[test]
    fn no_flag_where_they_are() {
        assert_no_lints("Do you know where they are going?", WereWhere::default());
    }

    #[test]
    fn no_flag_they_were_going() {
        assert_no_lints("They were going to the store.", WereWhere::default());
    }

    #[test]
    fn no_flag_we_were_right() {
        assert_no_lints("We were right about that.", WereWhere::default());
    }

    #[test]
    fn no_flag_show_you_where() {
        // "you" before "where" is legitimate — followed by "to" (PART), not a verb
        assert_no_lints("I'll show you where to go.", WereWhere::default());
    }

    #[test]
    fn no_flag_tell_you_where_the() {
        // "you where" followed by DET — not flagged (DET is not VERB/AUX/ADJ)
        assert_no_lints("I'll tell you where the exit is.", WereWhere::default());
    }

    #[test]
    fn no_flag_they_were_wrong() {
        // "they" (PRON) precedes "were", so VERB + "were" pattern does not fire
        assert_no_lints("I think they were wrong.", WereWhere::default());
    }

    #[test]
    fn no_flag_confirmed_they_were() {
        // "they" sits between "confirmed" and "were" — not adjacent, no match
        assert_no_lints("I confirmed they were correct.", WereWhere::default());
    }

    #[test]
    fn no_flag_found_they_were() {
        assert_no_lints("He found they were missing.", WereWhere::default());
    }

    #[test]
    fn no_flag_where_were_they() {
        // "Where" is an adverb or subordinating conjunction here, not VERB — the were→where pattern does not fire
        assert_no_lints("Where were they going?", WereWhere::default());
    }

    #[test]
    fn no_flag_showed_me_where() {
        // Object pronoun "me" sits between "showed" and "where" — no direct adjacency
        assert_no_lints("He showed me where the exit was.", WereWhere::default());
    }

    #[test]
    fn no_flag_where_you_go() {
        assert_no_lints("I wonder where you go from here.", WereWhere::default());
    }

    #[test]
    fn no_flag_where_you_can_customize() {
        assert_no_lints(
            "Click the menu item where you can customize the settings.",
            WereWhere::default(),
        );
    }

    #[test]
    fn no_flag_where_you_allocate() {
        assert_no_lints(
            "Use the panel where you allocate resources for the task.",
            WereWhere::default(),
        );
    }

    // ── known limitations (documented but not yet handled) ───────────────────

    #[test]
    #[ignore = "limitation: 'you where' followed by DET is not flagged; would need DET in the following-word set"]
    fn fix_you_where_the_only_one() {
        assert_suggestion_result(
            "you where the only one there.",
            WereWhere::default(),
            "you were the only one there.",
        );
    }

    #[test]
    #[ignore = "limitation: sentence-initial 'Where' as typo for 'Were' is not handled"]
    fn fix_where_they_going_sentence_start() {
        assert_suggestion_result(
            "Where they going to the party?",
            WereWhere::default(),
            "Were they going to the party?",
        );
    }

    #[test]
    #[ignore = "limitation: indirect object between verb and 'were' is not detected"]
    fn fix_showed_me_were() {
        assert_suggestion_result(
            "He showed me were the exit was.",
            WereWhere::default(),
            "He showed me where the exit was.",
        );
    }
}