|
| 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::{AnyHtmlAttributeInitializer, VueDirective, inner_string_text}; |
| 6 | +use biome_rowan::{AstNode, AstNodeList, TextRange}; |
| 7 | +use biome_rule_options::use_vue_valid_v_text::UseVueValidVTextOptions; |
| 8 | + |
| 9 | +declare_lint_rule! { |
| 10 | + /// Enforce valid `v-text` Vue directives. |
| 11 | + /// |
| 12 | + /// This rule reports `v-text` directives in the following cases: |
| 13 | + /// - The directive has an argument. E.g. `<div v-text:aaa></div>` |
| 14 | + /// - The directive has any modifiers. E.g. `<div v-text.bbb></div>` |
| 15 | + /// - The directive does not have a value. E.g. `<div v-text></div>` |
| 16 | + /// |
| 17 | + /// ## Examples |
| 18 | + /// |
| 19 | + /// ### Invalid |
| 20 | + /// |
| 21 | + /// ```vue,expect_diagnostic |
| 22 | + /// <div v-text /> |
| 23 | + /// ``` |
| 24 | + /// |
| 25 | + /// ```vue,expect_diagnostic |
| 26 | + /// <div v-text:aaa="foo"></div> |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// ```vue,expect_diagnostic |
| 30 | + /// <div v-text.bbb="foo"></div> |
| 31 | + /// ``` |
| 32 | + /// |
| 33 | + /// ### Valid |
| 34 | + /// |
| 35 | + /// ```vue |
| 36 | + /// <div v-text="foo" /> |
| 37 | + /// ``` |
| 38 | + /// |
| 39 | + pub UseVueValidVText { |
| 40 | + version: "next", |
| 41 | + name: "useVueValidVText", |
| 42 | + language: "html", |
| 43 | + recommended: true, |
| 44 | + domains: &[RuleDomain::Vue], |
| 45 | + sources: &[RuleSource::EslintVueJs("valid-v-text").same()], |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub enum ViolationKind { |
| 50 | + UnexpectedArgument(TextRange), |
| 51 | + UnexpectedModifier(TextRange), |
| 52 | + MissingValue, |
| 53 | +} |
| 54 | + |
| 55 | +impl Rule for UseVueValidVText { |
| 56 | + type Query = Ast<VueDirective>; |
| 57 | + type State = ViolationKind; |
| 58 | + type Signals = Option<Self::State>; |
| 59 | + type Options = UseVueValidVTextOptions; |
| 60 | + |
| 61 | + fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { |
| 62 | + let vue_directive = ctx.query(); |
| 63 | + if vue_directive.name_token().ok()?.text_trimmed() != "v-text" { |
| 64 | + return None; |
| 65 | + } |
| 66 | + |
| 67 | + if let Some(arg) = vue_directive.arg() { |
| 68 | + return Some(ViolationKind::UnexpectedArgument(arg.range())); |
| 69 | + } |
| 70 | + |
| 71 | + if !vue_directive.modifiers().is_empty() { |
| 72 | + let first_modifier = vue_directive.modifiers().iter().next()?; |
| 73 | + return Some(ViolationKind::UnexpectedModifier(first_modifier.range())); |
| 74 | + } |
| 75 | + |
| 76 | + if let Some(initializer) = vue_directive.initializer() |
| 77 | + && let Ok(AnyHtmlAttributeInitializer::HtmlString(html_string)) = initializer.value() |
| 78 | + && let Ok(token) = html_string.value_token() |
| 79 | + { |
| 80 | + if inner_string_text(&token).trim().is_empty() { |
| 81 | + Some(ViolationKind::MissingValue) |
| 82 | + } else { |
| 83 | + None |
| 84 | + } |
| 85 | + } else { |
| 86 | + Some(ViolationKind::MissingValue) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 91 | + Some( |
| 92 | + match state { |
| 93 | + ViolationKind::UnexpectedArgument(range) => RuleDiagnostic::new( |
| 94 | + rule_category!(), |
| 95 | + range, |
| 96 | + markup! { |
| 97 | + "The v-text directive does not accept an argument." |
| 98 | + }, |
| 99 | + ) |
| 100 | + .note(markup! { |
| 101 | + "v-text directives should be used without arguments, like " <Emphasis>"v-text=\"content\""</Emphasis>"." |
| 102 | + }), |
| 103 | + ViolationKind::UnexpectedModifier(range) => RuleDiagnostic::new( |
| 104 | + rule_category!(), |
| 105 | + range, |
| 106 | + markup! { |
| 107 | + "The v-text directive does not support modifiers." |
| 108 | + }, |
| 109 | + ) |
| 110 | + .note(markup! { |
| 111 | + "v-text directives do not support any modifiers. Remove the modifier." |
| 112 | + }), |
| 113 | + ViolationKind::MissingValue => RuleDiagnostic::new( |
| 114 | + rule_category!(), |
| 115 | + ctx.query().range(), |
| 116 | + markup! { |
| 117 | + "The v-text directive is missing a value." |
| 118 | + }, |
| 119 | + ) |
| 120 | + .note(markup! { |
| 121 | + "v-text directives require a value containing the text content to render." |
| 122 | + }).note(markup! { |
| 123 | + "For example, use " <Emphasis>"v-text=\"foo\""</Emphasis> " to render the content of the " <Emphasis>"foo"</Emphasis> " variable." |
| 124 | + }), |
| 125 | + } |
| 126 | + ) |
| 127 | + } |
| 128 | +} |
0 commit comments