poexam 0.0.10

Blazingly fast PO linter.
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
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later

//! Implementation of the `newlines` rule: check missing/extra newlines.

use crate::checker::Checker;
use crate::diagnostic::{Diagnostic, Severity};
use crate::po::entry::Entry;
use crate::po::message::Message;
use crate::rules::rule::RuleChecker;

pub struct NewlinesRule;

impl RuleChecker for NewlinesRule {
    fn name(&self) -> &'static str {
        "newlines"
    }

    fn description(&self) -> &'static str {
        "Check for missing or extra newlines in translation."
    }

    fn is_default(&self) -> bool {
        true
    }

    fn is_check(&self) -> bool {
        true
    }

    /// Check for missing or extra newlines in the translation: carriage return (`\r`) or line feed (`\n`).
    ///
    /// Wrong entry:
    /// ```text
    /// msgid "this is a test\n"
    /// "second line"
    /// msgstr "ceci est un test"
    /// "seconde ligne"
    /// ```
    ///
    /// Correct entry:
    /// ```text
    /// msgid "this is a test\n"
    /// "second line"
    /// msgstr "ceci est un test\n"
    /// "seconde ligne"
    /// ```
    ///
    /// Diagnostics reported:
    /// - [`error`](Severity::Error): `missing carriage returns '\r' (# / #)`
    /// - [`error`](Severity::Error): `extra carriage returns '\r' (# / #)`
    /// - [`error`](Severity::Error): `missing line feeds '\n' (# / #)`
    /// - [`error`](Severity::Error): `extra line feeds '\n' (# / #)`
    /// - [`error`](Severity::Error): `missing carriage return '\r' at the beginning`
    /// - [`error`](Severity::Error): `extra carriage return '\r' at the beginning`
    /// - [`error`](Severity::Error): `missing line feed '\n' at the beginning`
    /// - [`error`](Severity::Error): `extra line feed '\n' at the beginning`
    /// - [`error`](Severity::Error): `missing carriage return '\r' at the end`
    /// - [`error`](Severity::Error): `extra carriage return '\r' at the end`
    /// - [`error`](Severity::Error): `missing line feed '\n' at the end`
    /// - [`error`](Severity::Error): `extra line feed '\n' at the end`
    fn check_msg(
        &self,
        checker: &Checker,
        _entry: &Entry,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        diags.extend(self.check_cr_lf_count(checker, msgid, msgstr));
        diags.extend(self.check_cr_lf_beginning(checker, msgid, msgstr));
        diags.extend(self.check_cr_lf_end(checker, msgid, msgstr));
        diags
    }
}

impl NewlinesRule {
    /// Check the number of CR ('\r') and LF ('\n') characters.
    fn check_cr_lf_count(
        &self,
        checker: &Checker,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        // Check the number of CR ('\r').
        let id_count_cr = msgid.value.matches('\r').count();
        let str_count_cr = msgstr.value.matches('\r').count();
        match id_count_cr.cmp(&str_count_cr) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        format!("missing carriage returns '\\r' ({id_count_cr} / {str_count_cr})"),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        format!("extra carriage returns '\\r' ({id_count_cr} / {str_count_cr})"),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        // Check the number of LF ('\n').
        let id_count_lf = msgid.value.matches('\n').count();
        let str_count_lf = msgstr.value.matches('\n').count();
        match id_count_lf.cmp(&str_count_lf) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        format!("missing line feeds '\\n' ({id_count_lf} / {str_count_lf})"),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        format!("extra line feeds '\\n' ({id_count_lf} / {str_count_lf})"),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        diags
    }

    /// Check for CR ('\r') and LF ('\n') at the beginning of the strings.
    fn check_cr_lf_beginning(
        &self,
        checker: &Checker,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        // Check CR ('\r') at beginning.
        let id_starts_with_cr = msgid.value.starts_with('\r');
        let str_starts_with_cr = msgstr.value.starts_with('\r');
        match id_starts_with_cr.cmp(&str_starts_with_cr) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "missing carriage return '\\r' at the beginning".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "extra carriage return '\\r' at the beginning".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        // Check LF ('\n') at beginning.
        let id_starts_with_lf = msgid.value.starts_with('\n');
        let str_starts_with_lf = msgstr.value.starts_with('\n');
        match id_starts_with_lf.cmp(&str_starts_with_lf) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "missing line feed '\\n' at the beginning".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "extra line feed '\\n' at the beginning".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        diags
    }

