|
| 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_factory::make; |
| 6 | +use biome_html_syntax::AnyVueDirective; |
| 7 | +use biome_rowan::AstNode; |
| 8 | +use biome_rule_options::use_vue_consistent_v_bind_style::{ |
| 9 | + UseVueConsistentVBindStyleOptions, VueDirectiveStyle, |
| 10 | +}; |
| 11 | + |
| 12 | +declare_lint_rule! { |
| 13 | + /// Enforce a consistent style for `v-bind` in Vue templates. |
| 14 | + /// |
| 15 | + /// ## Examples |
| 16 | + /// |
| 17 | + /// ### Invalid |
| 18 | + /// |
| 19 | + /// ```vue,expect_diagnostic |
| 20 | + /// <div v-bind:foo="bar" /> |
| 21 | + /// ``` |
| 22 | + /// |
| 23 | + /// ### Valid |
| 24 | + /// |
| 25 | + /// ```vue |
| 26 | + /// <div :foo="bar" /> |
| 27 | + /// ``` |
| 28 | + /// |
| 29 | + /// ## Options |
| 30 | + /// |
| 31 | + /// ### `style` |
| 32 | + /// |
| 33 | + /// Configures the preferred directive style. Default: `"shorthand"`. |
| 34 | + /// |
| 35 | + /// ```json,options |
| 36 | + /// { |
| 37 | + /// "options": { |
| 38 | + /// "style": "longhand" |
| 39 | + /// } |
| 40 | + /// } |
| 41 | + /// ``` |
| 42 | + /// |
| 43 | + /// #### Invalid |
| 44 | + /// |
| 45 | + /// ```vue,expect_diagnostic,use_options |
| 46 | + /// <div :foo="bar" /> |
| 47 | + /// ``` |
| 48 | + /// |
| 49 | + /// #### Valid |
| 50 | + /// |
| 51 | + /// ```vue,use_options |
| 52 | + /// <div v-bind:foo="bar" /> |
| 53 | + /// ``` |
| 54 | + /// |
| 55 | + pub UseVueConsistentVBindStyle { |
| 56 | + version: "next", |
| 57 | + name: "useVueConsistentVBindStyle", |
| 58 | + language: "html", |
| 59 | + recommended: true, |
| 60 | + domains: &[RuleDomain::Vue], |
| 61 | + sources: &[RuleSource::EslintVueJs("v-bind-style").same()], |
| 62 | + fix_kind: biome_analyze::FixKind::Unsafe, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Rule for UseVueConsistentVBindStyle { |
| 67 | + type Query = Ast<AnyVueDirective>; |
| 68 | + type State = AnyVueDirective; |
| 69 | + type Signals = Option<Self::State>; |
| 70 | + type Options = UseVueConsistentVBindStyleOptions; |
| 71 | + |
| 72 | + fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { |
| 73 | + let node = ctx.query(); |
| 74 | + let style = ctx.options().style(); |
| 75 | + match node { |
| 76 | + AnyVueDirective::VueDirective(dir) => { |
| 77 | + // Only v-bind normal form |
| 78 | + if dir.name_token().ok()?.text_trimmed() != "v-bind" { |
| 79 | + return None; |
| 80 | + } |
| 81 | + // If prefer shorthand, normal form is invalid |
| 82 | + if style == VueDirectiveStyle::Shorthand { |
| 83 | + return Some(node.clone()); |
| 84 | + } |
| 85 | + None |
| 86 | + } |
| 87 | + AnyVueDirective::VueVBindShorthandDirective(_) => { |
| 88 | + // If prefer longhand, shorthand is invalid |
| 89 | + if style == VueDirectiveStyle::Longhand { |
| 90 | + return Some(node.clone()); |
| 91 | + } |
| 92 | + None |
| 93 | + } |
| 94 | + _ => None, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 99 | + let prefer = ctx.options().style(); |
| 100 | + let message = match (state, prefer) { |
| 101 | + (AnyVueDirective::VueDirective(_), VueDirectiveStyle::Shorthand) => { |
| 102 | + markup! { "Use shorthand ':' syntax instead of v-bind." } |
| 103 | + } |
| 104 | + (AnyVueDirective::VueVBindShorthandDirective(_), VueDirectiveStyle::Longhand) => { |
| 105 | + markup! { "Use longhand 'v-bind' syntax instead of ':'." } |
| 106 | + } |
| 107 | + _ => { |
| 108 | + // should be unreachable, but just in case |
| 109 | + debug_assert!( |
| 110 | + false, |
| 111 | + "Diagnostic should only be created for invalid states." |
| 112 | + ); |
| 113 | + return None; |
| 114 | + } |
| 115 | + }; |
| 116 | + let note = match (state, prefer) { |
| 117 | + (AnyVueDirective::VueDirective(_), VueDirectiveStyle::Shorthand) => { |
| 118 | + markup! { "This project prefers to use shorthand syntax for v-bind." } |
| 119 | + } |
| 120 | + (AnyVueDirective::VueVBindShorthandDirective(_), VueDirectiveStyle::Longhand) => { |
| 121 | + markup! { "This project prefers to use longhand syntax for v-bind." } |
| 122 | + } |
| 123 | + _ => { |
| 124 | + // should be unreachable, but just in case |
| 125 | + debug_assert!( |
| 126 | + false, |
| 127 | + "Diagnostic should only be created for invalid states." |
| 128 | + ); |
| 129 | + return None; |
| 130 | + } |
| 131 | + }; |
| 132 | + Some(RuleDiagnostic::new(rule_category!(), state.range(), message).note(note)) |
| 133 | + } |
| 134 | + |
| 135 | + fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<crate::HtmlRuleAction> { |
| 136 | + let prefer = ctx.options().style(); |
| 137 | + let mut mutation = biome_rowan::BatchMutationExt::begin(ctx.root()); |
| 138 | + match (state, prefer) { |
| 139 | + // Convert longhand v-bind:prop to :prop |
| 140 | + (AnyVueDirective::VueDirective(dir), VueDirectiveStyle::Shorthand) => { |
| 141 | + let arg = dir.arg()?; |
| 142 | + let mut builder = make::vue_v_bind_shorthand_directive(arg, dir.modifiers()); |
| 143 | + if let Some(init) = dir.initializer() { |
| 144 | + builder = builder.with_initializer(init); |
| 145 | + } |
| 146 | + let new_node = builder.build(); |
| 147 | + mutation.replace_node( |
| 148 | + AnyVueDirective::VueDirective(dir.clone()), |
| 149 | + AnyVueDirective::VueVBindShorthandDirective(new_node), |
| 150 | + ); |
| 151 | + Some(biome_analyze::RuleAction::new( |
| 152 | + ctx.metadata().action_category(ctx.category(), ctx.group()), |
| 153 | + ctx.metadata().applicability(), |
| 154 | + markup! { "Use the shorthand ':' syntax instead." }.to_owned(), |
| 155 | + mutation, |
| 156 | + )) |
| 157 | + } |
| 158 | + // Convert shorthand :prop to v-bind:prop |
| 159 | + (AnyVueDirective::VueVBindShorthandDirective(sh), VueDirectiveStyle::Longhand) => { |
| 160 | + let arg = sh.arg().ok()?; |
| 161 | + let mut builder = |
| 162 | + make::vue_directive(make::ident("v-bind"), sh.modifiers()).with_arg(arg); |
| 163 | + if let Some(init) = sh.initializer() { |
| 164 | + builder = builder.with_initializer(init); |
| 165 | + } |
| 166 | + let new_node = builder.build(); |
| 167 | + mutation.replace_node( |
| 168 | + AnyVueDirective::VueVBindShorthandDirective(sh.clone()), |
| 169 | + AnyVueDirective::VueDirective(new_node), |
| 170 | + ); |
| 171 | + Some(biome_analyze::RuleAction::new( |
| 172 | + ctx.metadata().action_category(ctx.category(), ctx.group()), |
| 173 | + ctx.metadata().applicability(), |
| 174 | + markup! { "Use longhand 'v-bind' syntax instead." }.to_owned(), |
| 175 | + mutation, |
| 176 | + )) |
| 177 | + } |
| 178 | + _ => None, |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments