lintje 0.6.0

Lintje is an opinionated linter for Git.
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
use crate::branch::Branch;
use crate::commit::Commit;
use crate::rule::Violation;
use crate::utils::{display_width, indent_string};

pub fn formatted_commit_violation(commit: &Commit, violation: &Violation) -> String {
    let mut s = String::from("");
    s.push_str(&format!("{}: {}\n", violation.rule, violation.message));
    s.push_str("  ");
    match &commit.short_sha {
        Some(sha) => s.push_str(sha),
        None => s.push_str("0000000"),
    }
    if let Some(line_number) = &violation.position.line_number() {
        s.push_str(&format!(":{}", line_number));
    }
    if let Some(column) = &violation.position.column() {
        s.push_str(&format!(":{}", column));
    }
    s.push_str(&format!(": {}", commit.subject));
    s.push('\n');
    s.push_str(&indent_string(formatted_context(violation), 2));
    s.push('\n');
    s
}

pub fn formatted_branch_violation(branch: &Branch, violation: &Violation) -> String {
    let mut s = String::from("");
    s.push_str(&format!("{}: {}\n", violation.rule, violation.message));
    s.push_str("  Branch");
    if let Some(column) = &violation.position.column() {
        s.push_str(&format!(":{}", column));
    }
    s.push_str(&format!(": {}", branch.name));
    s.push('\n');
    s.push_str(&indent_string(formatted_context(violation), 2));
    s.push('\n');
    s
}

pub fn formatted_context(violation: &Violation) -> String {
    let mut s = String::from("");
    let mut first_line = true;
    let line_number_width = &violation
        .context
        .iter()
        .map(|l| match l.source.line_number() {
            Some(line_number) => (line_number + 1).to_string().chars().count() + 1,
            None => 0,
        })
        .max()
        .unwrap_or(0);

    for context in &violation.context {
        let plain_line_number = if let Some(line_number) = context.source.line_number() {
            format!("{} ", line_number + 1)
        } else {
            "".to_string()
        };
        let line_prefix = format!("{:>spaces$}", plain_line_number, spaces = line_number_width);
        let empty_prefix = " ".repeat(line_prefix.len());
        if first_line {
            // Add empty line to give some space between violation and commit lines
            s.push_str(&format!("{}|\n", empty_prefix));
        }

        // Add line that provides context to the violation
        let content = &context.source.content();
        // Print tabs as 4 spaces because that will render more consistently than render the tab
        // character
        let formatted_content = content.replace("\t", "    ");
        s.push_str(&format!("{}| {}\n", line_prefix, formatted_content));

        // Add a hint if any
        if let Some(hint) = &context.hint {
            let range_start = hint.range.start;
            let leading = match content.get(0..range_start) {
                Some(v) => display_width(v),
                None => range_start,
            };
            let range_end = hint.range.end;
            let rest = match content.get(range_start..range_end) {
                Some(v) => display_width(v),
                None => hint.range.len(),
            };

            let leading_spaces = " ".repeat(leading);
            let underline = "^".repeat(rest);
            let message = format!("{}{} {}", leading_spaces, underline, hint.message);
            s.push_str(&format!("{}| {}\n", empty_prefix, message));
        }
        first_line = false;
    }
    s
}

#[cfg(test)]
mod tests {
    use super::{formatted_branch_violation, formatted_commit_violation, formatted_context};
    use crate::branch::Branch;
    use crate::commit::Commit;
    use crate::rule::{Context, Position, Rule, Violation};
    use core::ops::Range;

    fn commit<S: AsRef<str>>(sha: Option<String>, subject: S, message: S) -> Commit {
        Commit::new(
            sha,
            Some("test@example.com".to_string()),
            subject.as_ref().to_string(),
            message.as_ref().to_string(),
            true,
        )
    }

    fn subject_violation_hint(value: &str, message: &str, range: Range<usize>) -> Violation {
        let context = Context::subject_hint(value.to_string(), range, message.to_string());
        Violation {
            rule: Rule::SubjectLength,
            message: "Dummy message".to_string(),
            position: Position::Subject { column: 0 },
            context: vec![context],
        }
    }

    #[test]
    fn test_formatted_commit_violation_without_sha() {
        let commit = commit(None, "Subject", "Message");
        let context = vec![Context::subject("Subject".to_string())];
        let violation = Violation {
            rule: Rule::SubjectLength,
            message: "The error message".to_string(),
            position: Position::Subject { column: 1 },
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "SubjectLength: The error message\n\
            \x20\x200000000:1:1: Subject\n\
            \x20\x20  |\n\
            \x20\x201 | Subject\n\n"
        );
    }

    #[test]
    fn test_formatted_commit_violation_subject() {
        let commit = commit(Some("1234567".to_string()), "Subject", "Message");
        let context = vec![Context::subject("Subject".to_string())];
        let violation = Violation {
            rule: Rule::SubjectLength,
            message: "The error message".to_string(),
            position: Position::Subject { column: 1 },
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "SubjectLength: The error message\n\
            \x20\x201234567:1:1: Subject\n\
            \x20\x20  |\n\
            \x20\x201 | Subject\n\n"
        );
    }

