vize_patina 0.3.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
//! Musea lint rules for Art files (*.art.vue).
//!
//! These rules validate Art files used by the Musea component gallery
//! system (vize_musea).
//!
//! Art files have a specific structure:
//! - `<art>` block with metadata (title, component, etc.)
//! - `<variant>` blocks defining component variations
//! - Optional script and style blocks
//!
//! ## Performance
//!
//! The `MuseaLinter` uses single-pass scanning with SIMD-accelerated
//! pattern matching (memchr) for optimal performance.

mod no_empty_variant;
pub mod prefer_design_tokens;
mod require_component;
mod require_title;
mod unique_variant_names;
mod valid_variant;

pub use no_empty_variant::NoEmptyVariant;
pub use prefer_design_tokens::{PreferDesignTokens, PreferDesignTokensConfig};
pub use require_component::RequireComponent;
pub use require_title::RequireTitle;
pub use unique_variant_names::UniqueVariantNames;
pub use valid_variant::ValidVariant;

use memchr::memmem;
use vize_carton::FxHashSet;

use crate::diagnostic::{LintDiagnostic, Severity};

/// Musea Art file lint result
#[derive(Debug, Clone, Default)]
pub struct MuseaLintResult {
    /// Collected diagnostics
    pub diagnostics: Vec<LintDiagnostic>,
    /// Error count
    pub error_count: usize,
    /// Warning count
    pub warning_count: usize,
}

impl MuseaLintResult {
    /// Check if there are errors
    #[inline]
    pub fn has_errors(&self) -> bool {
        self.error_count > 0
    }

    /// Check if there are any diagnostics
    #[inline]
    pub fn has_diagnostics(&self) -> bool {
        !self.diagnostics.is_empty()
    }

    /// Add a diagnostic
    #[inline]
    pub fn add_diagnostic(&mut self, diagnostic: LintDiagnostic) {
        match diagnostic.severity {
            Severity::Error => self.error_count += 1,
            Severity::Warning => self.warning_count += 1,
        }
        self.diagnostics.push(diagnostic);
    }
}

/// Musea rule metadata
pub struct MuseaRuleMeta {
    /// Rule name (e.g., "musea/require-title")
    pub name: &'static str,
    /// Human-readable description
    pub description: &'static str,
    /// Default severity
    pub default_severity: Severity,
}

/// Trait for Musea Art file lint rules
pub trait MuseaRule: Send + Sync {
    /// Get rule metadata
    fn meta(&self) -> &'static MuseaRuleMeta;

    /// Check the Art file source
    fn check(&self, source: &str, result: &mut MuseaLintResult);
}

/// High-performance Musea Art file linter using single-pass scanning
pub struct MuseaLinter {
    /// Whether to check require-title rule
    pub check_require_title: bool,
    /// Whether to check require-component rule
    pub check_require_component: bool,
    /// Whether to check valid-variant rule
    pub check_valid_variant: bool,
    /// Whether to check no-empty-variant rule
    pub check_no_empty_variant: bool,
    /// Whether to check unique-variant-names rule
    pub check_unique_variant_names: bool,
    /// Optional design token checker for prefer-design-tokens rule
    prefer_design_tokens: Option<PreferDesignTokens>,
}

impl MuseaLinter {
    /// Create a new Musea linter with all rules enabled
    #[inline]
    pub fn new() -> Self {
        Self {
            check_require_title: true,
            check_require_component: true,
            check_valid_variant: true,
            check_no_empty_variant: true,
            check_unique_variant_names: true,
            prefer_design_tokens: None,
        }
    }

    /// Set design token configuration for prefer-design-tokens rule
    #[inline]
    pub fn with_design_tokens(mut self, config: PreferDesignTokensConfig) -> Self {
        self.prefer_design_tokens = Some(PreferDesignTokens::new(config));
        self
    }

    /// Lint an Art file source using optimized single-pass scanning
    pub fn lint(&self, source: &str) -> MuseaLintResult {
        let mut result = MuseaLintResult::default();
        let bytes = source.as_bytes();

        // Phase 1: Check <art> block (single scan)
        self.check_art_block(bytes, &mut result);

        // Phase 2: Check <variant> blocks (single scan for all variant rules)
        self.check_variant_blocks(bytes, &mut result);

        // Phase 3: Check <style> blocks for hardcoded token values
        if let Some(ref token_rule) = self.prefer_design_tokens {
            token_rule.check(source, &mut result);
        }

        result
    }

    /// Check <art> block for required attributes
    #[inline]
    fn check_art_block(&self, bytes: &[u8], result: &mut MuseaLintResult) {
        // Find <art tag
        let Some(art_start) = memmem::find(bytes, b"<art") else {
            return;
        };

        // Find the end of the opening tag
        let Some(tag_end) = memchr::memchr(b'>', &bytes[art_start..]) else {
            return;
        };

        let art_tag = &bytes[art_start..art_start + tag_end];

        // Check for title attribute
        if self.check_require_title && !has_attribute(art_tag, b"title=") {
            result.add_diagnostic(
                LintDiagnostic::error(
                    "musea/require-title",
                    "Missing required 'title' attribute in <art> block",
                    art_start as u32,
                    (art_start + tag_end) as u32,
                )
                .with_help("Add a title attribute: <art title=\"Component Name\">"),
            );
        }

        // Check for component attribute
        if self.check_require_component && !has_attribute(art_tag, b"component=") {
            result.add_diagnostic(
                LintDiagnostic::warn(
                    "musea/require-component",
                    "Missing 'component' attribute in <art> block",
                    art_start as u32,
                    (art_start + tag_end) as u32,
                )
                .with_help("Add component=\"./Component.vue\""),
            );
        }
    }

    /// Check all <variant> blocks in a single pass
    fn check_variant_blocks(&self, bytes: &[u8], result: &mut MuseaLintResult) {
        let variant_finder = memmem::Finder::new(b"<variant");
        let mut search_start = 0;
        let mut seen_names: FxHashSet<&[u8]> = FxHashSet::default();

        while let Some(variant_pos) = variant_finder.find(&bytes[search_start..]) {
            let abs_pos = search_start + variant_pos;
            let remaining = &bytes[abs_pos..];

            // Find the end of the opening tag
            let Some(tag_end) = memchr::memchr(b'>', remaining) else {
                break;
            };

            let variant_tag = &remaining[..tag_end];

            // Check for self-closing tag
            let is_self_closing = tag_end > 0 && remaining[tag_end - 1] == b'/';

            // Check valid-variant: name attribute required
            let name_value = extract_name_attr_bytes(variant_tag);

            if self.check_valid_variant && name_value.is_none() {
                result.add_diagnostic(
                    LintDiagnostic::error(
                        "musea/valid-variant",
                        "Missing required 'name' attribute in <variant> block",
                        abs_pos as u32,
                        (abs_pos + tag_end) as u32,
                    )
                    .with_help("Add name=\"variant-name\" to the variant"),
                );
            }

            // Check unique-variant-names
            if let Some(name) = name_value {
                if self.check_unique_variant_names {
                    if seen_names.contains(name) {
                        result.add_diagnostic(
                            LintDiagnostic::error(
                                "musea/unique-variant-names",
                                "Duplicate variant name",
                                abs_pos as u32,
                                (abs_pos + tag_end) as u32,
                            )
                            .with_help("Use a unique name for each variant"),
                        );
                    } else {
                        seen_names.insert(name);
                    }
                }
            }

            // Check no-empty-variant
            if self.check_no_empty_variant {
                if is_self_closing {
                    result.add_diagnostic(
                        LintDiagnostic::warn(
                            "musea/no-empty-variant",
                            "Empty self-closing <variant /> block",
                            abs_pos as u32,
                            (abs_pos + tag_end + 1) as u32,
                        )
                        .with_help("Add template content inside the variant"),
                    );
                    search_start = abs_pos + tag_end + 1;
                    continue;
                }

                // Find the closing tag
                let after_open = &remaining[tag_end + 1..];
                if let Some(close_pos) = memmem::find(after_open, b"</variant>") {
                    let content = &after_open[..close_pos];
                    if is_whitespace_only(content) {
                        result.add_diagnostic(
                            LintDiagnostic::warn(
                                "musea/no-empty-variant",
                                "Empty <variant> block with no content",
                                abs_pos as u32,
                                (abs_pos + tag_end + 1 + close_pos + 10) as u32,
                            )
                            .with_help("Add template content inside the variant"),
                        );
                    }
                    search_start = abs_pos + tag_end + 1 + close_pos + 10;
                } else {
                    break;
                }
            } else {
                search_start = abs_pos + tag_end + 1;
            }
        }
    }
}

impl Default for MuseaLinter {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

/// Check if a tag has an attribute (fast byte-level check)
#[inline]
fn has_attribute(tag: &[u8], attr: &[u8]) -> bool {
    memmem::find(tag, attr).is_some()
}

/// Extract the value of the name attribute from a tag (byte-level)
#[inline]
fn extract_name_attr_bytes(tag: &[u8]) -> Option<&[u8]> {
    // Find name=" or name='
    let name_pos = memmem::find(tag, b"name=")?;
    let after_eq = &tag[name_pos + 5..];

    // Skip whitespace
    let mut i = 0;
    while i < after_eq.len() && after_eq[i].is_ascii_whitespace() {
        i += 1;
    }

    if i >= after_eq.len() {
        return None;
    }

    let quote = after_eq[i];
    if quote != b'"' && quote != b'\'' {
        return None;
    }

    let after_quote = &after_eq[i + 1..];
    let end_quote = memchr::memchr(quote, after_quote)?;

    Some(&after_quote[..end_quote])
}

/// Check if bytes contain only whitespace
#[inline]
fn is_whitespace_only(bytes: &[u8]) -> bool {
    bytes.iter().all(|b| b.is_ascii_whitespace())
}

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

    #[test]
    fn test_lint_valid_art_file() {
        let source = r#"
<art title="Button" component="./Button.vue">
  <variant name="default">
    <Button>Click me</Button>
  </variant>
</art>
"#;
        let linter = MuseaLinter::new();
        let result = linter.lint(source);
        assert!(!result.has_errors());
    }

    #[test]
    fn test_lint_missing_title() {
        let source = r#"
<art component="./Button.vue">
  <variant name="default">
    <Button>Click me</Button>
  </variant>
</art>
"#;
        let linter = MuseaLinter::new();
        let result = linter.lint(source);
        assert!(result.has_errors());
    }

    #[test]
    fn test_lint_duplicate_variant_names() {
        let source = r#"
<art title="Button" component="./Button.vue">
  <variant name="same">
    <Button>One</Button>
  </variant>
  <variant name="same">
    <Button>Two</Button>
  </variant>
</art>
"#;
        let linter = MuseaLinter::new();
        let result = linter.lint(source);
        assert!(result.has_errors());
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_lint_empty_variant() {
        let source = r#"
<art title="Button" component="./Button.vue">
  <variant name="empty"></variant>
</art>
"#;
        let linter = MuseaLinter::new();
        let result = linter.lint(source);
        assert_eq!(result.warning_count, 1);
    }

    #[test]
    fn test_extract_name_attr() {
        assert_eq!(
            extract_name_attr_bytes(b"<variant name=\"test\""),
            Some(b"test".as_slice())
        );
        assert_eq!(
            extract_name_attr_bytes(b"<variant name='test'"),
            Some(b"test".as_slice())
        );
        assert_eq!(extract_name_attr_bytes(b"<variant "), None);
    }
}