vize_patina 0.9.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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//! Lint context for rule execution.
//!
//! Uses arena allocation for high-performance memory management.

use crate::diagnostic::{HelpLevel, LintDiagnostic, Severity};
use std::borrow::Cow;
use vize_carton::directive::DirectiveSeverity;
use vize_carton::i18n::{t, t_fmt, Locale};
use vize_carton::{Allocator, CompactString, FxHashMap, FxHashSet};
use vize_croquis::Croquis;
use vize_relief::ast::SourceLocation;
use vize_relief::BindingType;

/// Represents a disabled range for a specific rule or all rules
#[derive(Debug, Clone)]
pub struct DisabledRange {
    /// Start line (1-indexed)
    pub start_line: u32,
    /// End line (1-indexed, inclusive). None means until end of file.
    pub end_line: Option<u32>,
}

/// SSR mode for linting
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SsrMode {
    /// Disabled - no SSR-specific rules
    Disabled,
    /// Enabled - warn about SSR-unfriendly code (default)
    #[default]
    Enabled,
}

/// Context for tracking element state during traversal
///
/// Uses `CompactString` for tag to avoid lifetime complications while
/// maintaining efficiency for small strings (inline storage up to 24 bytes).
#[derive(Debug, Clone)]
pub struct ElementContext {
    /// Tag name (CompactString for efficiency)
    pub tag: CompactString,
    /// Whether element has v-for directive
    pub has_v_for: bool,
    /// Whether element has v-if directive
    pub has_v_if: bool,
    /// Variables defined by v-for on this element
    pub v_for_vars: Vec<CompactString>,
}

impl ElementContext {
    /// Create a new element context
    #[inline]
    pub fn new(tag: impl Into<CompactString>) -> Self {
        Self {
            tag: tag.into(),
            has_v_for: false,
            has_v_if: false,
            v_for_vars: Vec::new(),
        }
    }

    /// Create with v-for info
    #[inline]
    pub fn with_v_for(tag: impl Into<CompactString>, vars: Vec<CompactString>) -> Self {
        Self {
            tag: tag.into(),
            has_v_for: true,
            has_v_if: false,
            v_for_vars: vars,
        }
    }
}

/// Lint context provides utilities for rules during execution.
///
/// Uses arena allocation for efficient memory management during lint traversal.
pub struct LintContext<'a> {
    /// Arena allocator for this lint session
    allocator: &'a Allocator,
    /// Source code being linted
    pub source: &'a str,
    /// Filename for diagnostics
    pub filename: &'a str,
    /// Locale for i18n (default: English)
    locale: Locale,
    /// Collected diagnostics (pre-allocated capacity)
    diagnostics: Vec<LintDiagnostic>,
    /// Current rule name (set by visitor before calling rule methods)
    pub current_rule: &'static str,
    /// Parent element stack for context (pre-allocated capacity)
    element_stack: Vec<ElementContext>,
    /// Variables in current scope (from v-for)
    scope_variables: FxHashSet<CompactString>,
    /// Cached error count for fast access
    error_count: usize,
    /// Cached warning count for fast access
    warning_count: usize,
    /// Disabled ranges for all rules
    disabled_all: Vec<DisabledRange>,
    /// Disabled ranges per rule name
    disabled_rules: FxHashMap<CompactString, Vec<DisabledRange>>,
    /// Line offsets for fast line number lookup
    line_offsets: Vec<u32>,
    /// Optional set of enabled rule names (if None, all rules are enabled)
    enabled_rules: Option<FxHashSet<String>>,
    /// Optional semantic analysis from croquis
    analysis: Option<&'a Croquis>,
    /// SSR mode for linting
    ssr_mode: SsrMode,
    /// Help display level
    help_level: HelpLevel,
    /// Lines where `@vize:expected` expects an error on the next line
    expected_error_lines: FxHashSet<u32>,
    /// Severity overrides from `@vize:level(...)` keyed by next-line number
    severity_overrides: FxHashMap<u32, DirectiveSeverity>,
}

impl<'a> LintContext<'a> {
    /// Initial capacity for diagnostics vector
    const INITIAL_DIAGNOSTICS_CAPACITY: usize = 16;
    /// Initial capacity for element stack
    const INITIAL_STACK_CAPACITY: usize = 32;

    /// Create a new lint context with arena allocator
    #[inline]
    pub fn new(allocator: &'a Allocator, source: &'a str, filename: &'a str) -> Self {
        Self::with_locale(allocator, source, filename, Locale::default())
    }

    /// Create a new lint context with specified locale
    #[inline]
    pub fn with_locale(
        allocator: &'a Allocator,
        source: &'a str,
        filename: &'a str,
        locale: Locale,
    ) -> Self {
        Self {
            allocator,
            source,
            filename,
            locale,
            diagnostics: Vec::with_capacity(Self::INITIAL_DIAGNOSTICS_CAPACITY),
            current_rule: "",
            element_stack: Vec::with_capacity(Self::INITIAL_STACK_CAPACITY),
            scope_variables: FxHashSet::default(),
            error_count: 0,
            warning_count: 0,
            disabled_all: Vec::new(),
            disabled_rules: FxHashMap::default(),
            line_offsets: Self::compute_line_offsets(source),
            enabled_rules: None,
            analysis: None,
            ssr_mode: SsrMode::default(),
            help_level: HelpLevel::default(),
            expected_error_lines: FxHashSet::default(),
            severity_overrides: FxHashMap::default(),
        }
    }

    /// Create a new lint context with semantic analysis
    #[inline]
    pub fn with_analysis(
        allocator: &'a Allocator,
        source: &'a str,
        filename: &'a str,
        analysis: &'a Croquis,
    ) -> Self {
        Self {
            allocator,
            source,
            filename,
            locale: Locale::default(),
            diagnostics: Vec::with_capacity(Self::INITIAL_DIAGNOSTICS_CAPACITY),
            current_rule: "",
            element_stack: Vec::with_capacity(Self::INITIAL_STACK_CAPACITY),
            scope_variables: FxHashSet::default(),
            error_count: 0,
            warning_count: 0,
            disabled_all: Vec::new(),
            disabled_rules: FxHashMap::default(),
            line_offsets: Self::compute_line_offsets(source),
            enabled_rules: None,
            analysis: Some(analysis),
            ssr_mode: SsrMode::default(),
            help_level: HelpLevel::default(),
            expected_error_lines: FxHashSet::default(),
            severity_overrides: FxHashMap::default(),
        }
    }

    /// Set semantic analysis
    #[inline]
    pub fn set_analysis(&mut self, analysis: &'a Croquis) {
        self.analysis = Some(analysis);
    }

    /// Get semantic analysis (if available)
    #[inline]
    pub fn analysis(&self) -> Option<&Croquis> {
        self.analysis
    }

    /// Check if semantic analysis is available
    #[inline]
    pub fn has_analysis(&self) -> bool {
        self.analysis.is_some()
    }

    /// Set SSR mode
    #[inline]
    pub fn set_ssr_mode(&mut self, mode: SsrMode) {
        self.ssr_mode = mode;
    }

    /// Get SSR mode
    #[inline]
    pub fn ssr_mode(&self) -> SsrMode {
        self.ssr_mode
    }

    /// Check if SSR mode is enabled
    #[inline]
    pub fn is_ssr_enabled(&self) -> bool {
        self.ssr_mode == SsrMode::Enabled
    }

    /// Set help display level
    #[inline]
    pub fn set_help_level(&mut self, level: HelpLevel) {
        self.help_level = level;
    }

    /// Get help display level
    #[inline]
    pub fn help_level(&self) -> HelpLevel {
        self.help_level
    }

    /// Set enabled rules filter
    ///
    /// If set to Some, only rules in the set will report diagnostics.
    /// If set to None (default), all rules are enabled.
    #[inline]
    pub fn set_enabled_rules(&mut self, enabled: Option<FxHashSet<String>>) {
        self.enabled_rules = enabled;
    }

    /// Check if a rule is enabled
    #[inline]
    pub fn is_rule_enabled(&self, rule_name: &str) -> bool {
        match &self.enabled_rules {
            Some(set) => set.contains(rule_name),
            None => true,
        }
    }

    /// Get the current locale
    #[inline]
    pub fn locale(&self) -> Locale {
        self.locale
    }

    /// Translate a message key
    #[inline]
    pub fn t(&self, key: &str) -> Cow<'static, str> {
        t(self.locale, key)
    }

    /// Translate a message key with variable substitution
    #[inline]
    pub fn t_fmt(&self, key: &str, vars: &[(&str, &str)]) -> String {
        t_fmt(self.locale, key, vars)
    }

    /// Compute line offsets for fast line number lookup
    fn compute_line_offsets(source: &str) -> Vec<u32> {
        let mut offsets = vec![0];
        for (i, c) in source.char_indices() {
            if c == '\n' {
                offsets.push((i + 1) as u32);
            }
        }
        offsets
    }

    /// Get line number (1-indexed) from byte offset
    #[inline]
    pub fn offset_to_line(&self, offset: u32) -> u32 {
        match self.line_offsets.binary_search(&offset) {
            Ok(line) => (line + 1) as u32,
            Err(line) => line as u32,
        }
    }

    /// Get the allocator
    #[inline]
    pub fn allocator(&self) -> &'a Allocator {
        self.allocator
    }

    /// Allocate a string in the arena
    #[inline]
    pub fn alloc_str(&self, s: &str) -> &'a str {
        self.allocator.alloc_str(s)
    }

    /// Report a lint diagnostic
    #[inline]
    pub fn report(&mut self, mut diagnostic: LintDiagnostic) {
        // Check if this rule is enabled
        if !self.is_rule_enabled(diagnostic.rule_name) {
            return;
        }

        // Check if this diagnostic is disabled via comments
        let line = self.offset_to_line(diagnostic.start);
        if self.is_disabled_at(diagnostic.rule_name, line) {
            return;
        }

        // Check if this line has an @vize:expected directive
        if self.expected_error_lines.remove(&line) {
            // Error was expected — suppress it
            return;
        }

        // Apply @vize:level severity override
        if let Some(override_severity) = self.severity_overrides.remove(&line) {
            match override_severity {
                DirectiveSeverity::Off => return,
                DirectiveSeverity::Warn => diagnostic.severity = Severity::Warning,
                DirectiveSeverity::Error => diagnostic.severity = Severity::Error,
            }
        }

        match diagnostic.severity {
            Severity::Error => self.error_count += 1,
            Severity::Warning => self.warning_count += 1,
        }
        self.diagnostics.push(diagnostic);
    }

    /// Check if a rule is disabled at a specific line
    #[inline]
    fn is_disabled_at(&self, rule_name: &str, line: u32) -> bool {
        // Check global disables
        for range in &self.disabled_all {
            if line >= range.start_line {
                if let Some(end) = range.end_line {
                    if line <= end {
                        return true;
                    }
                } else {
                    return true;
                }
            }
        }

        // Check rule-specific disables
        if let Some(ranges) = self.disabled_rules.get(rule_name) {
            for range in ranges {
                if line >= range.start_line {
                    if let Some(end) = range.end_line {
                        if line <= end {
                            return true;
                        }
                    } else {
                        return true;
                    }
                }
            }
        }

        false
    }

    /// Disable all rules starting from a line
    pub fn disable_all(&mut self, start_line: u32, end_line: Option<u32>) {
        self.disabled_all.push(DisabledRange {
            start_line,
            end_line,
        });
    }

    /// Disable specific rules starting from a line
    pub fn disable_rules(&mut self, rules: &[&str], start_line: u32, end_line: Option<u32>) {
        for rule in rules {
            let range = DisabledRange {
                start_line,
                end_line,
            };
            self.disabled_rules
                .entry(CompactString::from(*rule))
                .or_default()
                .push(range);
        }
    }

    /// Disable all rules for the next line only
    pub fn disable_next_line(&mut self, current_line: u32) {
        self.disable_all(current_line + 1, Some(current_line + 1));
    }

    /// Disable specific rules for the next line only
    pub fn disable_rules_next_line(&mut self, rules: &[&str], current_line: u32) {
        self.disable_rules(rules, current_line + 1, Some(current_line + 1));
    }

    /// Begin a `@vize:ignore-start` region (disables all rules from this line)
    pub fn push_ignore_region(&mut self, line: u32) {
        self.disable_all(line, None);
    }

    /// End a `@vize:ignore-end` region (closes the most recent open ignore region)
    pub fn pop_ignore_region(&mut self, line: u32) {
        // Find the last disabled_all range with end_line = None and close it
        for range in self.disabled_all.iter_mut().rev() {
            if range.end_line.is_none() {
                range.end_line = Some(line);
                return;
            }
        }
    }

    /// Register that `@vize:expected` expects an error on the next line
    pub fn expect_error_next_line(&mut self, current_line: u32) {
        self.expected_error_lines.insert(current_line + 1);
    }

    /// Set a severity override for diagnostics on the next line
    pub fn set_severity_override_next_line(
        &mut self,
        current_line: u32,
        severity: DirectiveSeverity,
    ) {
        self.severity_overrides.insert(current_line + 1, severity);
    }

    /// Report an error at a location
    #[inline]
    pub fn error(&mut self, message: impl Into<CompactString>, loc: &SourceLocation) {
        self.report(LintDiagnostic::error(
            self.current_rule,
            message,
            loc.start.offset,
            loc.end.offset,
        ));
    }

    /// Report a warning at a location
    #[inline]
    pub fn warn(&mut self, message: impl Into<CompactString>, loc: &SourceLocation) {
        self.report(LintDiagnostic::warn(
            self.current_rule,
            message,
            loc.start.offset,
            loc.end.offset,
        ));
    }

    /// Report an error with help message
    #[inline]
    pub fn error_with_help(
        &mut self,
        message: impl Into<CompactString>,
        loc: &SourceLocation,
        help: impl Into<CompactString>,
    ) {
        let mut diag =
            LintDiagnostic::error(self.current_rule, message, loc.start.offset, loc.end.offset);
        let help_str: CompactString = help.into();
        if let Some(processed) = self.help_level.process(help_str.as_str()) {
            diag = diag.with_help(processed);
        }
        self.report(diag);
    }

    /// Report a warning with help message
    #[inline]
    pub fn warn_with_help(
        &mut self,
        message: impl Into<CompactString>,
        loc: &SourceLocation,
        help: impl Into<CompactString>,
    ) {
        let mut diag =
            LintDiagnostic::warn(self.current_rule, message, loc.start.offset, loc.end.offset);
        let help_str: CompactString = help.into();
        if let Some(processed) = self.help_level.process(help_str.as_str()) {
            diag = diag.with_help(processed);
        }
        self.report(diag);
    }

    /// Report a diagnostic with related label
    #[inline]
    pub fn error_with_label(
        &mut self,
        message: impl Into<CompactString>,
        loc: &SourceLocation,
        label_message: impl Into<CompactString>,
        label_loc: &SourceLocation,
    ) {
        self.report(
            LintDiagnostic::error(self.current_rule, message, loc.start.offset, loc.end.offset)
                .with_label(label_message, label_loc.start.offset, label_loc.end.offset),
        );
    }

    /// Get collected diagnostics
    #[inline]
    pub fn into_diagnostics(self) -> Vec<LintDiagnostic> {
        self.diagnostics
    }

    /// Get reference to collected diagnostics
    #[inline]
    pub fn diagnostics(&self) -> &[LintDiagnostic] {
        &self.diagnostics
    }

    /// Push an element onto the context stack
    #[inline]
    pub fn push_element(&mut self, ctx: ElementContext) {
        // Add v-for vars to scope
        for var in &ctx.v_for_vars {
            self.scope_variables.insert(var.clone());
        }
        self.element_stack.push(ctx);
    }

    /// Pop an element from the context stack
    #[inline]
    pub fn pop_element(&mut self) -> Option<ElementContext> {
        if let Some(ctx) = self.element_stack.pop() {
            // Remove v-for vars from scope
            for var in &ctx.v_for_vars {
                self.scope_variables.remove(var);
            }
            Some(ctx)
        } else {
            None
        }
    }

    /// Check if inside a v-for loop
    #[inline]
    pub fn is_in_v_for(&self) -> bool {
        self.element_stack.iter().any(|e| e.has_v_for)
    }

    /// Get all v-for variables in current scope
    #[inline]
    pub fn v_for_vars(&self) -> impl Iterator<Item = &str> {
        self.element_stack
            .iter()
            .flat_map(|e| e.v_for_vars.iter().map(|s| s.as_str()))
    }

    /// Check if a variable is defined by a parent v-for
    #[inline]
    pub fn is_v_for_var(&self, name: &str) -> bool {
        self.scope_variables.contains(name)
    }

    /// Check if a variable is defined by a PARENT v-for (excluding current element)
    ///
    /// This is useful for shadow detection where we want to check if a variable
    /// in the current v-for shadows a variable from an outer scope.
    #[inline]
    pub fn is_parent_v_for_var(&self, name: &str) -> bool {
        // Check all elements except the last one (current element)
        if self.element_stack.len() < 2 {
            return false;
        }
        for elem in self.element_stack.iter().take(self.element_stack.len() - 1) {
            for var in &elem.v_for_vars {
                if var.as_str() == name {
                    return true;
                }
            }
        }
        false
    }

    /// Get current element context (top of stack)
    #[inline]
    pub fn current_element(&self) -> Option<&ElementContext> {
        self.element_stack.last()
    }

    /// Get parent element context
    #[inline]
    pub fn parent_element(&self) -> Option<&ElementContext> {
        if self.element_stack.len() >= 2 {
            self.element_stack.get(self.element_stack.len() - 2)
        } else {
            None
        }
    }

    /// Check if any ancestor element matches the given predicate
    ///
    /// Searches the element stack from bottom to top (excluding the current element).
    /// Useful for detecting nested interactive elements or content model violations.
    #[inline]
    pub fn has_ancestor(&self, predicate: impl Fn(&ElementContext) -> bool) -> bool {
        if self.element_stack.len() < 2 {
            return false;
        }
        self.element_stack
            .iter()
            .take(self.element_stack.len() - 1)
            .any(predicate)
    }

    /// Get the error count (cached, O(1))
    #[inline]
    pub fn error_count(&self) -> usize {
        self.error_count
    }

    /// Get the warning count (cached, O(1))
    #[inline]
    pub fn warning_count(&self) -> usize {
        self.warning_count
    }

    // =========================================================================
    // Semantic Analysis Helpers
    // =========================================================================
    // These methods leverage croquis Croquis when available.
    // They provide fallback behavior when analysis is not available.

    /// Check if a variable is defined (in any scope or script binding)
    ///
    /// Uses semantic analysis if available, otherwise falls back to
    /// v-for variable tracking only.
    #[inline]
    pub fn is_variable_defined(&self, name: &str) -> bool {
        // First check template-local scope (v-for variables)
        if self.is_v_for_var(name) {
            return true;
        }

        // Then check semantic analysis if available
        if let Some(analysis) = &self.analysis {
            return analysis.is_defined(name);
        }

        false
    }

    /// Get the binding type for a variable
    ///
    /// Returns None if analysis is not available or variable is not found.
    #[inline]
    pub fn get_binding_type(&self, name: &str) -> Option<BindingType> {
        self.analysis.and_then(|a| a.get_binding_type(name))
    }

    /// Check if a name refers to a script-level binding
    #[inline]
    pub fn has_script_binding(&self, name: &str) -> bool {
        self.analysis
            .map(|a| a.bindings.contains(name))
            .unwrap_or(false)
    }

    /// Check if a component is registered or imported
    #[inline]
    pub fn is_component_registered(&self, name: &str) -> bool {
        self.analysis
            .map(|a| a.is_component_registered(name))
            .unwrap_or(false)
    }

    /// Check if a prop is defined via defineProps
    #[inline]
    pub fn has_prop(&self, name: &str) -> bool {
        self.analysis
            .map(|a| a.macros.props().iter().any(|p| p.name.as_str() == name))
            .unwrap_or(false)
    }

    /// Check if an emit is defined via defineEmits
    #[inline]
    pub fn has_emit(&self, name: &str) -> bool {
        self.analysis
            .map(|a| a.macros.emits().iter().any(|e| e.name.as_str() == name))
            .unwrap_or(false)
    }

    /// Check if a model is defined via defineModel
    #[inline]
    pub fn has_model(&self, name: &str) -> bool {
        self.analysis
            .map(|a| a.macros.models().iter().any(|m| m.name.as_str() == name))
            .unwrap_or(false)
    }

    /// Check if the component uses async setup (top-level await)
    #[inline]
    pub fn is_async_setup(&self) -> bool {
        self.analysis.map(|a| a.is_async()).unwrap_or(false)
    }

    /// Get all props defined in the component
    pub fn get_props(&self) -> Vec<&str> {
        self.analysis
            .map(|a| a.macros.props().iter().map(|p| p.name.as_str()).collect())
            .unwrap_or_default()
    }

    /// Get all emits defined in the component
    pub fn get_emits(&self) -> Vec<&str> {
        self.analysis
            .map(|a| a.macros.emits().iter().map(|e| e.name.as_str()).collect())
            .unwrap_or_default()
    }
}