vize_patina 0.24.0

Patina - The quality checker for Vize code linting
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
//! musea/prefer-design-tokens
//!
//! Warn when CSS style blocks contain hardcoded values that match known
//! design token primitive values. Components should reference tokens via
//! CSS custom properties instead of duplicating raw values.
//!
//! ## Examples
//!
//! ### Invalid
//! ```css
//! .button {
//!   background: #3b82f6;  /* matches primitive token color.primary */
//! }
//! ```
//!
//! ### Valid
//! ```css
//! .button {
//!   background: var(--color-primary);
//! }
//! ```

#![allow(clippy::disallowed_macros)]

use memchr::memmem;
use vize_carton::FxHashMap;

use super::{MuseaLintResult, MuseaRuleMeta};
use crate::diagnostic::{Fix, LintDiagnostic, Severity, TextEdit};
use vize_carton::String;
use vize_carton::ToCompactString;

static META: MuseaRuleMeta = MuseaRuleMeta {
    name: "musea/prefer-design-tokens",
    description: "Prefer design token CSS variables over hardcoded primitive values",
    default_severity: Severity::Warning,
};

/// A known design token for matching against hardcoded values
#[derive(Debug, Clone)]
pub struct TokenInfo {
    /// Token path (e.g., "color.primary")
    pub path: String,
    /// CSS variable name (e.g., "--color-primary")
    pub var_name: String,
    /// Tier: "primitive" or "semantic"
    pub tier: String,
}

/// Configuration for the prefer-design-tokens rule
#[derive(Debug, Clone, Default)]
pub struct PreferDesignTokensConfig {
    /// Map of normalized value -> token info
    /// Multiple tokens may map to the same value
    pub value_map: FxHashMap<String, Vec<TokenInfo>>,
}

impl PreferDesignTokensConfig {
    /// Add a token to the configuration
    pub fn add_token(&mut self, value: &str, path: &str, tier: &str) {
        let normalized = normalize_value(value);
        let var_name: String = format!("--{}", path.replace('.', "-")).into();
        let info = TokenInfo {
            path: path.to_compact_string(),
            var_name,
            tier: tier.to_compact_string(),
        };
        self.value_map.entry(normalized).or_default().push(info);
    }
}

/// Rule: prefer design token CSS variables
pub struct PreferDesignTokens {
    config: PreferDesignTokensConfig,
}

impl PreferDesignTokens {
    pub fn new(config: PreferDesignTokensConfig) -> Self {
        Self { config }
    }

    pub fn meta() -> &'static MuseaRuleMeta {
        &META
    }

    /// Check a source file for hardcoded token values in style blocks
    pub fn check(&self, source: &str, result: &mut MuseaLintResult) {
        if self.config.value_map.is_empty() {
            return;
        }

        let bytes = source.as_bytes();

        // Find all <style> blocks
        let style_finder = memmem::Finder::new(b"<style");
        let style_close_finder = memmem::Finder::new(b"</style>");
        let mut search_start = 0;

        while let Some(style_pos) = style_finder.find(&bytes[search_start..]) {
            let abs_style_start = search_start + style_pos;

            // Find > to get end of opening tag
            let Some(tag_end_offset) = memchr::memchr(b'>', &bytes[abs_style_start..]) else {
                break;
            };
            let content_start = abs_style_start + tag_end_offset + 1;

            // Find </style>
            let Some(close_pos) = style_close_finder.find(&bytes[content_start..]) else {
                break;
            };
            let content_end = content_start + close_pos;

            // Extract and check the CSS content
            if let Ok(css_content) = std::str::from_utf8(&bytes[content_start..content_end]) {
                self.check_css_block(css_content, content_start, result);
            }

            search_start = content_end + 8; // skip </style>
        }
    }

    /// Check a CSS block for hardcoded token values
    fn check_css_block(&self, css: &str, block_offset: usize, result: &mut MuseaLintResult) {
        for (line_idx, line) in css.lines().enumerate() {
            let trimmed = line.trim();

            // Skip empty lines, comments, selectors
            if trimmed.is_empty()
                || trimmed.starts_with("/*")
                || trimmed.starts_with("//")
                || trimmed.starts_with("}")
                || trimmed.starts_with("{")
                || trimmed.ends_with('{')
            {
                continue;
            }

            // Check for property: value pattern
            if let Some(colon_pos) = trimmed.find(':') {
                let value_part = trimmed[colon_pos + 1..].trim();
                // Remove trailing semicolon and !important
                let value_part = value_part
                    .trim_end_matches(';')
                    .trim_end_matches("!important")
                    .trim();

                // Skip values that already use var()
                if value_part.contains("var(") {
                    continue;
                }

                // Check individual value tokens (split on whitespace for shorthand properties)
                let line_byte_offset = css[..css
                    .lines()
                    .take(line_idx)
                    .map(|l| l.len() + 1)
                    .sum::<usize>()]
                    .len();
                let line_start = block_offset + line_byte_offset;
                let line_end = line_start + line.len();

                // Check the full value first
                let normalized_full = normalize_value(value_part);
                if let Some(tokens) = self.config.value_map.get(&normalized_full) {
                    // Prefer primitive tokens for warnings
                    let token = tokens
                        .iter()
                        .find(|t| t.tier == "primitive")
                        .unwrap_or(&tokens[0]);

                    let message = if token.tier == "primitive" {
                        format!(
                            "Hardcoded value '{}' matches primitive token '{}' — use var({})",
                            value_part, token.path, token.var_name
                        )
                    } else {
                        format!(
                            "Hardcoded value '{}' matches token '{}' — use var({})",
                            value_part, token.path, token.var_name
                        )
                    };

                    let fix = Fix::new(
                        format!("Replace with var({})", token.var_name),
                        TextEdit::replace(
                            line_start as u32,
                            line_end as u32,
                            line.replace(value_part, &format!("var({})", token.var_name)),
                        ),
                    );

                    result.add_diagnostic(
                        LintDiagnostic::warn(
                            META.name,
                            message,
                            line_start as u32,
                            line_end as u32,
                        )
                        .with_help(format!(
                            "Use `var({})` for consistent theming and maintainability",
                            token.var_name
                        ))
                        .with_fix(fix),
                    );
                    continue;
                }

                // Check individual tokens in shorthand values (e.g., "1px solid #3b82f6")
                for part in value_part.split_whitespace() {
                    let normalized = normalize_value(part);
                    if let Some(tokens) = self.config.value_map.get(&normalized) {
                        let token = tokens
                            .iter()
                            .find(|t| t.tier == "primitive")
                            .unwrap_or(&tokens[0]);

                        let message = if token.tier == "primitive" {
                            format!(
                                "Hardcoded value '{}' matches primitive token '{}' — use var({})",
                                part, token.path, token.var_name
                            )
                        } else {
                            format!(
                                "Hardcoded value '{}' matches token '{}' — use var({})",
                                part, token.path, token.var_name
                            )
                        };

                        let fix = Fix::new(
                            format!("Replace with var({})", token.var_name),
                            TextEdit::replace(
                                line_start as u32,
                                line_end as u32,
                                line.replace(part, &format!("var({})", token.var_name)),
                            ),
                        );

                        result.add_diagnostic(
                            LintDiagnostic::warn(
                                META.name,
                                message,
                                line_start as u32,
                                line_end as u32,
                            )
                            .with_help(format!(
                                "Use `var({})` for consistent theming and maintainability",
                                token.var_name
                            ))
                            .with_fix(fix),
                        );
                    }
                }
            }
        }
    }
}

/// Normalize a CSS value for comparison
fn normalize_value(value: &str) -> String {
    let v = value.trim().to_lowercase();

    // Normalize hex colors: #fff -> #ffffff
    if let Some(hex) = v.strip_prefix('#') {
        if hex.len() == 3 {
            let expanded: String = hex
                .chars()
                .flat_map(|c| std::iter::repeat_n(c, 2))
                .collect();
            return format!("#{}", expanded).into();
        }
        if hex.len() == 4 {
            // #rgba -> #rrggbbaa
            let expanded: String = hex
                .chars()
                .flat_map(|c| std::iter::repeat_n(c, 2))
                .collect();
            return format!("#{}", expanded).into();
        }
    }

    // Normalize leading zero: .5rem -> 0.5rem
    if v.starts_with('.') {
        return format!("0{}", v).into();
    }

    // Remove spaces in rgb/hsl functions
    if v.starts_with("rgb") || v.starts_with("hsl") {
        return v.replace(' ', "").into();
    }

    v.into()
}

#[cfg(test)]
mod tests {
    use super::{normalize_value, MuseaLintResult, PreferDesignTokens, PreferDesignTokensConfig};

    fn create_config() -> PreferDesignTokensConfig {
        let mut config = PreferDesignTokensConfig::default();
        config.add_token("#3b82f6", "color.primary", "primitive");
        config.add_token("#ef4444", "color.error", "primitive");
        config.add_token("0.5rem", "spacing.sm", "primitive");
        config.add_token("4px", "radius.sm", "primitive");
        config.add_token("#fff", "color.white", "primitive");
        config
    }

    #[test]
    fn test_detects_hardcoded_color() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<art title="Test" component="./Test.vue">
  <variant name="default"><Test /></variant>
</art>

<style scoped>
.test {
  background: #3b82f6;
}
</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(result.warning_count, 1, "Should detect hardcoded color");
        assert!(result.diagnostics[0].message.contains("color.primary"));
    }

    #[test]
    fn test_ignores_var_usage() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<art title="Test" component="./Test.vue">
  <variant name="default"><Test /></variant>
</art>

<style scoped>
.test {
  background: var(--color-primary);
}
</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(result.warning_count, 0, "Should not warn for var() usage");
    }

    #[test]
    fn test_detects_shorthand_value() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<art title="Test" component="./Test.vue">
  <variant name="default"><Test /></variant>
</art>

<style scoped>
.test {
  border: 1px solid #3b82f6;
}
</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(result.warning_count, 1, "Should detect color in shorthand");
    }

    #[test]
    fn test_normalizes_short_hex() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<style>
.test {
  color: #FFF;
}
</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(
            result.warning_count, 1,
            "Should normalize #FFF to #ffffff and match"
        );
    }

    #[test]
    fn test_provides_autofix() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<style>
.test {
  background: #3b82f6;
}
</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert!(result.diagnostics[0].fix.is_some(), "Should provide a fix");
    }

    #[test]
    fn test_normalize_value() {
        assert_eq!(normalize_value("#fff"), "#ffffff");
        assert_eq!(normalize_value("#FFF"), "#ffffff");
        assert_eq!(normalize_value("#3b82f6"), "#3b82f6");
        assert_eq!(normalize_value(".5rem"), "0.5rem");
        assert_eq!(normalize_value("0.5rem"), "0.5rem");
        assert_eq!(normalize_value("rgb(255, 0, 0)"), "rgb(255,0,0)");
    }

    #[test]
    fn test_no_style_block() {
        let rule = PreferDesignTokens::new(create_config());
        let source = r#"<art title="Test" component="./Test.vue">
  <variant name="default"><Test /></variant>
</art>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(result.warning_count, 0, "Should handle no style block");
    }

    #[test]
    fn test_empty_config() {
        let rule = PreferDesignTokens::new(PreferDesignTokensConfig::default());
        let source = r#"<style>.test { color: #3b82f6; }</style>"#;

        let mut result = MuseaLintResult::default();
        rule.check(source, &mut result);
        assert_eq!(result.warning_count, 0, "Empty config should not warn");
    }
}