    /// Check for CR ('\r') and LF ('\n') at the end of the strings.
    fn check_cr_lf_end(
        &self,
        checker: &Checker,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        // Check CR ('\r') at end.
        let id_ends_with_cr = msgid.value.ends_with('\r');
        let str_ends_with_cr = msgstr.value.ends_with('\r');
        match id_ends_with_cr.cmp(&str_ends_with_cr) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "missing carriage return '\\r' at the end".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "extra carriage return '\\r' at the end".to_string(),
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        // Check LF ('\n') at end.
        let id_ends_with_lf = msgid.value.ends_with('\n');
        let str_ends_with_lf = msgstr.value.ends_with('\n');
        match id_ends_with_lf.cmp(&str_ends_with_lf) {
            std::cmp::Ordering::Greater => {
                diags.extend(
                    self.new_diag(
                        checker,
                        Severity::Error,
                        "missing line feed '\\n' at the end",
                    )
                    .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Less => {
                diags.extend(
                    self.new_diag(checker, Severity::Error, "extra line feed '\\n' at the end")
                        .map(|d| d.with_msgs(msgid, msgstr)),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        diags
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{diagnostic::Diagnostic, rules::rule::Rules};

    fn check_newlines(content: &str) -> Vec<Diagnostic> {
        let mut checker = Checker::new(content.as_bytes());
        let rules = Rules::new(vec![Box::new(NewlinesRule {})]);
        checker.do_all_checks(&rules);
        checker.diagnostics
    }

    #[test]
    fn test_no_newlines() {
        let diags = check_newlines(
            r#"
msgid "tested"
msgstr "testé"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_newlines_ok() {
        let diags = check_newlines(
            r#"
msgid "\ntested\nline 2\n"
msgstr "\ntesté\nligne 2\n"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_newlines_count_error() {
        let diags = check_newlines(
            r#"
msgid "tested\rline 2"
msgstr "testé ligne 2"

msgid "tested line 2"
msgstr "testé\rligne 2"

msgid "tested\nline 2"
msgstr "testé ligne 2"

msgid "testedline 2"
msgstr "testé\nligne 2"
"#,
        );
        assert_eq!(diags.len(), 4);
        let diag = &diags[0];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "missing carriage returns '\\r' (1 / 0)");
        let diag = &diags[1];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra carriage returns '\\r' (0 / 1)");
        let diag = &diags[2];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "missing line feeds '\\n' (1 / 0)");
        let diag = &diags[3];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra line feeds '\\n' (0 / 1)");
    }

    #[test]
    fn test_newlines_beginning_error() {
        let diags = check_newlines(
            r#"
msgid "\rtested"
msgstr "testé\rligne 2"

msgid "\ntested"
msgstr "testé\nligne 2"

msgid "tested\rline 2"
msgstr "\rtesté"

msgid "tested\nline 2"
msgstr "\ntesté"
"#,
        );
        assert_eq!(diags.len(), 4);
        let diag = &diags[0];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(
            diag.message,
            "missing carriage return '\\r' at the beginning"
        );
        let diag = &diags[1];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "missing line feed '\\n' at the beginning");
        let diag = &diags[2];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra carriage return '\\r' at the beginning");
        let diag = &diags[3];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra line feed '\\n' at the beginning");
    }

    #[test]
    fn test_newlines_error_noqa() {
        let diags = check_newlines(
            r#"
#, noqa:newlines
msgid "\rtested"
msgstr "testé\rligne 2"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_newlines_end_error() {
        let diags = check_newlines(
            r#"
msgid "tested\r"
msgstr "testé\rligne 2"

msgid "tested\n"
msgstr "testé\nligne 2"

msgid "tested\rline 2"
msgstr "testé\r"

msgid "tested\nline 2"
msgstr "testé\n"
"#,
        );
        assert_eq!(diags.len(), 4);
        let diag = &diags[0];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "missing carriage return '\\r' at the end");
        let diag = &diags[1];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "missing line feed '\\n' at the end");
        let diag = &diags[2];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra carriage return '\\r' at the end");
        let diag = &diags[3];
        assert_eq!(diag.severity, Severity::Error);
        assert_eq!(diag.message, "extra line feed '\\n' at the end");
    }
}