lanekeep-core 0.3.0

Core types and execution engine for lanekeep.
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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Suppression directives: `lanekeep-ignore-next-line` and `lanekeep-ignore-file`.
//!
//! ```text
//! // lanekeep-ignore-next-line local/no-numeric-sizes reason: legacy API requires exact 44
//! minWidth: 44,
//!
//! // lanekeep-ignore-file local/no-primitive-components reason: generated fixture
//! ```
//!
//! # A suppression that does not work must say so
//!
//! The failure mode this module is arranged against is a directive that looks like it
//! silences something and does not — a typo in the rule id, a missing `reason:`, a
//! `lanekeep-ignore-nextline`. The author moves on believing the violation is handled, and
//! nothing ever tells them otherwise.
//!
//! So a malformed directive is **reported**, not skipped. Every field that could be
//! mistyped is either required or checked, and the diagnostic names what was wrong.
//!
//! # Why `reason:` is mandatory
//!
//! A suppression is a decision to accept a violation, and the next person to read it needs
//! to know whether that decision still holds. Without a reason it is indistinguishable from
//! someone silencing a diagnostic to make a build pass.
//!
//! # Scanning text, not the tree
//!
//! Directives are found by scanning the source for a standalone token rather than by
//! walking comments in the parse tree. That is what §10 specifies, it costs one pass over
//! bytes already in memory, and it works for a file that failed to parse.
//!
//! The cost is that a directive inside a string literal counts. That is a strange thing to
//! write and the consequence is a suppression that does nothing visible, which the unused
//! report surfaces.

use crate::rule_id::RuleId;

/// The token introducing a directive that covers the following line.
const NEXT_LINE: &str = "lanekeep-ignore-next-line";

/// The token introducing a directive that covers the whole file.
const WHOLE_FILE: &str = "lanekeep-ignore-file";

/// What a directive covers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scope {
    /// The line after the directive.
    NextLine,
    /// Every line in the file.
    File,
}

/// A calendar date, for `expires:`.
///
/// Deliberately not a general date type: it exists to be parsed from `YYYY-MM-DD`, compared,
/// and printed. Comparison is on the tuple, which is why the fields are in that order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Date {
    /// Four-digit year.
    pub year: u16,
    /// One-based month.
    pub month: u8,
    /// One-based day.
    pub day: u8,
}

impl Date {
    /// Parse `YYYY-MM-DD`.
    ///
    /// Rejects anything else, including a real date written another way. A directive whose
    /// expiry could not be read would otherwise never expire, which is the one outcome an
    /// expiry exists to prevent.
    #[must_use]
    pub fn parse(text: &str) -> Option<Self> {
        let bytes = text.as_bytes();
        if bytes.len() != 10 || bytes[4] != b'-' || bytes[7] != b'-' {
            return None;
        }

        let year: u16 = text.get(0..4)?.parse().ok()?;
        let month: u8 = text.get(5..7)?.parse().ok()?;
        let day: u8 = text.get(8..10)?.parse().ok()?;

        // Range-checked but not calendar-checked: 2026-02-30 is accepted. Validating month
        // lengths would need leap-year rules for no benefit — the date is only ever
        // compared, and an impossible date compares perfectly sensibly.
        if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
            return None;
        }

        Some(Self { year, month, day })
    }
}

impl std::fmt::Display for Date {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
    }
}

/// A directive that parsed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Suppression {
    /// What it covers.
    pub scope: Scope,
    /// The rules it silences. Never empty — a directive naming none is malformed.
    pub rules: Vec<RuleId>,
    /// Why, as the author wrote it.
    pub reason: String,
    /// When it stops applying, if it says.
    pub expires: Option<Date>,
    /// One-based line the directive is on.
    pub line: u32,
    /// One-based column the directive starts at.
    pub column: u32,
}

impl Suppression {
    /// Whether this directive covers a violation of `rule` at `line`.
    #[must_use]
    pub fn covers(&self, rule: &RuleId, line: u32) -> bool {
        let in_scope = match self.scope {
            Scope::File => true,
            // The line after the directive. Not the directive's own line: a trailing
            // comment on the offending line would be a different form, and supporting both
            // silently would make it unclear which one a reader is looking at.
            Scope::NextLine => line == self.line + 1,
        };
        in_scope && self.rules.contains(rule)
    }
}

/// A directive that did not parse, and why.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Malformed {
    /// One-based line.
    pub line: u32,
    /// One-based column.
    pub column: u32,
    /// What is wrong, phrased for the person who wrote it.
    pub problem: String,
}

/// Everything a file's directives amount to.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Suppressions {
    /// Directives that parsed.
    pub valid: Vec<Suppression>,
    /// Directives that did not.
    pub malformed: Vec<Malformed>,
}

impl Suppressions {
    /// Whether anything here could silence a violation.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.valid.is_empty() && self.malformed.is_empty()
    }

    /// The index of the directive covering a violation, if one does.
    ///
    /// The index rather than a boolean, so a caller can record which directives were used
    /// and report the rest as unused.
    #[must_use]
    pub fn covering(&self, rule: &RuleId, line: u32) -> Option<usize> {
        self.valid
            .iter()
            .position(|suppression| suppression.covers(rule, line))
    }
}

/// Find every directive in a file.
///
/// Never fails: a directive that cannot be understood becomes a [`Malformed`] entry rather
/// than an error, because one bad comment must not stop a file from being checked.
#[must_use]
pub fn parse(source: &str) -> Suppressions {
    let mut found = Suppressions::default();

    for (index, text) in source.lines().enumerate() {
        let line = u32::try_from(index + 1).unwrap_or(u32::MAX);

        let Some((scope, at)) = find_directive(text) else {
            continue;
        };
        let column = u32::try_from(at + 1).unwrap_or(u32::MAX);

        let token = match scope {
            Scope::NextLine => NEXT_LINE,
            Scope::File => WHOLE_FILE,
        };
        let rest = text.get(at + token.len()..).unwrap_or_default();

        match parse_body(scope, rest, line, column) {
            Ok(suppression) => found.valid.push(suppression),
            Err(problem) => found.malformed.push(Malformed {
                line,
                column,
                problem,
            }),
        }
    }

    found
}

/// Locate a directive token in a line, if it stands alone.
///
/// "Stands alone" means not adjacent to a word character on either side, which is what stops
/// prose about `lanekeep-ignore-next-line-ish` from matching. `lanekeep-ignore-file` is
/// checked first because it is not a prefix of the other, so order is only a matter of
/// finding the earlier one.
fn find_directive(text: &str) -> Option<(Scope, usize)> {
    let next_line = standalone(text, NEXT_LINE).map(|at| (Scope::NextLine, at));
    let whole_file = standalone(text, WHOLE_FILE).map(|at| (Scope::File, at));

    match (next_line, whole_file) {
        (Some(a), Some(b)) => Some(if a.1 <= b.1 { a } else { b }),
        (found, None) | (None, found) => found,
    }
}

/// The offset of `token` in `text`, if it appears as a standalone word.
fn standalone(text: &str, token: &str) -> Option<usize> {
    let mut from = 0usize;
    while let Some(offset) = text.get(from..)?.find(token) {
        let at = from + offset;
        let before = text[..at].chars().next_back();
        let after = text[at + token.len()..].chars().next();

        let bounded = !before.is_some_and(is_word)
            // A trailing `-` would make this `lanekeep-ignore-file-later`, which is not the
            // directive and must not be treated as one.
            && !after.is_some_and(|c| is_word(c) || c == '-');

        if bounded {
            return Some(at);
        }
        from = at + token.len();
    }
    None
}

const fn is_word(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_'
}

