Skip to main content

alint_rules/
line_max_width.rs

1//! `line_max_width` — cap on characters per line.
2//!
3//! Counts Unicode scalar values (chars) per line, not bytes or
4//! display cells. CJK, combining marks, and emoji that occupy
5//! two terminal columns count as one char — if you want real
6//! display-width accounting, use a formatter (Biome, prettier);
7//! that's out of alint's byte/structure scope.
8//!
9//! Check-only: truncation isn't a safe auto-fix. Users either
10//! refactor the line or widen the limit.
11
12use std::path::Path;
13
14use alint_core::{
15    Context, Error, Level, PerFileRule, Result, Rule, RuleSpec, Scope, Violation, eval_per_file,
16};
17use serde::Deserialize;
18
19#[derive(Debug, Deserialize)]
20#[serde(deny_unknown_fields)]
21struct Options {
22    max_width: usize,
23}
24
25#[derive(Debug)]
26pub struct LineMaxWidthRule {
27    id: String,
28    level: Level,
29    policy_url: Option<String>,
30    message: Option<String>,
31    scope: Scope,
32    max_width: usize,
33}
34
35impl Rule for LineMaxWidthRule {
36    alint_core::rule_common_impl!();
37
38    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
39        eval_per_file(self, ctx)
40    }
41
42    fn as_per_file(&self) -> Option<&dyn PerFileRule> {
43        Some(self)
44    }
45}
46
47impl PerFileRule for LineMaxWidthRule {
48    fn path_scope(&self) -> &Scope {
49        &self.scope
50    }
51
52    fn evaluate_file(
53        &self,
54        _ctx: &Context<'_>,
55        path: &Path,
56        bytes: &[u8],
57    ) -> Result<Vec<Violation>> {
58        // `chars().count()` requires a UTF-8-validated `&str` —
59        // line widths count Unicode scalars, not bytes. Non-UTF-8
60        // files silently skip, matching the rule-major path.
61        let Ok(text) = std::str::from_utf8(bytes) else {
62            return Ok(Vec::new());
63        };
64        let Some((line_no, width)) = first_overlong_line(text, self.max_width) else {
65            return Ok(Vec::new());
66        };
67        let msg = self.message.clone().unwrap_or_else(|| {
68            format!(
69                "line {line_no} is {width} chars wide; max is {}",
70                self.max_width
71            )
72        });
73        Ok(vec![
74            Violation::new(msg)
75                .with_path(std::sync::Arc::<Path>::from(path))
76                .with_location(line_no, self.max_width + 1),
77        ])
78    }
79}
80
81fn first_overlong_line(text: &str, max_width: usize) -> Option<(usize, usize)> {
82    for (idx, line) in text.split('\n').enumerate() {
83        let trimmed = line.strip_suffix('\r').unwrap_or(line);
84        let width = trimmed.chars().count();
85        if width > max_width {
86            return Some((idx + 1, width));
87        }
88    }
89    None
90}
91
92pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
93    let _paths = spec
94        .paths
95        .as_ref()
96        .ok_or_else(|| Error::rule_config(&spec.id, "line_max_width requires a `paths` field"))?;
97    let opts: Options = spec
98        .deserialize_options()
99        .map_err(|e| Error::rule_config(&spec.id, format!("invalid options: {e}")))?;
100    if opts.max_width == 0 {
101        return Err(Error::rule_config(
102            &spec.id,
103            "line_max_width `max_width` must be > 0",
104        ));
105    }
106    if spec.fix.is_some() {
107        return Err(Error::rule_config(
108            &spec.id,
109            "line_max_width has no fix op — truncation is unsafe",
110        ));
111    }
112    Ok(Box::new(LineMaxWidthRule {
113        id: spec.id.clone(),
114        level: spec.level,
115        policy_url: spec.policy_url.clone(),
116        message: spec.message.clone(),
117        scope: Scope::from_spec(spec)?,
118        max_width: opts.max_width,
119    }))
120}
121
122#[cfg(test)]
123mod tests {
124    use super::*;
125
126    #[test]
127    fn short_file_is_ok() {
128        assert_eq!(first_overlong_line("hi\nthere\n", 10), None);
129    }
130
131    #[test]
132    fn flags_first_overlong_line() {
133        let txt = "short\nway too looooong for ten\nok\n";
134        // "way too looooong for ten" is 24 chars.
135        assert_eq!(first_overlong_line(txt, 10), Some((2, 24)));
136    }
137
138    #[test]
139    fn width_exactly_at_limit_is_ok() {
140        assert_eq!(first_overlong_line("0123456789\n", 10), None);
141    }
142
143    #[test]
144    fn crlf_is_stripped_before_counting() {
145        // "hi\r\n" should count as 2 chars ("hi"), not 3.
146        assert_eq!(first_overlong_line("hi\r\n", 2), None);
147    }
148
149    #[test]
150    fn counts_unicode_scalar_values_not_bytes() {
151        // "☃☃☃" is 3 scalars / 9 bytes. Under `max_width: 3` it's fine.
152        assert_eq!(first_overlong_line("☃☃☃\n", 3), None);
153        // Under max_width: 2 it's flagged.
154        assert_eq!(first_overlong_line("☃☃☃\n", 2), Some((1, 3)));
155    }
156}