mit-lint 4.0.2

Lints for commits parsed with mit-commit.
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
use miette::SourceOffset;
use mit_commit::CommitMessage;

use crate::model::{Code, Problem};

/// Builder for creating Problem instances with a fluent interface
pub struct ProblemBuilder {
    error: String,
    tip: String,
    code: Code,
    commit_message: String,
    labels: Vec<(String, usize, usize)>,
    url: Option<String>,
}

impl ProblemBuilder {
    /// Create a new problem builder with required fields
    ///
    /// # Arguments
    ///
    /// * `error` - The error message
    /// * `tip` - Advice on how to fix the problem
    /// * `code` - The error code
    /// * `commit_message` - The commit message that has the problem
    ///
    /// # Returns
    ///
    /// A new `ProblemBuilder` instance
    pub fn new(
        error: impl Into<String>,
        tip: impl Into<String>,
        code: Code,
        commit_message: &CommitMessage<'_>,
    ) -> Self {
        Self {
            error: error.into(),
            tip: tip.into(),
            code,
            commit_message: String::from(commit_message.clone()),
            labels: Vec::new(),
            url: None,
        }
    }

    /// Add a URL with more information about the problem
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to documentation about this problem
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_url(mut self, url: impl Into<String>) -> Self {
        self.url = Some(url.into());
        self
    }

    /// Add a label to highlight a specific part of the commit message
    ///
    /// # Arguments
    ///
    /// * `text` - The label text
    /// * `position` - The byte offset in the commit message
    /// * `length` - The length of the highlighted section in bytes
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_label(mut self, text: impl Into<String>, position: usize, length: usize) -> Self {
        self.labels.push((text.into(), position, length));
        self
    }

    /// Add a label for a line that exceeds a character limit
    ///
    /// # Arguments
    ///
    /// * `commit_text` - The full commit message text
    /// * `line_index` - The zero-based index of the line
    /// * `line` - The content of the line
    /// * `limit` - The character limit
    /// * `label_text` - The text to show in the label
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_label_for_line(
        self,
        commit_text: &str,
        line_index: usize,
        line: &str,
        limit: usize,
        label_text: impl Into<String>,
    ) -> Self {
        if line.chars().count() <= limit {
            return self;
        }

        // SourceOffset::from_location expects a 1-indexed CHARACTER column,
        // not a byte offset. Passing a byte offset here breaks when the line
        // contains multi-byte UTF-8 characters before `limit` (e.g. Umlauts),
        // because the byte offset exceeds the character count and from_location
        // overshoots the target position. Use the character count directly.
        let position = SourceOffset::from_location(commit_text, line_index + 1, limit + 1).offset();

        let length: usize = line.chars().skip(limit).map(char::len_utf8).sum();

        self.with_label(label_text, position, length)
    }

    /// Add a label for the last non-empty line of the commit message.
    ///
    /// This is useful for indicating that something is missing at the end of the commit message.
    ///
    /// # Arguments
    ///
    /// * `label_text` - The text to show in the label
    pub fn with_label_at_last_line(self, label_text: impl Into<String>) -> Self {
        let original = &self.commit_message;
        let trimmed = original.trim_end();
        let trimmed_len = trimmed.len();

        // Find the last newline in the trimmed string
        let last_line_start = trimmed.rfind('\n').map_or(0, |pos| pos + 1);
        let last_line_length = trimmed_len - last_line_start;
        self.with_label(label_text, last_line_start, last_line_length)
    }