    #[test]
    fn test_formatted_commit_violation_subject_hint() {
        let commit = commit(Some("1234567".to_string()), "Subject", "Message");
        let context = vec![Context::subject_hint(
            "Subject".to_string(),
            Range { start: 1, end: 3 },
            "The hint".to_string(),
        )];
        let violation = Violation {
            rule: Rule::SubjectMood,
            message: "The error message".to_string(),
            position: Position::Subject { column: 2 },
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "SubjectMood: The error message\n\
            \x20\x201234567:1:2: Subject\n\
            \x20\x20  |\n\
            \x20\x201 | Subject\n\
            \x20\x20  |  ^^ The hint\n\n"
        );
    }

    #[test]
    fn test_formatted_commit_violation_message_line() {
        let commit = commit(Some("1234567".to_string()), "Subject", "Message");
        let context = vec![Context::message_line(9, "Message line".to_string())];
        let violation = Violation {
            rule: Rule::MessageLineLength,
            message: "The error message".to_string(),
            position: Position::MessageLine {
                line: 10,
                column: 50,
            },
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "MessageLineLength: The error message\n\
            \x20\x201234567:11:50: Subject\n\
            \x20\x20   |\n\
            \x20\x2011 | Message line\n\n"
        );
    }

    #[test]
    fn test_formatted_commit_violation_message_line_hint() {
        let commit = commit(Some("1234567".to_string()), "Subject", "Message");
        let context = vec![
            Context::message_line(9, "Message line".to_string()),
            Context::message_line_hint(
                10,
                "Message line with hint".to_string(),
                Range { start: 3, end: 10 },
                "My hint".to_string(),
            ),
        ];
        let violation = Violation {
            rule: Rule::MessageLineLength,
            message: "The error message".to_string(),
            position: Position::MessageLine {
                line: 10,
                column: 50,
            },
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "MessageLineLength: The error message\n\
            \x20\x201234567:11:50: Subject\n\
            \x20\x20   |\n\
            \x20\x2011 | Message line\n\
            \x20\x2012 | Message line with hint\n\
            \x20\x20   |    ^^^^^^^ My hint\n\n"
        );
    }

    #[test]
    fn test_formatted_commit_violation_diff_hint() {
        let commit = commit(Some("1234567".to_string()), "Subject", "Message");
        let context = vec![Context::diff_hint(
            "Diff line".to_string(),
            Range { start: 3, end: 5 },
            "My hint".to_string(),
        )];
        let violation = Violation {
            rule: Rule::DiffPresence,
            message: "The error message".to_string(),
            position: Position::Diff,
            context,
        };
        let output = formatted_commit_violation(&commit, &violation);
        assert_eq!(
            output,
            "DiffPresence: The error message\n\
            \x20\x201234567: Subject\n\
            \x20\x20|\n\
            \x20\x20| Diff line\n\
            \x20\x20|    ^^ My hint\n\n"
        );
    }

    #[test]
    fn test_formatted_branch_violation_branch_hint() {
        let branch = Branch::new("branch-name".to_string());
        let context = vec![Context::branch_hint(
            "branch-name".to_string(),
            Range { start: 3, end: 5 },
            "My hint".to_string(),
        )];
        let violation = Violation {
            rule: Rule::BranchNameLength,
            message: "The error message".to_string(),
            position: Position::Branch { column: 3 },
            context,
        };
        let output = formatted_branch_violation(&branch, &violation);
        assert_eq!(
            output,
            "BranchNameLength: The error message\n\
            \x20\x20Branch:3: branch-name\n\
            \x20\x20|\n\
            \x20\x20| branch-name\n\
            \x20\x20|    ^^ My hint\n\n"
        );
    }

    #[test]
    fn formatted_context_subject() {
        let context = vec![
            Context::subject("Subject".to_string()),
            Context::message_line(0, "".to_string()),
            Context::message_line(1, "Line 1".to_string()),
        ];
        let violation = Violation {
            rule: Rule::SubjectLength,
            message: "Dummy message".to_string(),
            position: Position::MessageLine { line: 0, column: 0 },
            context,
        };
        assert_eq!(
            formatted_context(&violation),
            "\x20 |\n\
                1 | Subject\n\
                2 | \n\
                3 | Line 1\n"
        );
    }

    #[test]
    fn formatted_context_message_multi_line() {
        let context = vec![
            Context::message_line(7, "Line 9".to_string()),
            Context::message_line(8, "Line 10".to_string()),
            Context::message_line(9, "Line 11".to_string()),
            Context::message_line_hint(
                10,
                "Line 12".to_string(),
                Range { start: 1, end: 2 },
                "Message".to_string(),
            ),
        ];
        let violation = Violation {
            rule: Rule::SubjectLength,
            message: "Dummy message".to_string(),
            position: Position::MessageLine { line: 0, column: 0 },
            context,
        };
        assert_eq!(
            formatted_context(&violation),
            "\x20\x20 |\n\
                \x209 | Line 9\n\
                   10 | Line 10\n\
                   11 | Line 11\n\
                   12 | Line 12\n\
             \x20\x20 |  ^ Message\n"
        );
    }

