poexam 0.0.11

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
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
// 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::fix::{Edit, Fix, FixTarget};
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` (auto-fixable)
    /// - [`error`](Severity::Error): `extra carriage return '\r' at the beginning` (auto-fixable)
    /// - [`error`](Severity::Error): `missing line feed '\n' at the beginning` (auto-fixable)
    /// - [`error`](Severity::Error): `extra line feed '\n' at the beginning` (auto-fixable)
    /// - [`error`](Severity::Error): `missing carriage return '\r' at the end` (auto-fixable)
    /// - [`error`](Severity::Error): `extra carriage return '\r' at the end` (auto-fixable)
    /// - [`error`](Severity::Error): `missing line feed '\n' at the end` (auto-fixable)
    /// - [`error`](Severity::Error): `extra line feed '\n' at the end` (auto-fixable)
    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.
    ///
    /// When the leading CR/LF run of `msgstr` differs from `msgid`'s, the rule
    /// attaches the same byte-range fix to every diagnostic it emits for this
    /// boundary. `apply_msgstr_fixes` dedups identical edits, so attaching the
    /// fix to both the CR and LF diagnostics is safe.
    fn check_cr_lf_beginning(
        &self,
        checker: &Checker,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        let id_run = get_newline_start(&msgid.value);
        let str_run = get_newline_start(&msgstr.value);
        let fix = (id_run != str_run).then(|| Fix {
            target: FixTarget::Msgstr {
                file_byte_range: msgstr.byte_range.clone(),
            },
            edits: vec![Edit {
                range: 0..str_run.len(),
                replacement: id_run.to_string(),
            }],
        });
        // 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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        diags
    }

    /// Check for CR ('\r') and LF ('\n') at the end of the strings.
    ///
    /// See [`check_cr_lf_beginning`](Self::check_cr_lf_beginning) for the
    /// fix-attachment strategy; the same applies here mirrored to the end of
    /// the string.
    fn check_cr_lf_end(
        &self,
        checker: &Checker,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let mut diags = vec![];
        let id_run = get_newline_end(&msgid.value);
        let str_run = get_newline_end(&msgstr.value);
        let str_run_start = msgstr.value.len() - str_run.len();
        let fix = (id_run != str_run).then(|| Fix {
            target: FixTarget::Msgstr {
                file_byte_range: msgstr.byte_range.clone(),
            },
            edits: vec![Edit {
                range: str_run_start..msgstr.value.len(),
                replacement: id_run.to_string(),
            }],
        });
        // 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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            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).with_optional_fix(fix.clone())),
                );
            }
            std::cmp::Ordering::Equal => {}
        }
        diags
    }
}

/// Get the leading run of CR/LF characters in `value`.
fn get_newline_start(value: &str) -> &str {
    let pos = value
        .chars()
        .take_while(|c| matches!(c, '\r' | '\n'))
        .map(char::len_utf8)
        .sum::<usize>();
    &value[..pos]
}

/// Get the trailing run of CR/LF characters in `value`.
fn get_newline_end(value: &str) -> &str {
    let pos = value
        .chars()
        .rev()
        .take_while(|c| matches!(c, '\r' | '\n'))
        .map(char::len_utf8)
        .sum::<usize>();
    &value[value.len() - pos..]
}

#[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");
    }

    #[test]
    fn test_get_newline_start_and_end() {
        assert_eq!(get_newline_start(""), "");
        assert_eq!(get_newline_start("hello"), "");
        assert_eq!(get_newline_start("\nhello"), "\n");
        assert_eq!(get_newline_start("\r\nhello"), "\r\n");
        assert_eq!(get_newline_start("\n\rhello"), "\n\r");
        assert_eq!(get_newline_end(""), "");
        assert_eq!(get_newline_end("hello"), "");
        assert_eq!(get_newline_end("hello\n"), "\n");
        assert_eq!(get_newline_end("hello\r\n"), "\r\n");
    }

    #[test]
    fn test_newlines_count_diagnostic_has_no_fix() {
        // Count mismatches cannot be auto-fixed (we don't know where the
        // missing newline belongs).
        let diags = check_newlines(
            r#"
msgid "first\nsecond"
msgstr "premier second"
"#,
        );
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].message, "missing line feeds '\\n' (1 / 0)");
        assert!(diags[0].fix.is_none());
    }

    fn diag_with_message<'a>(diags: &'a [Diagnostic], message: &str) -> &'a Diagnostic {
        diags
            .iter()
            .find(|d| d.message == message)
            .unwrap_or_else(|| panic!("no diagnostic with message {message:?} in {diags:#?}"))
    }

    #[test]
    fn test_newlines_beginning_fix_attached() {
        // msgid has a leading LF, msgstr is missing it. The "missing LF at the
        // beginning" diagnostic carries a fix that prepends "\n"; the count
        // diagnostic that also fires has no fix.
        let diags = check_newlines(
            r#"
msgid "\ntested"
msgstr "testé"
"#,
        );
        let count = diag_with_message(&diags, "missing line feeds '\\n' (1 / 0)");
        assert!(
            count.fix.is_none(),
            "count diagnostics are not auto-fixable"
        );
        let begin = diag_with_message(&diags, "missing line feed '\\n' at the beginning");
        let fix = begin.fix.as_ref().expect("fix attached");
        assert_eq!(fix.edits.len(), 1);
        assert_eq!(fix.edits[0].range, 0..0);
        assert_eq!(fix.edits[0].replacement, "\n");
    }

    #[test]
    fn test_newlines_beginning_fix_with_cr_and_lf() {
        // msgid leading = "\n", msgstr leading = "\r\n". Both CR and LF
        // begin-diagnostics fire; both must carry the same fix so dedup
        // composes them into a single edit replacing 0..2 with "\n".
        let diags = check_newlines(
            r#"
msgid "\ntested"
msgstr "\r\ntesté"
"#,
        );
        let cr_begin = diag_with_message(&diags, "extra carriage return '\\r' at the beginning");
        let lf_begin = diag_with_message(&diags, "missing line feed '\\n' at the beginning");
        for diag in [cr_begin, lf_begin] {
            let fix = diag.fix.as_ref().expect("fix on every begin diag");
            assert_eq!(fix.edits.len(), 1);
            assert_eq!(fix.edits[0].range, 0..2);
            assert_eq!(fix.edits[0].replacement, "\n");
        }
    }

    #[test]
    fn test_newlines_end_fix_attached() {
        // msgid trails with "\n", msgstr doesn't.
        let diags = check_newlines(
            r#"
msgid "tested\n"
msgstr "testé"
"#,
        );
        let end = diag_with_message(&diags, "missing line feed '\\n' at the end");
        let fix = end.fix.as_ref().expect("fix attached");
        // msgstr value "testé" is 6 bytes (t-e-s-t-é(2)). Edit inserts "\n" at the end.
        assert_eq!(fix.edits.len(), 1);
        assert_eq!(fix.edits[0].range, 6..6);
        assert_eq!(fix.edits[0].replacement, "\n");
    }

    #[test]
    fn test_newlines_end_fix_removes_extra() {
        // msgstr has trailing "\n" that msgid doesn't.
        let diags = check_newlines(
            r#"
msgid "tested"
msgstr "testé\n"
"#,
        );
        let end = diag_with_message(&diags, "extra line feed '\\n' at the end");
        let fix = end.fix.as_ref().expect("fix attached");
        assert_eq!(fix.edits.len(), 1);
        assert_eq!(fix.edits[0].range, 6..7);
        assert_eq!(fix.edits[0].replacement, "");
    }
}