vize_patina 0.73.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
//! a11y/aria-role
//!
//! Elements with ARIA roles must use a valid, non-abstract ARIA role.
//!
//! This rule enforces that `role` attributes contain only valid ARIA roles
//! from the WAI-ARIA specification. Abstract roles are not allowed as they
//! are meant to be used as base concepts, not directly on elements.
//!
//! Incorrect roles can prevent assistive technologies from conveying the
//! intended meaning to users.
//!
//! Based on eslint-plugin-jsx-a11y aria-role rule.

use crate::context::LintContext;
use crate::diagnostic::Severity;
use crate::rule::{Rule, RuleCategory, RuleMeta};
use vize_relief::ast::{ElementNode, PropNode, SourceLocation};

static META: RuleMeta = RuleMeta {
    name: "a11y/aria-role",
    description: "Elements with ARIA roles must use a valid, non-abstract ARIA role",
    category: RuleCategory::Accessibility,
    fixable: false,
    default_severity: Severity::Error,
};

/// Valid ARIA roles from WAI-ARIA specification.
/// https://www.w3.org/TR/wai-aria/#role_definitions
///
/// This list includes:
/// - Widget roles
/// - Composite widget roles
/// - Document structure roles
/// - Landmark roles
/// - Live region roles
/// - Window roles
const VALID_ARIA_ROLES: &[&str] = &[
    // === Widget Roles ===
    // https://www.w3.org/TR/wai-aria/#widget_roles
    "alert",
    "alertdialog",
    "button",
    "checkbox",
    "dialog",
    "gridcell",
    "link",
    "log",
    "marquee",
    "menuitem",
    "menuitemcheckbox",
    "menuitemradio",
    "option",
    "progressbar",
    "radio",
    "scrollbar",
    "searchbox",
    "slider",
    "spinbutton",
    "status",
    "switch",
    "tab",
    "tabpanel",
    "textbox",
    "timer",
    "tooltip",
    "treeitem",
    // === Composite Widget Roles ===
    // https://www.w3.org/TR/wai-aria/#composite_roles
    "combobox",
    "grid",
    "listbox",
    "menu",
    "menubar",
    "radiogroup",
    "tablist",
    "tree",
    "treegrid",
    // === Document Structure Roles ===
    // https://www.w3.org/TR/wai-aria/#document_structure_roles
    "application",
    "article",
    "blockquote",
    "caption",
    "cell",
    "code",
    "columnheader",
    "definition",
    "deletion",
    "directory", // Deprecated but still valid
    "document",
    "emphasis",
    "feed",
    "figure",
    "generic",
    "group",
    "heading",
    "img",
    "insertion",
    "list",
    "listitem",
    "math",
    "meter",
    "none",
    "note",
    "paragraph",
    "presentation",
    "row",
    "rowgroup",
    "rowheader",
    "separator",
    "strong",
    "subscript",
    "superscript",
    "table",
    "term",
    "time",
    "toolbar",
    // === Landmark Roles ===
    // https://www.w3.org/TR/wai-aria/#landmark_roles
    "banner",
    "complementary",
    "contentinfo",
    "form",
    "main",
    "navigation",
    "region",
    "search",
    // === Live Region Roles ===
    // (alert, log, marquee, status, timer already listed above)
    // === Window Roles ===
    // (alertdialog, dialog already listed above)
    // === ARIA 1.3 Additions ===
    "comment",
    "mark",
    "suggestion",
];

/// Abstract ARIA roles that should NOT be used directly.
/// https://www.w3.org/TR/wai-aria/#abstract_roles
///
/// These are base concepts that other roles inherit from.
/// They must not be used by web authors.
const ABSTRACT_ARIA_ROLES: &[&str] = &[
    "command",
    "composite",
    "input",
    "landmark",
    "range",
    "roletype",
    "section",
    "sectionhead",
    "select",
    "structure",
    "widget",
    "window",
];

/// Elements with ARIA roles must use a valid, non-abstract ARIA role
#[derive(Default)]
pub struct AriaRole {
    /// Whether to ignore non-DOM elements (custom components)
    pub ignore_non_dom: bool,
}

impl AriaRole {
    /// Check if a role is a valid ARIA role
    #[inline]
    fn is_valid_role(role: &str) -> bool {
        VALID_ARIA_ROLES.contains(&role)
    }

    /// Check if a role is an abstract ARIA role
    #[inline]
    fn is_abstract_role(role: &str) -> bool {
        ABSTRACT_ARIA_ROLES.contains(&role)
    }

    /// Check if an element is a DOM element (not a custom component)
    #[inline]
    fn is_dom_element(tag: &str) -> bool {
        // Custom components typically start with uppercase or contain hyphens
        // DOM elements are lowercase without hyphens (except for custom elements)
        let first_char = tag.chars().next().unwrap_or('a');
        first_char.is_ascii_lowercase() && !tag.contains('-')
    }

    /// Find a similar valid role for suggestions
    fn find_similar(invalid: &str) -> Option<&'static str> {
        // Common mistakes mapping
        let typo_fixes: &[(&str, &str)] = &[
            ("date", "textbox"),
            ("datepicker", "textbox"),
            ("dropdown", "listbox"),
            ("input", "textbox"),
            ("item", "listitem"),
            ("listitem", "listitem"), // Already valid but catch typos
            ("modal", "dialog"),
            ("popup", "dialog"),
            ("text", "textbox"),
            ("titlebar", "heading"),
        ];

        // Check direct typo mapping first
        let invalid_lower = invalid.to_ascii_lowercase();
        for (typo, fix) in typo_fixes {
            if *typo == invalid_lower {
                return Some(fix);
            }
        }

        // Levenshtein-like similarity check for other cases
        VALID_ARIA_ROLES
            .iter()
            .find(|valid| {
                let valid_lower = valid.to_ascii_lowercase();
                // Simple similarity: same length +/- 2 and most chars match
                let len_diff = (invalid_lower.len() as i32 - valid_lower.len() as i32).abs();
                if len_diff > 2 {
                    return false;
                }
                // Count matching chars
                let matches = invalid_lower
                    .chars()
                    .zip(valid_lower.chars())
                    .filter(|(a, b)| a == b)
                    .count();
                matches >= invalid_lower.len().saturating_sub(2)
            })
            .copied()
    }
}

impl Rule for AriaRole {
    fn meta(&self) -> &'static RuleMeta {
        &META
    }

    fn enter_element<'a>(&self, ctx: &mut LintContext<'a>, element: &ElementNode<'a>) {
        // Skip non-DOM elements if configured
        if self.ignore_non_dom && !Self::is_dom_element(element.tag.as_str()) {
            return;
        }

        for prop in &element.props {
            match prop {
                PropNode::Attribute(attr) => {
                    if attr.name.as_str() == "role" {
                        if let Some(value) = &attr.value {
                            self.check_role(ctx, value.content.as_str(), &attr.loc);
                        }
                    }
                }
                PropNode::Directive(dir) => {
                    // Check :role or v-bind:role with static value
                    if dir.name == "bind" {
                        if let Some(vize_relief::ast::ExpressionNode::Simple(arg)) = &dir.arg {
                            if arg.content.as_str() == "role" {
                                // For dynamic roles, we can only check if it's a static string
                                if let Some(vize_relief::ast::ExpressionNode::Simple(expr)) =
                                    &dir.exp
                                {
                                    let content = expr.content.as_str().trim();
                                    // Check if it's a string literal like "'button'" or "\"button\""
                                    if (content.starts_with('\'') && content.ends_with('\''))
                                        || (content.starts_with('"') && content.ends_with('"'))
                                    {
                                        let role = &content[1..content.len() - 1];
                                        self.check_role(ctx, role, &dir.loc);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

impl AriaRole {
    fn check_role(&self, ctx: &mut LintContext<'_>, role: &str, loc: &SourceLocation) {
        // Handle multiple roles (space-separated)
        for single_role in role.split_whitespace() {
            let role_lower = single_role.to_ascii_lowercase();

            // Check for abstract role
            if Self::is_abstract_role(&role_lower) {
                let message =
                    ctx.t_fmt("a11y/aria-role.message_abstract", &[("role", single_role)]);
                ctx.error_with_help(message, loc, ctx.t("a11y/aria-role.help_abstract"));
                continue;
            }

            // Check for invalid role
            if !Self::is_valid_role(&role_lower) {
                let message = ctx.t_fmt("a11y/aria-role.message", &[("role", single_role)]);

                if let Some(suggestion) = Self::find_similar(&role_lower) {
                    let help = ctx.t_fmt(
                        "a11y/aria-role.help_suggestion",
                        &[("invalid", single_role), ("valid", suggestion)],
                    );
                    ctx.error_with_help(message.clone(), loc, help);
                } else {
                    ctx.error_with_help(message, loc, ctx.t("a11y/aria-role.help"));
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::AriaRole;
    use crate::linter::Linter;
    use crate::rule::RuleRegistry;

    fn create_linter() -> Linter {
        let mut registry = RuleRegistry::new();
        registry.register(Box::new(AriaRole::default()));
        Linter::with_registry(registry)
    }

    #[test]
    fn test_valid_button_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div role="button"></div>"#, "test.vue");
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_valid_navigation_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<nav role="navigation"></nav>"#, "test.vue");
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_valid_dialog_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div role="dialog"></div>"#, "test.vue");
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_valid_multiple_roles() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div role="img presentation"></div>"#, "test.vue");
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_valid_dynamic_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div :role="role"></div>"#, "test.vue");
        // Dynamic roles with variable values are not checked
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_valid_no_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div class="test"></div>"#, "test.vue");
        assert_eq!(result.error_count, 0);
    }

    #[test]
    fn test_invalid_datepicker_role() {
        let linter = create_linter();
        // "datepicker" is not an ARIA role
        let result = linter.lint_template(r#"<div role="datepicker"></div>"#, "test.vue");
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_invalid_abstract_range_role() {
        let linter = create_linter();
        // "range" is an abstract ARIA role
        let result = linter.lint_template(r#"<div role="range"></div>"#, "test.vue");
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_invalid_abstract_input_role() {
        let linter = create_linter();
        // "input" is an abstract ARIA role
        let result = linter.lint_template(r#"<div role="input"></div>"#, "test.vue");
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_invalid_abstract_widget_role() {
        let linter = create_linter();
        // "widget" is an abstract ARIA role
        let result = linter.lint_template(r#"<div role="widget"></div>"#, "test.vue");
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_invalid_made_up_role() {
        let linter = create_linter();
        let result = linter.lint_template(r#"<div role="foobar"></div>"#, "test.vue");
        assert_eq!(result.error_count, 1);
    }

    #[test]
    fn test_is_valid_role() {
        assert!(AriaRole::is_valid_role("button"));
        assert!(AriaRole::is_valid_role("dialog"));
        assert!(AriaRole::is_valid_role("navigation"));
        assert!(!AriaRole::is_valid_role("datepicker"));
        assert!(!AriaRole::is_valid_role("range")); // Abstract role
    }

    #[test]
    fn test_is_abstract_role() {
        assert!(AriaRole::is_abstract_role("range"));
        assert!(AriaRole::is_abstract_role("widget"));
        assert!(AriaRole::is_abstract_role("composite"));
        assert!(!AriaRole::is_abstract_role("button"));
        assert!(!AriaRole::is_abstract_role("dialog"));
    }

    #[test]
    fn test_is_dom_element() {
        assert!(AriaRole::is_dom_element("div"));
        assert!(AriaRole::is_dom_element("button"));
        assert!(AriaRole::is_dom_element("nav"));
        assert!(!AriaRole::is_dom_element("MyComponent"));
        assert!(!AriaRole::is_dom_element("custom-element"));
    }

    #[test]
    fn test_find_similar() {
        assert_eq!(AriaRole::find_similar("modal"), Some("dialog"));
        assert_eq!(AriaRole::find_similar("datepicker"), Some("textbox"));
        assert_eq!(AriaRole::find_similar("dropdown"), Some("listbox"));
    }
}