harper-core 2.8.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
use crate::expr::{Expr, ExprMap, FixedPhrase};
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};

use super::{ExprLinter, LintGroup};
use super::{Lint, LintKind, Suggestion};
use crate::Document;
use crate::linting::expr_linter::Chunk;
use crate::parsers::PlainEnglish;
use crate::{Token, TokenStringExt};

/// A linter that corrects the capitalization of multi-word proper nouns.
/// They are corrected to a "canonical capitalization" provided at construction.
///
/// If you would like to add a proper noun to Harper, see `proper_noun_rules.json`.
pub struct ProperNounCapitalizationLinter {
    pattern_map: ExprMap<Document>,
    description: String,
}

impl ProperNounCapitalizationLinter {
    /// Create a linter that corrects the capitalization of phrases provided.
    pub fn new_strs(
        canonical_versions: impl IntoIterator<Item = impl AsRef<str>>,
        description: impl ToString,
    ) -> Self {
        let mut expr_map = ExprMap::default();

        for can_vers in canonical_versions {
            let doc = Document::new_basic_tokenize(can_vers.as_ref(), &PlainEnglish);

            let expr = FixedPhrase::from_document(&doc);

            expr_map.insert(expr, doc);
        }

        Self {
            pattern_map: expr_map,
            description: description.to_string(),
        }
    }
}

impl ExprLinter for ProperNounCapitalizationLinter {
    type Unit = Chunk;

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

    fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
        let canonical_case = self.pattern_map.lookup(0, matched_tokens, source).unwrap();

        let mut broken = false;

        for (err_token, correct_token) in matched_tokens.iter().zip(canonical_case.fat_tokens()) {
            let err_chars = err_token.get_ch(source);
            if err_chars != correct_token.content {
                broken = true;
                break;
            }
        }

        if !broken {
            return None;
        }

        Some(Lint {
            span: matched_tokens.span()?,
            lint_kind: LintKind::Capitalization,
            suggestions: vec![Suggestion::ReplaceWith(
                canonical_case.get_source().to_vec(),
            )],
            message: self.description.to_owned(),
            priority: 31,
        })
    }

    fn description(&self) -> &str {
        self.description.as_str()
    }
}

#[derive(Serialize, Deserialize)]
struct RuleEntry {
    canonical: Vec<String>,
    description: String,
}

/// For the time being, this panics on invalid JSON.
/// Do not use with user provided JSON.
fn lint_group_from_json(json: &str) -> LintGroup {
    let mut group = LintGroup::empty();

    let rules: HashMap<String, RuleEntry> = serde_json::from_str(json).unwrap();

    for (key, rule) in rules.into_iter() {
        group.add_chunk_expr_linter(
            key,
            Box::new(ProperNounCapitalizationLinter::new_strs(
                rule.canonical,
                rule.description,
            )),
        );
    }

    group.set_all_rules_to(Some(true));

    group
}

pub fn lint_group() -> LintGroup {
    lint_group_from_json(include_str!("../../proper_noun_rules.json"))
}

#[cfg(test)]
mod tests {
    use super::lint_group;
    use crate::linting::LintGroup;
    use crate::linting::create_test_pool;
    use crate::linting::tests::{assert_lint_count, assert_suggestion_result};

    create_test_pool!(LintGroup, LintGroup, lint_group());

    #[test]
    fn americas_lowercase() {
        assert_suggestion_result("south america", test_linter(), "South America");
        assert_suggestion_result("north america", test_linter(), "North America");
    }

    #[test]
    fn americas_uppercase() {
        assert_suggestion_result("SOUTH AMERICA", test_linter(), "South America");
        assert_suggestion_result("NORTH AMERICA", test_linter(), "North America");
    }

    #[test]
    fn americas_allow_correct() {
        assert_lint_count("South America", test_linter(), 0);
        assert_lint_count("North America", test_linter(), 0);
    }

    #[test]
    fn issue_798() {
        assert_suggestion_result(
            "The United states is a big country.",
            test_linter(),
            "The United States is a big country.",
        );
    }

    #[test]
    fn united_nations_uppercase() {
        assert_suggestion_result("UNITED NATIONS", test_linter(), "United Nations");
    }

    #[test]
    fn united_arab_emirates_lowercase() {
        assert_suggestion_result(
            "UNITED ARAB EMIRATES",
            test_linter(),
            "United Arab Emirates",
        );
    }

    #[test]
    fn united_nations_allow_correct() {
        assert_lint_count("United Nations", test_linter(), 0);
    }

    #[test]
    fn meta_allow_correct() {
        assert_lint_count("Meta Quest", test_linter(), 0);
    }

    #[test]
    fn microsoft_lowercase() {
        assert_suggestion_result(
            "microsoft visual studio",
            test_linter(),
            "Microsoft Visual Studio",
        );
    }

    #[test]
    fn microsoft_first_word_is_correct() {
        assert_suggestion_result(
            "Microsoft visual studio",
            test_linter(),
            "Microsoft Visual Studio",
        );
    }

    #[test]
    fn apple_ipod_lowercase() {
        assert_suggestion_result("apple ipod", lint_group(), "Apple iPod");
    }

    #[test]
    fn apple_ipod_allow_correct() {
        assert_lint_count("Apple iPod", lint_group(), 0);
    }

    #[test]
    fn apple_macbook_family_lowercase() {
        assert_suggestion_result("macbook pro", lint_group(), "MacBook Pro");
        assert_suggestion_result("macbook air", lint_group(), "MacBook Air");
    }

    #[test]
    fn apple_macbook_family_allow_correct() {
        assert_lint_count("MacBook Pro", lint_group(), 0);
        assert_lint_count("MacBook Air", lint_group(), 0);
    }

    #[test]
    fn apple_mac_family_lowercase() {
        assert_suggestion_result("mac pro", lint_group(), "Mac Pro");
        assert_suggestion_result("mac mini", lint_group(), "Mac Mini");
    }

    #[test]
    fn apple_mac_family_allow_correct() {
        assert_lint_count("Mac Pro", lint_group(), 0);
        assert_lint_count("Mac Mini", lint_group(), 0);
    }

    #[test]
    fn apple_airpods_family_lowercase() {
        assert_suggestion_result("airpods pro", lint_group(), "AirPods Pro");
        assert_suggestion_result("airpods max", lint_group(), "AirPods Max");
    }

    #[test]
    fn apple_airpods_family_allow_correct() {
        assert_lint_count("AirPods Pro", lint_group(), 0);
        assert_lint_count("AirPods Max", lint_group(), 0);
    }

    #[test]
    fn test_atlantic_ocean_lowercase() {
        assert_suggestion_result("atlantic ocean", test_linter(), "Atlantic Ocean");
    }

    #[test]
    fn test_pacific_ocean_lowercase() {
        assert_suggestion_result("pacific ocean", test_linter(), "Pacific Ocean");
    }

    #[test]
    fn test_indian_ocean_lowercase() {
        assert_suggestion_result("indian ocean", test_linter(), "Indian Ocean");
    }

    #[test]
    fn test_southern_ocean_lowercase() {
        assert_suggestion_result("southern ocean", test_linter(), "Southern Ocean");
    }

    #[test]
    fn test_arctic_ocean_lowercase() {
        assert_suggestion_result("arctic ocean", test_linter(), "Arctic Ocean");
    }

    #[test]
    fn test_mediterranean_sea_lowercase() {
        assert_suggestion_result("mediterranean sea", test_linter(), "Mediterranean Sea");
    }

    #[test]
    fn test_caribbean_sea_lowercase() {
        assert_suggestion_result("caribbean sea", test_linter(), "Caribbean Sea");
    }

    #[test]
    fn test_south_china_sea_lowercase() {
        assert_suggestion_result("south china sea", test_linter(), "South China Sea");
    }

    #[test]
    fn test_atlantic_ocean_correct() {
        assert_lint_count("Atlantic Ocean", test_linter(), 0);
    }

    #[test]
    fn test_pacific_ocean_correct() {
        assert_lint_count("Pacific Ocean", test_linter(), 0);
    }

    #[test]
    fn test_indian_ocean_correct() {
        assert_lint_count("Indian Ocean", test_linter(), 0);
    }

    #[test]
    fn test_mediterranean_sea_correct() {
        assert_lint_count("Mediterranean Sea", test_linter(), 0);
    }

    #[test]
    fn test_south_china_sea_correct() {
        assert_lint_count("South China Sea", test_linter(), 0);
    }

    #[test]
    fn day_one_in_sentence() {
        assert_suggestion_result(
            "I love day one. It is the best journaling app.",
            test_linter(),
            "I love Day One. It is the best journaling app.",
        );
    }

    #[test]
    fn gilded_age_in_sentence() {
        assert_suggestion_result(
            "Mani-Chess Destiny is a JavaScript based computer game built off of chess, but in the style of the gilded age.",
            test_linter(),
            "Mani-Chess Destiny is a JavaScript based computer game built off of chess, but in the style of the Gilded Age.",
        );
    }

    #[test]
    fn chrome_extension_lowercase() {
        assert_suggestion_result("chrome extension", test_linter(), "Chrome Extension");
    }

    #[test]
    fn chrome_extension_uppercase() {
        assert_suggestion_result("CHROME EXTENSION", test_linter(), "Chrome Extension");
    }

    #[test]
    fn chrome_extension_mixed_case() {
        assert_suggestion_result("cHrOmE eXtEnSiOn", test_linter(), "Chrome Extension");
    }

    #[test]
    fn chrome_extension_second_word_lowercase() {
        assert_suggestion_result("Chrome extension", test_linter(), "Chrome Extension");
    }

    #[test]
    fn chrome_extension_first_word_lowercase() {
        assert_suggestion_result("chrome Extension", test_linter(), "Chrome Extension");
    }

    #[test]
    fn chrome_extension_in_sentence() {
        assert_suggestion_result(
            "Install the chrome extension from the store.",
            test_linter(),
            "Install the Chrome Extension from the store.",
        );
    }

    #[test]
    fn chrome_extension_with_leading_article() {
        assert_suggestion_result(
            "The chrome extension is ready.",
            test_linter(),
            "The Chrome Extension is ready.",
        );
    }

    #[test]
    fn chrome_extension_with_trailing_period() {
        assert_suggestion_result(
            "We shipped the chrome extension.",
            test_linter(),
            "We shipped the Chrome Extension.",
        );
    }

    #[test]
    fn chrome_extension_with_trailing_comma() {
        assert_suggestion_result(
            "The chrome extension, not the app, needs review.",
            test_linter(),
            "The Chrome Extension, not the app, needs review.",
        );
    }

    #[test]
    fn chrome_extension_with_trailing_colon() {
        assert_suggestion_result(
            "Preferred install target: chrome extension",
            test_linter(),
            "Preferred install target: Chrome Extension",
        );
    }

    #[test]
    fn chrome_extension_inside_quotes() {
        assert_suggestion_result(
            "They called it the \"chrome extension\" build.",
            test_linter(),
            "They called it the \"Chrome Extension\" build.",
        );
    }

    #[test]
    fn chrome_extension_across_sentence_boundary_not_present() {
        assert_lint_count("Chrome. Extension", test_linter(), 0);
    }

    #[test]
    fn chrome_extension_allows_correct_case() {
        assert_lint_count("Chrome Extension", test_linter(), 0);
    }

    #[test]
    fn chrome_extension_allows_correct_case_in_sentence() {
        assert_lint_count("The Chrome Extension is ready.", test_linter(), 0);
    }

    #[test]
    fn browser_extension_not_flagged() {
        assert_lint_count("browser extension", test_linter(), 0);
    }
}