-
-
Notifications
You must be signed in to change notification settings - Fork 964
feat(analyze/html/vue): add useVueVapor rule #8644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dyc3
merged 2 commits into
biomejs:main
from
JacquesLeupin:feat/analyze-html-vue-use-vue-vapor
Jan 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule `useVueVapor` to enforce `<script setup vapor>` in Vue SFCs. For example `<script setup>` is invalid. |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
crates/biome_html_analyze/src/lint/nursery/use_vue_vapor.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| use biome_analyze::{ | ||
| Ast, FixKind, Rule, RuleDiagnostic, RuleDomain, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_html_factory::make; | ||
| use biome_html_syntax::{ | ||
| AnyHtmlAttribute, HtmlAttributeList, HtmlOpeningElement, HtmlSyntaxKind, HtmlSyntaxToken, | ||
| }; | ||
| use biome_rowan::{AstNode, AstNodeList, BatchMutationExt, TriviaPiece}; | ||
| use biome_rule_options::use_vue_vapor::UseVueVaporOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Enforce opting in to Vue Vapor mode in `<script setup>` blocks. | ||
| /// | ||
| /// Vue 3.6 introduces an opt-in “Vapor mode” for SFC `<script setup>` blocks: | ||
| /// `<script setup vapor>`. | ||
| /// | ||
| /// Vapor mode only works for Vue Single File Components (SFCs) using `<script setup>`. | ||
| /// | ||
| /// This rule reports `<script setup>` opening tags that are missing the `vapor` attribute. | ||
|
JacquesLeupin marked this conversation as resolved.
|
||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <script setup> | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```vue | ||
| /// <script setup vapor> | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| pub UseVueVapor { | ||
| version: "next", | ||
| name: "useVueVapor", | ||
| language: "html", | ||
| recommended: false, | ||
| domains: &[RuleDomain::Vue], | ||
| sources: &[], | ||
| fix_kind: FixKind::Unsafe, | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseVueVapor { | ||
| type Query = Ast<HtmlOpeningElement>; | ||
| type State = (); | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseVueVaporOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let opening = ctx.query(); | ||
|
|
||
| let name = opening.name().ok()?; | ||
| let name_token = name.value_token().ok()?; | ||
| if !name_token.text_trimmed().eq_ignore_ascii_case("script") { | ||
| return None; | ||
| } | ||
|
|
||
| let attributes = opening.attributes(); | ||
| attributes.find_by_name("setup")?; | ||
|
|
||
| if attributes.find_by_name("vapor").is_some() { | ||
| return None; | ||
| } | ||
|
|
||
| Some(()) | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| ctx.query().range(), | ||
| markup! { | ||
| "This "<Emphasis>"<script setup>"</Emphasis>" is missing the "<Emphasis>"vapor"</Emphasis>" attribute." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Add "<Emphasis>"vapor"</Emphasis>" to opt in to Vue Vapor mode: "<Emphasis>"<script setup vapor>"</Emphasis>"." | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<crate::HtmlRuleAction> { | ||
| let opening = ctx.query(); | ||
| let old_attributes = opening.attributes(); | ||
|
|
||
| // Only apply the fix for <script setup> that doesn't already have vapor. | ||
| if old_attributes.find_by_name("setup").is_none() | ||
| || old_attributes.find_by_name("vapor").is_some() | ||
| { | ||
| return None; | ||
| } | ||
|
|
||
| let new_attributes = insert_after_setup(old_attributes)?; | ||
|
|
||
| let mut mutation = BatchMutationExt::begin(ctx.root()); | ||
| mutation.replace_node(opening.attributes(), new_attributes); | ||
|
|
||
| Some(biome_analyze::RuleAction::new( | ||
| ctx.metadata().action_category(ctx.category(), ctx.group()), | ||
| ctx.metadata().applicability(), | ||
| markup! { "Add the "<Emphasis>"vapor"</Emphasis>" attribute." }.to_owned(), | ||
| mutation, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| enum VaporAttributeSpacing { | ||
| /// Add a leading space before `vapor` (used when `setup` is the last attribute). | ||
| Leading, | ||
| /// Add a trailing space after `vapor` (used when there are more attributes after `setup`). | ||
| Trailing, | ||
| } | ||
|
|
||
| fn make_vapor_attribute(spacing: VaporAttributeSpacing) -> AnyHtmlAttribute { | ||
| let vapor_token = match spacing { | ||
| VaporAttributeSpacing::Leading => HtmlSyntaxToken::new_detached( | ||
| HtmlSyntaxKind::IDENT, | ||
| " vapor", | ||
| [TriviaPiece::whitespace(1)], | ||
| [], | ||
| ), | ||
| VaporAttributeSpacing::Trailing => HtmlSyntaxToken::new_detached( | ||
| HtmlSyntaxKind::IDENT, | ||
| "vapor ", | ||
| [], | ||
| [TriviaPiece::whitespace(1)], | ||
| ), | ||
| }; | ||
|
|
||
| AnyHtmlAttribute::HtmlAttribute( | ||
| make::html_attribute(make::html_attribute_name(vapor_token)).build(), | ||
| ) | ||
| } | ||
|
|
||
| fn insert_after_setup(old_attributes: HtmlAttributeList) -> Option<HtmlAttributeList> { | ||
| let mut items: Vec<AnyHtmlAttribute> = old_attributes.iter().collect(); | ||
|
|
||
| let setup_index = items.iter().position(is_setup_attribute)?; | ||
|
|
||
| let spacing = if setup_index + 1 == items.len() { | ||
| VaporAttributeSpacing::Leading | ||
| } else { | ||
| VaporAttributeSpacing::Trailing | ||
| }; | ||
|
|
||
| items.insert(setup_index + 1, make_vapor_attribute(spacing)); | ||
|
|
||
| Some(make::html_attribute_list(items)) | ||
| } | ||
|
|
||
| fn is_setup_attribute(attribute: &AnyHtmlAttribute) -> bool { | ||
| match attribute { | ||
| AnyHtmlAttribute::HtmlAttribute(attr) => attr | ||
| .name() | ||
| .ok() | ||
| .and_then(|name| name.value_token().ok()) | ||
| .is_some_and(|tok| tok.text_trimmed() == "setup"), | ||
| _ => false, | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
crates/biome_html_analyze/tests/specs/nursery/useVueVapor/invalid.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!-- should generate diagnostics --> | ||
|
|
||
| <script setup> | ||
| const a = 1; | ||
| </script> | ||
|
|
||
| <script setup lang="ts"> | ||
| const b: number = 1; | ||
| </script> | ||
|
|
||
|
|
66 changes: 66 additions & 0 deletions
66
crates/biome_html_analyze/tests/specs/nursery/useVueVapor/invalid.vue.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: invalid.vue | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should generate diagnostics --> | ||
|
|
||
| <script setup> | ||
| const a = 1; | ||
| </script> | ||
|
|
||
| <script setup lang="ts"> | ||
| const b: number = 1; | ||
| </script> | ||
|
|
||
|
|
||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.vue:3:1 lint/nursery/useVueVapor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This <script setup> is missing the vapor attribute. | ||
|
|
||
| 1 │ <!-- should generate diagnostics --> | ||
| 2 │ | ||
| > 3 │ <script setup> | ||
| │ ^^^^^^^^^^^^^^ | ||
| 4 │ const a = 1; | ||
| 5 │ </script> | ||
|
|
||
| i Add vapor to opt in to Vue Vapor mode: <script setup vapor>. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
| i Unsafe fix: Add the vapor attribute. | ||
|
|
||
| 3 │ <script·setup·vapor> | ||
| │ ++++++ | ||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid.vue:7:1 lint/nursery/useVueVapor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This <script setup> is missing the vapor attribute. | ||
|
|
||
| 5 │ </script> | ||
| 6 │ | ||
| > 7 │ <script setup lang="ts"> | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 8 │ const b: number = 1; | ||
| 9 │ </script> | ||
|
|
||
| i Add vapor to opt in to Vue Vapor mode: <script setup vapor>. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
| i Unsafe fix: Add the vapor attribute. | ||
|
|
||
| 7 │ <script·setup·vapor·lang="ts"> | ||
| │ ++++++ | ||
|
|
||
| ``` |
15 changes: 15 additions & 0 deletions
15
crates/biome_html_analyze/tests/specs/nursery/useVueVapor/valid.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!-- should not generate diagnostics --> | ||
|
|
||
| <script setup vapor> | ||
| const a = 1; | ||
| </script> | ||
|
|
||
| <script setup vapor lang="ts"> | ||
| const b: number = 1; | ||
| </script> | ||
|
|
||
| <script> | ||
| export default {}; | ||
| </script> | ||
|
|
||
|
|
23 changes: 23 additions & 0 deletions
23
crates/biome_html_analyze/tests/specs/nursery/useVueVapor/valid.vue.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| source: crates/biome_html_analyze/tests/spec_tests.rs | ||
| expression: valid.vue | ||
| --- | ||
| # Input | ||
| ```html | ||
| <!-- should not generate diagnostics --> | ||
|
|
||
| <script setup vapor> | ||
| const a = 1; | ||
| </script> | ||
|
|
||
| <script setup vapor lang="ts"> | ||
| const b: number = 1; | ||
| </script> | ||
|
|
||
| <script> | ||
| export default {}; | ||
| </script> | ||
|
|
||
|
|
||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| use biome_deserialize_macros::{Deserializable, Merge}; | ||
| use serde::{Deserialize, Serialize}; | ||
| #[derive(Default, Clone, Debug, Deserialize, Deserializable, Merge, Eq, PartialEq, Serialize)] | ||
| #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] | ||
| #[serde(rename_all = "camelCase", deny_unknown_fields, default)] | ||
| pub struct UseVueVaporOptions {} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.