    /// Build the Problem instance
    ///
    /// # Returns
    ///
    /// A Problem instance with all the configured properties
    pub fn build(self) -> Problem {
        let labels = if self.labels.is_empty() {
            None
        } else {
            Some(self.labels)
        };

        Problem::new(
            self.error,
            self.tip,
            self.code,
            &self.commit_message.into(),
            labels,
            self.url,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use miette::Diagnostic;
    use mit_commit::CommitMessage;

    #[test]
    fn test_builder_creates_problem_with_basic_fields() {
        let commit = CommitMessage::from("Test commit");
        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .build();

        assert_eq!(problem.error(), "Error message");
        assert_eq!(problem.tip(), "Fix advice");
        assert_eq!(problem.code(), &Code::BodyWiderThan72Characters);
        assert_eq!(problem.commit_message(), commit);
    }

    #[test]
    fn test_builder_adds_url() {
        let commit = CommitMessage::from("Test commit");
        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_url("https://example.com")
        .build();

        // We can't directly access the URL, but we can check the diagnostic output
        let diagnostic_output = format!("{problem:?}");
        assert!(diagnostic_output.contains("https://example.com"));
    }

    #[test]
    fn test_builder_adds_labels() {
        let commit = CommitMessage::from("Test commit");
        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label("Label 1", 0, 4)
        .with_label("Label 2", 5, 6)
        .build();

        // We can't directly access the labels, but we can check the diagnostic output
        let diagnostic_output = format!("{problem:?}");
        assert!(diagnostic_output.contains("Label 1"));
        assert!(diagnostic_output.contains("Label 2"));
    }

    #[test]
    fn test_with_label_for_line() {
        let commit_text = "Subject\n\nThis is a very long line that exceeds the character limit";
        let commit = CommitMessage::from(commit_text);

        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label_for_line(
            commit_text,
            2,
            "This is a very long line that exceeds the character limit",
            10,
            "Too long",
        )
        .build();

        // We can't directly access the labels, but we can check the diagnostic output
        let diagnostic_output = format!("{problem:?}");
        assert!(diagnostic_output.contains("Too long"));
    }

    #[test]
    fn test_with_label_for_line_does_not_add_label_if_within_limit() {
        let commit_text = "Subject\n\nShort line";
        let commit = CommitMessage::from(commit_text);

        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label_for_line(commit_text, 2, "Short line", 72, "Too long")
        .build();

        // Check that no labels were added
        let diagnostic_output = format!("{problem:?}");
        assert!(!diagnostic_output.contains("Too long"));
    }

    #[test]
    fn test_builder_with_multiple_methods_chained() {
        let commit_text = "Subject\n\nThis is a very long line that exceeds the character limit";
        let commit = CommitMessage::from(commit_text);

        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_url("https://example.com")
        .with_label("Manual label", 0, 7)
        .with_label_for_line(
            commit_text,
            2,
            "This is a very long line that exceeds the character limit",
            10,
            "Too long",
        )
        .build();

        let diagnostic_output = format!("{problem:?}");
        assert!(diagnostic_output.contains("https://example.com"));
        assert!(diagnostic_output.contains("Manual label"));
        assert!(diagnostic_output.contains("Too long"));
    }

    #[test]
    fn test_with_label_for_line_position_calculation() {
        // Setup a multi-line commit message with known positions
        let commit_text = "Subject\nSecond line\nThis is a line that exceeds the limit";
        let commit = CommitMessage::from(commit_text);

        // The line we're testing is the third line (index 2)
        let line_index = 2;
        let line = "This is a line that exceeds the limit";
        let limit = 10;

        // Create a problem with a label at the position where the line exceeds the limit
        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label_for_line(commit_text, line_index, line, limit, "Too long")
        .build();

        // Calculate the expected position - the start of the third line plus the first 10 characters
        let third_line_offset = commit_text.find("This is a line").unwrap();
        let first_ten_chars_length = line.chars().take(limit).map(char::len_utf8).sum::<usize>();
        let expected_position = third_line_offset + first_ten_chars_length;

        // Verify the label is positioned correctly
        let labels = problem.labels().unwrap().collect::<Vec<_>>();
        assert_eq!(
            labels[0].offset(),
            expected_position,
            "Label should be positioned at character {} (after the first {} characters of line {})",
            expected_position,
            limit,
            line_index + 1
        );
    }

    #[test]
    fn test_with_label_for_line_uses_byte_length_for_unicode() {
        // A line with exactly 72 ASCII characters followed by multi-byte Unicode characters
        let prefix = "a".repeat(72);
        let suffix = "éé"; // each 'é' is 2 bytes in UTF-8
        let line = format!("{prefix}{suffix}");
        let commit_text = format!("Subject\n\n{line}");
        let commit = CommitMessage::from(commit_text.as_str());

        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label_for_line(&commit_text, 2, &line, 72, "Too long")
        .build();

        let labels = problem.labels().unwrap().collect::<Vec<_>>();
        assert_eq!(labels.len(), 1);

        // The label should cover the byte length of the suffix, not the character count
        let expected_length = suffix.len(); // 4 bytes for "éé"
        assert_eq!(
            labels[0].len(),
            expected_length,
            "Label length should be {} bytes for the suffix '{}' ({} chars, {} bytes), but got {}",
            expected_length,
            suffix,
            suffix.chars().count(),
            suffix.len(),
            labels[0].len()
        );
    }

    #[test]
    fn test_with_label_for_line_multibyte_before_limit() {
        // A body line with multi-byte UTF-8 characters BEFORE the 72-char limit.
        // Each 'é' is 2 bytes in UTF-8. The line has 80 'é' chars = 160 bytes.
        //
        // The label should start at the 73rd character (char index 72),
        // which is at byte offset 72*2 = 144 within the line.
        // Total byte offset in commit_text = 9 ("Subject\n\n") + 144 = 153.
        //
        // BUG: with_label_for_line passes a BYTE offset (144) + 1 = 145 as the
        // character column to SourceOffset::from_location, which interprets it
        // as a character position. Since the line only has 80 characters,
        // from_location overshoots and returns the wrong offset.
        let line = "é".repeat(80);
        let commit_text = format!("Subject\n\n{line}");
        let commit = CommitMessage::from(commit_text.as_str());

        let problem = ProblemBuilder::new(
            "Error message",
            "Fix advice",
            Code::BodyWiderThan72Characters,
            &commit,
        )
        .with_label_for_line(&commit_text, 2, &line, 72, "Too long")
        .build();

        let labels = problem.labels().unwrap().collect::<Vec<_>>();
        assert_eq!(labels.len(), 1);

        // Expected: byte offset 9 + 144 = 153, byte length 8*2 = 16
        let expected_offset = "Subject\n\n".len() + 72 * 2; // 153
        assert_eq!(
            labels[0].offset(),
            expected_offset,
            "Label offset should be {} (byte offset of the 73rd char), but got {}",
            expected_offset,
            labels[0].offset()
        );

        // The remaining 8 chars are each 2 bytes = 16 bytes
        let expected_length = (80 - 72) * 2; // 16
        assert_eq!(
            labels[0].len(),
            expected_length,
            "Label length should be {} bytes for the 8 remaining 'é' chars",
            expected_length
        );
    }

    #[test]
    fn test_build_preserves_commit_message_without_unnecessary_clone() {
        // Regression test: build() used to clone the commit_message String unnecessarily.
        // Verify the commit message is preserved correctly through the build process.
        let original_text = "Test commit message with unicode: ñ ö ü";
        let commit = CommitMessage::from(original_text);
        let problem =
            ProblemBuilder::new("Error", "Fix it", Code::BodyWiderThan72Characters, &commit)
                .build();

        assert_eq!(
            problem.commit_message(),
            CommitMessage::from(original_text),
            "Commit message should be preserved through build()"
        );
    }
}