    #[test]
    fn formatted_context_branch() {
        let context = vec![Context::branch_hint(
            "branch-name".to_string(),
            Range { start: 1, end: 3 },
            "A message".to_string(),
        )];
        let violation = Violation {
            rule: Rule::BranchNameLength,
            message: "Dummy message".to_string(),
            position: Position::Branch { column: 0 },
            context,
        };
        assert_eq!(
            formatted_context(&violation),
            "|\n\
             | branch-name\n\
             |  ^^ A message\n"
        );
    }

    #[test]
    fn formatted_context_diff() {
        let context = vec![Context::diff_hint(
            "Some diff".to_string(),
            Range { start: 1, end: 3 },
            "A message".to_string(),
        )];
        let violation = Violation {
            rule: Rule::DiffPresence,
            message: "Dummy message".to_string(),
            position: Position::Diff,
            context,
        };
        assert_eq!(
            formatted_context(&violation),
            "|\n\
             | Some diff\n\
             |  ^^ A message\n"
        );
    }

    #[test]
    fn formatted_context_ascii() {
        let v_start = subject_violation_hint("Lorem ipsum", "A lorem", Range { start: 0, end: 5 });
        assert_eq!(
            formatted_context(&v_start),
            "\x20\x20|\n\
                   1 | Lorem ipsum\n\
             \x20\x20| ^^^^^ A lorem\n"
        );

        let v_end = subject_violation_hint("Lorem ipsum", "A sum", Range { start: 8, end: 11 });
        assert_eq!(
            formatted_context(&v_end),
            "\x20\x20|\n\
                   1 | Lorem ipsum\n\
             \x20\x20|         ^^^ A sum\n"
        );

        let v_middle = subject_violation_hint("Lorem ipsum", "A space", Range { start: 5, end: 6 });
        assert_eq!(
            formatted_context(&v_middle),
            "\x20\x20|\n\
                   1 | Lorem ipsum\n\
             \x20\x20|      ^ A space\n"
        );
    }

    #[test]
    fn formatted_context_whitespace() {
        let v_space = subject_violation_hint(" Lorem ipsum", "A space", Range { start: 0, end: 1 });
        assert_eq!(
            formatted_context(&v_space),
            "\x20\x20|\n\
                   1 |  Lorem ipsum\n\
             \x20\x20| ^ A space\n"
        );

        let v_space =
            subject_violation_hint("\x20Lorem ipsum", "A space", Range { start: 0, end: 1 });
        assert_eq!(
            formatted_context(&v_space),
            "\x20\x20|\n\
                   1 | \x20Lorem ipsum\n\
             \x20\x20| ^ A space\n"
        );

        let v_tab = subject_violation_hint(
            "\tLorem ipsum",
            "A tab",
            Range {
                start: 0,
                end: "\t".len(),
            },
        );
        assert_eq!(
            formatted_context(&v_tab),
            "\x20\x20|\n\
                   1 |     Lorem ipsum\n\
             \x20\x20| ^^^^ A tab\n"
        );
    }

    #[test]
    fn formatted_context_accents() {
        // This accented character is two characters, the `a` and the accent, but renders as one
        // column. The character is 3 bytes.
        //
        // This test makes sure the formatted_context function points to the single column, because
        // it has a display width of one, and not two columns because it's two characters.
        let v = subject_violation_hint(
            "This is a̐ char with an accent",
            "Mark accent",
            Range { start: 8, end: 11 },
        );
        assert_eq!(
            formatted_context(&v),
            "\x20\x20|\n\
                   1 | This is a̐ char with an accent\n\
             \x20\x20|         ^ Mark accent\n"
        );
    }

    #[test]
    fn formatted_context_emoji() {
        let v = subject_violation_hint("Aa😀Bb", "Mark emoji", Range { start: 2, end: 4 });
        assert_eq!(
            formatted_context(&v),
            "\x20\x20|\n\
                   1 | Aa😀Bb\n\
             \x20\x20|   ^^ Mark emoji\n"
        );

        let v = subject_violation_hint("Aa👍Bb", "Mark emoji", Range { start: 2, end: 4 });
        assert_eq!(
            formatted_context(&v),
            "\x20\x20|\n\
                   1 | Aa👍Bb\n\
             \x20\x20|   ^^ Mark emoji\n"
        );

        let v = subject_violation_hint(
            "Fix ❤️ in controller Fix #123",
            "Mark fix ticket",
            Range { start: 25, end: 33 },
        );
        assert_eq!(
            formatted_context(&v),
            "\x20\x20|\n\
                   1 | Fix ❤️ in controller Fix #123\n\
             \x20\x20|                     ^^^^^^^^ Mark fix ticket\n"
        );
    }

    #[test]
    fn formatted_context_double_width() {
        let v = subject_violation_hint(
            "ああああああああああああああああああああああああああ",
            "Mark double width character",
            Range { start: 75, end: 78 },
        );
        assert_eq!(
            formatted_context(&v),
            "\x20\x20|\n\
                   1 | ああああああああああああああああああああああああああ\n\
             \x20\x20|                                                   ^^ Mark double width character\n"
        );
    }
}