/// Parse everything after the directive token.
fn parse_body(scope: Scope, rest: &str, line: u32, column: u32) -> Result<Suppression, String> {
    // `reason:` splits the directive: rule ids before, prose after. Splitting on the keyword
    // rather than on whitespace is what lets a reason contain anything, including a colon.
    let Some((ids, tail)) = rest.split_once("reason:") else {
        return Err(format!(
            "suppression has no `reason:` — a suppression is a decision to accept a \
             violation, and the next person to read it cannot tell whether it still holds \
             without one\n  write: {} <rule-id> reason: why this is acceptable",
            token_for(scope)
        ));
    };

    let rules = parse_rules(ids)?;

    // `expires:` may follow the reason. Taken from the end so the reason keeps any text
    // before it — a reason is prose and must not be truncated by a word appearing in it.
    let (reason, expires) = match tail.rsplit_once("expires:") {
        Some((before, date)) => {
            let text = date.trim();
            let Some(parsed) = Date::parse(text) else {
                return Err(format!(
                    "suppression has an unreadable `expires: {text}` — expected \
                     YYYY-MM-DD\n  an expiry that cannot be read would never expire, which \
                     is the one thing an expiry exists to prevent"
                ));
            };
            (before.trim(), Some(parsed))
        }
        None => (tail.trim(), None),
    };

    if reason.is_empty() {
        return Err(format!(
            "suppression has an empty `reason:`\n  write: {} <rule-id> reason: why this is \
             acceptable",
            token_for(scope)
        ));
    }

    Ok(Suppression {
        scope,
        rules,
        reason: reason.to_owned(),
        expires,
        line,
        column,
    })
}

/// Parse the rule ids between the directive and `reason:`.
fn parse_rules(text: &str) -> Result<Vec<RuleId>, String> {
    let mut rules = Vec::new();

    for token in text.split([',', ' ', '\t']).filter(|t| !t.is_empty()) {
        match token.parse::<RuleId>() {
            Ok(id) => rules.push(id),
            Err(_) => {
                return Err(format!(
                    "`{token}` is not a rule id\n  ids are namespaced — `lanekeep/<name>` \
                     for built-in rules, `local/<name>` for this project's"
                ));
            }
        }
    }

    if rules.is_empty() {
        return Err(String::from(
            "suppression names no rules\n  a directive that silenced everything would hide \
             violations nobody chose to accept — name the rules it is for",
        ));
    }

    Ok(rules)
}

const fn token_for(scope: Scope) -> &'static str {
    match scope {
        Scope::NextLine => NEXT_LINE,
        Scope::File => WHOLE_FILE,
    }
}

/// Today, from the host clock.
///
/// The one place lanekeep looks at the clock, and it is deliberately here rather than in the
/// sandbox: a rule must not be able to observe the date, but a suppression's expiry has to
/// be compared against something. Callers fix it once per run so two files checked a
/// millisecond apart cannot disagree about what day it is — and any file whose result
/// depends on it says so in its cache key.
///
/// UTC, not local time. A deadline that moved with the reader's time zone would expire
/// twice in some places and not at all in others.
#[must_use]
pub fn today() -> Date {
    let seconds = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |elapsed| elapsed.as_secs());
    from_unix_days(i64::try_from(seconds / 86_400).unwrap_or(0))
}

