-
-
Notifications
You must be signed in to change notification settings - Fork 884
feat(lint): add noVueOptionsApi rule #8648
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9ae52a
feat(lint): add noVueOptionsApi nursery rule
baeseokjae b5cc770
fix(biome_js_analyze): address review comments for noVueOptionsApi rule
baeseokjae 70d730c
feat(lint): add Related Rules section to Vue Vapor rules
baeseokjae b1d0a11
test(lint): add edge case tests for noVueOptionsApi rule
baeseokjae 50e693a
Update crates/biome_js_analyze/src/lint/nursery/no_vue_options_api.rs
baeseokjae 7bd398f
test(lint): update noVueOptionsApi rule snapshots
baeseokjae 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
Some comments aren't visible on the classic Files Changed page.
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,21 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added a new nursery rule [`noVueOptionsApi`](https://biomejs.dev/linter/rules/no-vue-options-api/). | ||
|
|
||
| Biome now reports Vue Options API usage, which is incompatible with Vue 3.6's Vapor Mode. | ||
| This rule detects Options API patterns in `<script>` blocks, `defineComponent()`, and `createApp()` calls, | ||
| helping prepare codebases for Vapor Mode adoption. | ||
|
|
||
| For example, the following now triggers this rule: | ||
|
|
||
| ```vue | ||
| <script> | ||
| export default { | ||
| data() { | ||
| return { count: 0 }; | ||
| } | ||
| } | ||
| </script> | ||
| ``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
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
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
216 changes: 216 additions & 0 deletions
216
crates/biome_js_analyze/src/lint/nursery/no_vue_options_api.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,216 @@ | ||
| use biome_analyze::{Rule, RuleDiagnostic, RuleDomain, context::RuleContext, declare_lint_rule}; | ||
| use biome_console::markup; | ||
| use biome_diagnostics::Severity; | ||
| use biome_js_syntax::{AnyJsExpression, JsFileSource}; | ||
| use biome_rowan::{AstNode, TextRange}; | ||
| use biome_rule_options::no_vue_options_api::NoVueOptionsApiOptions; | ||
|
|
||
| use crate::frameworks::vue::vue_call::is_vue_api_reference; | ||
| use crate::frameworks::vue::vue_component::{ | ||
| AnyVueComponent, VueComponent, VueComponentQuery, VueOptionsApiBasedComponent, | ||
| }; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Disallow the use of Vue Options API. | ||
| /// | ||
| /// Vue 3.6's Vapor Mode does not support the Options API. | ||
| /// Components must use the Composition API (`<script setup>` or `defineComponent` with function signature) instead. | ||
| /// | ||
| /// This rule helps prepare codebases for Vapor Mode by detecting Options API | ||
| /// patterns that are incompatible with the new rendering mode. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <script> | ||
| /// export default { | ||
| /// data() { | ||
| /// return { count: 0 } | ||
| /// } | ||
| /// } | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <script> | ||
| /// export default { | ||
| /// methods: { | ||
| /// increment() { | ||
| /// this.count++ | ||
| /// } | ||
| /// } | ||
| /// } | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <script> | ||
| /// export default { | ||
| /// computed: { | ||
| /// doubled() { | ||
| /// return this.count * 2 | ||
| /// } | ||
| /// } | ||
| /// } | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <script> | ||
| /// export default { | ||
| /// mounted() { | ||
| /// console.log('Component mounted') | ||
| /// } | ||
| /// } | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```js,expect_diagnostic | ||
| /// import { defineComponent } from 'vue' | ||
| /// | ||
| /// defineComponent({ | ||
| /// name: 'MyComponent', | ||
| /// data() { | ||
| /// return { count: 0 } | ||
| /// } | ||
| /// }) | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```vue | ||
| /// <script setup> | ||
| /// import { ref } from 'vue' | ||
| /// const count = ref(0) | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```vue | ||
| /// <script setup> | ||
| /// import { ref, computed } from 'vue' | ||
| /// | ||
| /// const count = ref(0) | ||
| /// const doubled = computed(() => count.value * 2) | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ```vue | ||
| /// <script setup> | ||
| /// import { onMounted } from 'vue' | ||
| /// | ||
| /// onMounted(() => { | ||
| /// console.log('Component mounted') | ||
| /// }) | ||
| /// </script> | ||
| /// ``` | ||
| /// | ||
| /// ## Related Rules | ||
| /// | ||
| /// - [useVueVapor](https://biomejs.dev/linter/rules/use-vue-vapor): Enforces the use of Vapor mode in Vue components | ||
| /// | ||
| /// ## Resources | ||
| /// | ||
| /// - [Vue 3 Composition API](https://vuejs.org/api/composition-api-setup.html) | ||
| /// - [Options API vs Composition API](https://vuejs.org/guide/introduction.html#api-styles) | ||
| /// | ||
| pub NoVueOptionsApi { | ||
| version: "next", | ||
| name: "noVueOptionsApi", | ||
| language: "js", | ||
| recommended: false, | ||
| severity: Severity::Error, | ||
| domains: &[RuleDomain::Vue], | ||
| } | ||
| } | ||
|
|
||
| /// State for detected Options API component. | ||
| pub struct RuleState { | ||
| range: TextRange, | ||
| } | ||
|
|
||
| impl Rule for NoVueOptionsApi { | ||
| type Query = VueComponentQuery; | ||
| type State = RuleState; | ||
| type Signals = Option<Self::State>; | ||
| type Options = NoVueOptionsApiOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let component = VueComponent::from_potential_component( | ||
| ctx.query(), | ||
| ctx.model(), | ||
| ctx.source_type::<JsFileSource>(), | ||
| ctx.file_path(), | ||
| )?; | ||
|
|
||
| // <script setup> or defineComponent with function signature are valid for Vapor Mode | ||
| match component.kind() { | ||
| AnyVueComponent::Setup(_) => None, | ||
| AnyVueComponent::OptionsApi(opts) => { | ||
| let expr = opts.definition_expression()?; | ||
| if is_define_component_or_create_app(&expr, ctx.model()) { | ||
| return None; | ||
| } | ||
| Some(RuleState { | ||
| range: expr.range(), | ||
| }) | ||
| } | ||
| AnyVueComponent::DefineComponent(component) => { | ||
| if component.setup_func().is_some() { | ||
| return None; | ||
| } | ||
| let definition = component.definition_expression()?; | ||
| if definition.as_js_object_expression().is_some() { | ||
| Some(RuleState { | ||
| range: definition.range(), | ||
| }) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| AnyVueComponent::CreateApp(component) => { | ||
| let definition = component.definition_expression()?; | ||
| if definition.as_js_object_expression().is_some() { | ||
| Some(RuleState { | ||
| range: definition.range(), | ||
| }) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| state.range, | ||
| markup! { | ||
| "Using the Options API is not allowed." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Although the Options API is still supported by Vue, using the Composition API is recommended, and makes it possible to use Vue's Vapor mode for better performance." | ||
| }) | ||
| .note(markup! { | ||
| "Use "<Emphasis>"<script setup>"</Emphasis>" or "<Emphasis>"defineComponent"</Emphasis>" with a function signature to use the "<Hyperlink href="https://vuejs.org/guide/introduction.html#composition-api">"Composition API"</Hyperlink>" instead." | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fn is_define_component_or_create_app( | ||
| expr: &AnyJsExpression, | ||
| model: &biome_js_semantic::SemanticModel, | ||
| ) -> bool { | ||
| let Some(call_expr) = expr.as_js_call_expression() else { | ||
| return false; | ||
| }; | ||
| let Some(callee) = call_expr.callee().ok().and_then(|c| c.inner_expression()) else { | ||
| return false; | ||
| }; | ||
| is_vue_api_reference(&callee, model, "defineComponent") | ||
| || is_vue_api_reference(&callee, model, "createApp") | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
crates/biome_js_analyze/tests/specs/nursery/noVueOptionsApi/invalid-createapp-data.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 @@ | ||
| <script> | ||
| // should generate diagnostics | ||
| // createApp with Options API is not supported in Vapor Mode | ||
| import { createApp } from "vue"; | ||
|
|
||
| createApp({ | ||
| data() { | ||
| return { count: 0 }; | ||
| }, | ||
| }).mount("#app"); | ||
| </script> |
43 changes: 43 additions & 0 deletions
43
crates/biome_js_analyze/tests/specs/nursery/noVueOptionsApi/invalid-createapp-data.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,43 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid-createapp-data.vue | ||
| --- | ||
| # Input | ||
| ```ts | ||
| // should generate diagnostics | ||
| // createApp with Options API is not supported in Vapor Mode | ||
| import { createApp } from "vue"; | ||
|
|
||
| createApp({ | ||
| data() { | ||
| return { count: 0 }; | ||
| }, | ||
| }).mount("#app"); | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid-createapp-data.vue:5:11 lint/nursery/noVueOptionsApi ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| × Using the Options API is not allowed. | ||
|
|
||
| 3 │ import { createApp } from "vue"; | ||
| 4 │ | ||
| > 5 │ createApp({ | ||
| │ ^ | ||
| > 6 │ data() { | ||
| > 7 │ return { count: 0 }; | ||
| > 8 │ }, | ||
| > 9 │ }).mount("#app"); | ||
| │ ^ | ||
| 10 │ | ||
|
|
||
| i Although the Options API is still supported by Vue, using the Composition API is recommended, and makes it possible to use Vue's Vapor mode for better performance. | ||
|
|
||
| i Use <script setup> or defineComponent with a function signature to use the Composition API instead. | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noVueOptionsApi/invalid-createapp-empty.js
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 @@ | ||
| // should generate diagnostics | ||
| // createApp with empty object is Options API style | ||
| import { createApp } from "vue"; | ||
|
|
||
| createApp({}).mount("#app"); |
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.