|
| 1 | +use biome_analyze::{ |
| 2 | + Ast, Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, declare_lint_rule, |
| 3 | +}; |
| 4 | +use biome_console::markup; |
| 5 | +use biome_html_syntax::{AnyVueDirective, VueModifierList}; |
| 6 | +use biome_rowan::{AstNode, TextRange}; |
| 7 | +use biome_rule_options::no_vue_invalid_v_bind::NoVueInvalidVBindOptions; |
| 8 | + |
| 9 | +declare_lint_rule! { |
| 10 | + /// Forbids `v-bind` directives with missing arguments or invalid modifiers. |
| 11 | + /// |
| 12 | + /// This rule reports v-bind directives in the following cases: |
| 13 | + /// - The directive does not have that attribute value. E.g. <div v-bind:aaa></div> |
| 14 | + /// - The directive has invalid modifiers. E.g. <div v-bind:aaa.bbb="ccc"></div> |
| 15 | + /// |
| 16 | + /// ## Examples |
| 17 | + /// |
| 18 | + /// ### Invalid |
| 19 | + /// |
| 20 | + /// ```vue,expect_diagnostic |
| 21 | + /// <Foo v-bind /> |
| 22 | + /// ``` |
| 23 | + /// |
| 24 | + /// ```vue,expect_diagnostic |
| 25 | + /// <div v-bind></div> |
| 26 | + /// ``` |
| 27 | + /// |
| 28 | + /// ### Valid |
| 29 | + /// |
| 30 | + /// ```vue |
| 31 | + /// <Foo v-bind:foo="foo" /> |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + pub NoVueInvalidVBind { |
| 35 | + version: "next", |
| 36 | + name: "noVueInvalidVBind", |
| 37 | + language: "html", |
| 38 | + recommended: true, |
| 39 | + domains: &[RuleDomain::Vue], |
| 40 | + sources: &[RuleSource::EslintVueJs("valid-v-bind").same()], |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const VALID_MODIFIERS: &[&str] = &["prop", "camel", "sync", "attr"]; |
| 45 | + |
| 46 | +pub enum ViolationKind { |
| 47 | + MissingArgument, |
| 48 | + InvalidModifier(TextRange), |
| 49 | +} |
| 50 | + |
| 51 | +impl Rule for NoVueInvalidVBind { |
| 52 | + type Query = Ast<AnyVueDirective>; |
| 53 | + type State = ViolationKind; |
| 54 | + type Signals = Option<Self::State>; |
| 55 | + type Options = NoVueInvalidVBindOptions; |
| 56 | + |
| 57 | + fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { |
| 58 | + let node = ctx.query(); |
| 59 | + match node { |
| 60 | + AnyVueDirective::VueDirective(vue_directive) => { |
| 61 | + if vue_directive.name_token().ok()?.text_trimmed() != "v-bind" { |
| 62 | + return None; |
| 63 | + } |
| 64 | + |
| 65 | + if vue_directive.arg().is_none() { |
| 66 | + return Some(ViolationKind::MissingArgument); |
| 67 | + } |
| 68 | + |
| 69 | + if let Some(invalid_range) = find_invalid_modifiers(&vue_directive.modifiers()) { |
| 70 | + return Some(ViolationKind::InvalidModifier(invalid_range)); |
| 71 | + } |
| 72 | + |
| 73 | + None |
| 74 | + } |
| 75 | + AnyVueDirective::VueVBindShorthandDirective(dir) => { |
| 76 | + // missing argument would be caught by the parser |
| 77 | + |
| 78 | + if let Some(invalid_range) = find_invalid_modifiers(&dir.modifiers()) { |
| 79 | + return Some(ViolationKind::InvalidModifier(invalid_range)); |
| 80 | + } |
| 81 | + |
| 82 | + None |
| 83 | + } |
| 84 | + _ => None, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 89 | + Some( |
| 90 | + match state { |
| 91 | + ViolationKind::MissingArgument => RuleDiagnostic::new( |
| 92 | + rule_category!(), |
| 93 | + ctx.query().range(), |
| 94 | + markup! { |
| 95 | + "The v-bind directive is missing an argument." |
| 96 | + }, |
| 97 | + ) |
| 98 | + .note(markup! { |
| 99 | + "v-bind directives require an argument to specify which attribute to bind to." |
| 100 | + }).note(markup! { |
| 101 | + "For example, use " <Emphasis>"v-bind:foo"</Emphasis> " to bind to the " <Emphasis>"foo"</Emphasis> " attribute." |
| 102 | + }), |
| 103 | + ViolationKind::InvalidModifier(invalid_range) => |
| 104 | + RuleDiagnostic::new( |
| 105 | + rule_category!(), |
| 106 | + invalid_range, |
| 107 | + markup! { |
| 108 | + "This v-bind directive has an invalid modifier." |
| 109 | + }, |
| 110 | + ) |
| 111 | + .note(markup! { |
| 112 | + "Only the following modifiers are allowed on v-bind directives: "<Emphasis>"prop"</Emphasis>", "<Emphasis>"camel"</Emphasis>", "<Emphasis>"sync"</Emphasis>", and "<Emphasis>"attr"</Emphasis>"." |
| 113 | + }).note(markup! { |
| 114 | + "Remove or correct the invalid modifier." |
| 115 | + }), |
| 116 | + } |
| 117 | + ) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +fn find_invalid_modifiers(modifiers: &VueModifierList) -> Option<TextRange> { |
| 122 | + for modifier in modifiers { |
| 123 | + if !VALID_MODIFIERS.contains(&modifier.modifier_token().ok()?.text()) { |
| 124 | + return Some(modifier.range()); |
| 125 | + } |
| 126 | + } |
| 127 | + None |
| 128 | +} |
0 commit comments