/// Civil date from a count of days since 1970-01-01.
///
/// Howard Hinnant's `civil_from_days`, which is exact for the whole proleptic Gregorian
/// range and needs no table. Written out rather than pulled in: one function against a
/// dependency that would carry formatting, parsing and time zones for a date this only ever
/// compares.
fn from_unix_days(days: i64) -> Date {
    let z = days + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let day_of_era = z - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let shifted_month = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
    let month = if shifted_month < 10 {
        shifted_month + 3
    } else {
        shifted_month - 9
    };

    Date {
        year: u16::try_from(if month <= 2 { year + 1 } else { year }).unwrap_or(1970),
        month: u8::try_from(month).unwrap_or(1),
        day: u8::try_from(day).unwrap_or(1),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rule(id: &str) -> RuleId {
        id.parse().expect("valid id")
    }

    fn only(source: &str) -> Suppression {
        let found = parse(source);
        assert!(
            found.malformed.is_empty(),
            "unexpectedly malformed: {:?}",
            found.malformed
        );
        assert_eq!(found.valid.len(), 1, "{:?}", found.valid);
        found.valid.into_iter().next().expect("one")
    }

    fn problem(source: &str) -> String {
        let found = parse(source);
        assert!(
            found.valid.is_empty(),
            "unexpectedly valid: {:?}",
            found.valid
        );
        assert_eq!(found.malformed.len(), 1, "{:?}", found.malformed);
        found.malformed.into_iter().next().expect("one").problem
    }

    #[test]
    fn a_next_line_directive_parses() {
        let found = only("// lanekeep-ignore-next-line local/a reason: legacy\nminWidth: 44,\n");
        assert_eq!(found.scope, Scope::NextLine);
        assert_eq!(found.rules, vec![rule("local/a")]);
        assert_eq!(found.reason, "legacy");
        assert_eq!(found.line, 1);
        assert_eq!(found.expires, None);
    }

    #[test]
    fn a_file_directive_parses() {
        let found = only("// lanekeep-ignore-file local/a reason: generated fixture\n");
        assert_eq!(found.scope, Scope::File);
        assert_eq!(found.reason, "generated fixture");
    }

    #[test]
    fn several_rules_may_be_named() {
        let found = only("// lanekeep-ignore-next-line local/a, local/b lanekeep/c reason: x\n");
        assert_eq!(
            found.rules,
            vec![rule("local/a"), rule("local/b"), rule("lanekeep/c")]
        );
    }

    #[test]
    fn an_expiry_parses_and_leaves_the_reason_intact() {
        let found = only(
            "// lanekeep-ignore-file local/a reason: waiting on the rewrite expires: 2026-12-31\n",
        );
        assert_eq!(found.reason, "waiting on the rewrite");
        assert_eq!(
            found.expires,
            Some(Date {
                year: 2026,
                month: 12,
                day: 31
            })
        );
    }

    #[test]
    fn a_reason_may_contain_a_colon() {
        // Splitting on the `reason:` keyword rather than on whitespace is what allows this.
        let found = only("// lanekeep-ignore-file local/a reason: see ticket ABC-1: the API\n");
        assert_eq!(found.reason, "see ticket ABC-1: the API");
    }

    #[test]
    fn a_missing_reason_is_malformed() {
        // The failure this module exists for: a directive that looks like it works.
        let text = problem("// lanekeep-ignore-next-line local/a\n");
        assert!(text.contains("no `reason:`"), "{text}");
    }

    #[test]
    fn an_empty_reason_is_malformed() {
        let text = problem("// lanekeep-ignore-next-line local/a reason:   \n");
        assert!(text.contains("empty"), "{text}");
    }

    #[test]
    fn naming_no_rules_is_malformed() {
        // A blanket suppression would hide violations nobody chose to accept.
        let text = problem("// lanekeep-ignore-next-line reason: everything\n");
        assert!(text.contains("names no rules"), "{text}");
    }

    #[test]
    fn a_bare_rule_id_is_malformed() {
        // Namespacing is a one-way door, and a bare id here would silently silence nothing.
        let text = problem("// lanekeep-ignore-next-line no-default-export reason: x\n");
        assert!(text.contains("not a rule id"), "{text}");
        assert!(text.contains("namespaced"), "{text}");
    }

    #[test]
    fn an_unreadable_expiry_is_malformed() {
        // An expiry that cannot be read would never expire, which is the one thing an
        // expiry exists to prevent.
        for bad in [
            "31-12-2026",
            "2026/12/31",
            "soon",
            "2026-13-01",
            "2026-12-32",
        ] {
            let text = problem(&format!(
                "// lanekeep-ignore-file local/a reason: x expires: {bad}\n"
            ));
            assert!(text.contains("unreadable"), "`{bad}` gave: {text}");
        }
    }

    #[test]
    fn prose_mentioning_the_directive_does_not_match() {
        // §10: the directive must be a standalone token.
        for prose in [
            "// use lanekeep-ignore-next-liner for this\n",
            "// see lanekeep-ignore-file-format docs\n",
            "// xlanekeep-ignore-file local/a reason: x\n",
        ] {
            let found = parse(prose);
            assert!(
                found.is_empty(),
                "prose matched as a directive: {prose:?} -> {found:?}"
            );
        }
    }

    #[test]
    fn a_directive_is_found_wherever_it_sits_on_the_line() {
        let found = only("const a = 1; // lanekeep-ignore-next-line local/a reason: x\n");
        assert_eq!(found.line, 1);
        assert!(found.column > 1, "column should point at the directive");
    }

    #[test]
    fn several_directives_in_one_file_all_parse() {
        let found = parse(
            "// lanekeep-ignore-file local/a reason: one\n\
             const x = 1;\n\
             // lanekeep-ignore-next-line local/b reason: two\n\
             const y = 2;\n",
        );
        assert_eq!(found.valid.len(), 2);
        assert_eq!(found.valid[0].line, 1);
        assert_eq!(found.valid[1].line, 3);
    }

    #[test]
    fn a_malformed_directive_does_not_stop_the_others() {
        // One bad comment must not stop a file from being checked.
        let found = parse(
            "// lanekeep-ignore-next-line local/a\n\
             const x = 1;\n\
             // lanekeep-ignore-next-line local/b reason: fine\n",
        );
        assert_eq!(found.valid.len(), 1);
        assert_eq!(found.malformed.len(), 1);
    }

    // --- what a directive covers ---------------------------------------------------------

    #[test]
    fn next_line_covers_the_following_line_only() {
        let found = only("// lanekeep-ignore-next-line local/a reason: x\nconst y = 1;\n");
        assert!(found.covers(&rule("local/a"), 2));
        assert!(!found.covers(&rule("local/a"), 1), "not its own line");
        assert!(
            !found.covers(&rule("local/a"), 3),
            "not the line after that"
        );
    }

    #[test]
    fn a_directive_covers_only_the_rules_it_names() {
        let found = only("// lanekeep-ignore-next-line local/a reason: x\n");
        assert!(found.covers(&rule("local/a"), 2));
        assert!(!found.covers(&rule("local/b"), 2));
    }

    #[test]
    fn file_scope_covers_every_line() {
        let found = only("// lanekeep-ignore-file local/a reason: x\n");
        for line in [1, 2, 500] {
            assert!(found.covers(&rule("local/a"), line));
        }
    }

    #[test]
    fn covering_reports_which_directive_matched() {
        // The index, not a boolean, so a caller can tell which directives went unused.
        let found = parse(
            "// lanekeep-ignore-next-line local/a reason: one\n\
             const x = 1;\n\
             // lanekeep-ignore-next-line local/b reason: two\n\
             const y = 2;\n",
        );
        assert_eq!(found.covering(&rule("local/a"), 2), Some(0));
        assert_eq!(found.covering(&rule("local/b"), 4), Some(1));
        assert_eq!(found.covering(&rule("local/c"), 2), None);
    }

    // --- dates ----------------------------------------------------------------------------

    #[test]
    fn dates_compare_chronologically() {
        let earlier = Date::parse("2026-01-31").expect("valid");
        let later = Date::parse("2026-02-01").expect("valid");
        assert!(earlier < later);

        let next_year = Date::parse("2027-01-01").expect("valid");
        assert!(later < next_year);
    }

    #[test]
    fn known_epochs_convert_correctly() {
        // Fixed points, including a leap day and a century boundary, so the conversion is
        // checked against something other than itself.
        for (days, expected) in [
            (0, "1970-01-01"),
            (18_993, "2022-01-01"),
            (19_051, "2022-02-28"),
            (11_016, "2000-02-29"),
            (20_666, "2026-08-01"),
        ] {
            assert_eq!(from_unix_days(days).to_string(), expected, "day {days}");
        }
    }

    #[test]
    fn today_is_a_plausible_date() {
        let now = today();
        assert!(now.year >= 2024 && now.year < 2200, "{now}");
        assert!((1..=12).contains(&now.month), "{now}");
        assert!((1..=31).contains(&now.day), "{now}");
    }

    #[test]
    fn a_date_renders_back_to_its_input() {
        assert_eq!(
            Date::parse("2026-08-01").expect("valid").to_string(),
            "2026-08-01"
        );
    }
}