|
| 1 | +use biome_analyze::{ |
| 2 | + Ast, FixKind, Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, |
| 3 | + declare_lint_rule, |
| 4 | +}; |
| 5 | +use biome_console::markup; |
| 6 | +use biome_html_factory::make; |
| 7 | +use biome_html_syntax::{ |
| 8 | + AnyHtmlAttribute, AstroIsDirective, HtmlFileSource, HtmlOpeningElement, HtmlSyntaxKind, |
| 9 | + HtmlSyntaxToken, |
| 10 | +}; |
| 11 | +use biome_rowan::{AstNode, AstNodeList, BatchMutationExt, SyntaxNodeCast}; |
| 12 | +use biome_rule_options::use_scoped_styles::UseScopedStylesOptions; |
| 13 | + |
| 14 | +declare_lint_rule! { |
| 15 | + /// Enforce that `<style>` blocks in Vue SFCs have the `scoped` attribute and that `<style>` blocks in Astro components do not have the `is:global` directive. |
| 16 | + /// |
| 17 | + /// Vue's `scoped` attribute automatically scopes CSS to the component, |
| 18 | + /// preventing style leakage and conflicts. Astro's `is:global` attribute |
| 19 | + /// allows for global styles, but without it, styles are scoped to the component by default. |
| 20 | + /// |
| 21 | + /// Style blocks with the `module` attribute are exempt, as CSS Modules |
| 22 | + /// is an alternative scoping mechanism. |
| 23 | + /// |
| 24 | + /// ## Examples |
| 25 | + /// |
| 26 | + /// ### Invalid |
| 27 | + /// |
| 28 | + /// ```vue,expect_diagnostic |
| 29 | + /// <style> |
| 30 | + /// .foo { color: red; } |
| 31 | + /// </style> |
| 32 | + /// ``` |
| 33 | + /// |
| 34 | + /// ```astro,expect_diagnostic |
| 35 | + /// <style is:global> |
| 36 | + /// .foo { color: red; } |
| 37 | + /// </style> |
| 38 | + /// ``` |
| 39 | + /// |
| 40 | + /// ### Valid |
| 41 | + /// |
| 42 | + /// ```vue |
| 43 | + /// <style scoped> |
| 44 | + /// .foo { color: red; } |
| 45 | + /// </style> |
| 46 | + /// ``` |
| 47 | + /// |
| 48 | + /// ```vue |
| 49 | + /// <style module> |
| 50 | + /// .foo { color: red; } |
| 51 | + /// </style> |
| 52 | + /// ``` |
| 53 | + /// |
| 54 | + /// ## References: |
| 55 | + /// |
| 56 | + /// - [Vue Documentation](https://vuejs.org/api/sfc-css-features.html#scoped-css) |
| 57 | + /// - [Astro Documentation](https://docs.astro.build/en/guides/styling/#global-styles) |
| 58 | + pub UseScopedStyles { |
| 59 | + version: "next", |
| 60 | + name: "useScopedStyles", |
| 61 | + language: "html", |
| 62 | + recommended: true, |
| 63 | + domains: &[RuleDomain::Vue], |
| 64 | + sources: &[RuleSource::EslintVueJs("enforce-style-attribute").inspired()], |
| 65 | + fix_kind: FixKind::Unsafe, |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +pub enum GlobalStylesKind { |
| 70 | + Vue, |
| 71 | + Astro { directive: AstroIsDirective }, |
| 72 | +} |
| 73 | + |
| 74 | +impl Rule for UseScopedStyles { |
| 75 | + type Query = Ast<HtmlOpeningElement>; |
| 76 | + type State = GlobalStylesKind; |
| 77 | + type Signals = Option<Self::State>; |
| 78 | + type Options = UseScopedStylesOptions; |
| 79 | + |
| 80 | + fn run(ctx: &RuleContext<Self>) -> Self::Signals { |
| 81 | + if !ctx.source_type::<HtmlFileSource>().is_vue() |
| 82 | + && !ctx.source_type::<HtmlFileSource>().is_astro() |
| 83 | + { |
| 84 | + return None; |
| 85 | + } |
| 86 | + |
| 87 | + let opening = ctx.query(); |
| 88 | + |
| 89 | + let name = opening.name().ok()?; |
| 90 | + let name_text = name.token_text_trimmed()?; |
| 91 | + if !name_text.eq_ignore_ascii_case("style") { |
| 92 | + return None; |
| 93 | + } |
| 94 | + |
| 95 | + let attributes = opening.attributes(); |
| 96 | + if ctx.source_type::<HtmlFileSource>().is_vue() { |
| 97 | + let has_scoped = attributes.find_by_name("scoped").is_some(); |
| 98 | + let has_module = attributes.find_by_name("module").is_some(); |
| 99 | + |
| 100 | + if has_scoped || has_module { |
| 101 | + return None; |
| 102 | + } else { |
| 103 | + return Some(GlobalStylesKind::Vue); |
| 104 | + } |
| 105 | + } else if ctx.source_type::<HtmlFileSource>().is_astro() { |
| 106 | + let is_directives = attributes |
| 107 | + .iter() |
| 108 | + .filter_map(|attr| attr.syntax().clone().cast::<AstroIsDirective>()); |
| 109 | + for directive in is_directives { |
| 110 | + let name = directive.value().ok()?.name().ok()?; |
| 111 | + let name_text = name.token_text_trimmed()?; |
| 112 | + if name_text.eq_ignore_ascii_case("global") { |
| 113 | + return Some(GlobalStylesKind::Astro { directive }); |
| 114 | + } |
| 115 | + } |
| 116 | + return None; |
| 117 | + } |
| 118 | + |
| 119 | + None |
| 120 | + } |
| 121 | + |
| 122 | + fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { |
| 123 | + match state { |
| 124 | + GlobalStylesKind::Vue => { |
| 125 | + Some( |
| 126 | + RuleDiagnostic::new( |
| 127 | + rule_category!(), |
| 128 | + ctx.query().range(), |
| 129 | + markup! { |
| 130 | + "This "<Emphasis>"<style>"</Emphasis>" block is missing the "<Emphasis>"scoped"</Emphasis>" attribute." |
| 131 | + }, |
| 132 | + ) |
| 133 | + .note(markup! { |
| 134 | + "In Vue, unscoped styles become global across the entire project. This can lead to unintended side effects and maintenance challenges. Adding the "<Emphasis>"scoped"</Emphasis>" attribute ensures that styles are scoped to this component, preventing style leakage and conflicts." |
| 135 | + }), |
| 136 | + ) |
| 137 | + }, |
| 138 | + GlobalStylesKind::Astro { directive } => { |
| 139 | + Some( |
| 140 | + RuleDiagnostic::new( |
| 141 | + rule_category!(), |
| 142 | + directive.range(), |
| 143 | + markup! { |
| 144 | + "This "<Emphasis>"is:global"</Emphasis>" directive is making the styles in this block global." |
| 145 | + }, |
| 146 | + ) |
| 147 | + .note(markup! { |
| 148 | + "In Astro, styles are scoped to the component by default. The "<Emphasis>"is:global"</Emphasis>" directive allows for global styles, but it can lead to unintended side effects and maintenance challenges." |
| 149 | + }), |
| 150 | + ) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + fn action(ctx: &RuleContext<Self>, state: &Self::State) -> Option<crate::HtmlRuleAction> { |
| 156 | + match state { |
| 157 | + GlobalStylesKind::Vue => { |
| 158 | + let opening = ctx.query(); |
| 159 | + let old_attributes = opening.attributes(); |
| 160 | + |
| 161 | + let token = |
| 162 | + HtmlSyntaxToken::new_detached(HtmlSyntaxKind::HTML_LITERAL, " scoped", [], []); |
| 163 | + |
| 164 | + let attr = AnyHtmlAttribute::HtmlAttribute( |
| 165 | + make::html_attribute(make::html_attribute_name(token)).build(), |
| 166 | + ); |
| 167 | + let mut items: Vec<AnyHtmlAttribute> = old_attributes.iter().collect(); |
| 168 | + items.push(attr); |
| 169 | + let new_attributes = make::html_attribute_list(items); |
| 170 | + |
| 171 | + let mut mutation = BatchMutationExt::begin(ctx.root()); |
| 172 | + mutation.replace_node(old_attributes, new_attributes); |
| 173 | + |
| 174 | + Some(biome_analyze::RuleAction::new( |
| 175 | + ctx.metadata().action_category(ctx.category(), ctx.group()), |
| 176 | + ctx.metadata().applicability(), |
| 177 | + markup! { "Add the "<Emphasis>"scoped"</Emphasis>" attribute so the styles will only apply to this component." }.to_owned(), |
| 178 | + mutation, |
| 179 | + )) |
| 180 | + } |
| 181 | + GlobalStylesKind::Astro { directive } => { |
| 182 | + let mut mutation = BatchMutationExt::begin(ctx.root()); |
| 183 | + mutation.remove_node(directive.clone()); |
| 184 | + |
| 185 | + Some(biome_analyze::RuleAction::new( |
| 186 | + ctx.metadata().action_category(ctx.category(), ctx.group()), |
| 187 | + ctx.metadata().applicability(), |
| 188 | + markup! { "Remove the "<Emphasis>"is:global"</Emphasis>" directive so the styles in this block will be scoped to this component." }.to_owned(), |
| 189 | + mutation, |
| 190 | + )) |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments