-
-
Notifications
You must be signed in to change notification settings - Fork 958
feat(html/analyze): add useVueValidVBind #8060
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
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,12 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useVueValidVBind`](https://biomejs.dev/linter/rules/use-vue-valid-v-bind/), which enforces the validity of `v-bind` directives in Vue files. | ||
|
|
||
| Invalid `v-bind` usages include: | ||
| ```vue | ||
| <Foo v-bind /> <!-- Missing argument --> | ||
| <Foo v-bind:foo /> <!-- Missing value --> | ||
| <Foo v-bind:foo.bar="baz" /> <!-- Invalid modifier --> | ||
| ``` | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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,7 @@ | ||
| //! Generated file, do not edit by hand, see `xtask/codegen` | ||
|
|
||
| //! Generated file, do not edit by hand, see `xtask/codegen` | ||
|
|
||
| use biome_analyze::declare_lint_group; | ||
| pub mod use_vue_valid_v_bind; | ||
| declare_lint_group! { pub Nursery { name : "nursery" , rules : [self :: use_vue_valid_v_bind :: UseVueValidVBind ,] } } |
150 changes: 150 additions & 0 deletions
150
crates/biome_html_analyze/src/lint/nursery/use_vue_valid_v_bind.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,150 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleDomain, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_html_syntax::{AnyVueDirective, VueModifierList}; | ||
| use biome_rowan::{AstNode, TextRange}; | ||
| use biome_rule_options::use_vue_valid_v_bind::UseVueValidVBindOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Forbids `v-bind` directives with missing arguments or invalid modifiers. | ||
| /// | ||
| /// This rule reports v-bind directives in the following cases: | ||
| /// - The directive does not have an argument. E.g. `<div v-bind></div>` | ||
| /// - The directive does not have a value. E.g. `<div v-bind:aaa></div>` | ||
| /// - The directive has invalid modifiers. E.g. `<div v-bind:aaa.bbb="ccc"></div>` | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <Foo v-bind /> | ||
| /// ``` | ||
| /// | ||
| /// ```vue,expect_diagnostic | ||
| /// <div v-bind></div> | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```vue | ||
| /// <Foo v-bind:foo="foo" /> | ||
| /// ``` | ||
| /// | ||
| pub UseVueValidVBind { | ||
| version: "next", | ||
| name: "useVueValidVBind", | ||
| language: "html", | ||
| recommended: true, | ||
| domains: &[RuleDomain::Vue], | ||
| sources: &[RuleSource::EslintVueJs("valid-v-bind").same()], | ||
| } | ||
| } | ||
|
|
||
| const VALID_MODIFIERS: &[&str] = &["prop", "camel", "sync", "attr"]; | ||
dyc3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| pub enum ViolationKind { | ||
| MissingValue, | ||
| MissingArgument, | ||
| InvalidModifier(TextRange), | ||
| } | ||
|
|
||
| impl Rule for UseVueValidVBind { | ||
| type Query = Ast<AnyVueDirective>; | ||
| type State = ViolationKind; | ||
| type Signals = Option<Self::State>; | ||
| type Options = UseVueValidVBindOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Option<Self::State> { | ||
| let node = ctx.query(); | ||
| match node { | ||
| AnyVueDirective::VueDirective(vue_directive) => { | ||
| if vue_directive.name_token().ok()?.text_trimmed() != "v-bind" { | ||
| return None; | ||
| } | ||
|
|
||
| if vue_directive.initializer().is_none() { | ||
| return Some(ViolationKind::MissingValue); | ||
| } | ||
|
|
||
| if vue_directive.arg().is_none() { | ||
| return Some(ViolationKind::MissingArgument); | ||
| } | ||
|
|
||
| if let Some(invalid_range) = find_invalid_modifiers(&vue_directive.modifiers()) { | ||
| return Some(ViolationKind::InvalidModifier(invalid_range)); | ||
| } | ||
|
|
||
| None | ||
dyc3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| AnyVueDirective::VueVBindShorthandDirective(dir) => { | ||
| // missing argument would be caught by the parser | ||
|
|
||
| if dir.initializer().is_none() { | ||
| return Some(ViolationKind::MissingValue); | ||
| } | ||
|
|
||
| if let Some(invalid_range) = find_invalid_modifiers(&dir.modifiers()) { | ||
| return Some(ViolationKind::InvalidModifier(invalid_range)); | ||
| } | ||
|
|
||
| None | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
||
| fn diagnostic(ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| match state { | ||
| ViolationKind::MissingValue => RuleDiagnostic::new( | ||
| rule_category!(), | ||
| ctx.query().range(), | ||
| markup! { | ||
| "This v-bind directive is missing a value." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "v-bind directives require a value." | ||
| }).note(markup! { | ||
| "Add a value to the directive, e.g. "<Emphasis>"v-bind:foo=\"bar\""</Emphasis>"." | ||
| }), | ||
| ViolationKind::MissingArgument => RuleDiagnostic::new( | ||
| rule_category!(), | ||
| ctx.query().range(), | ||
| markup! { | ||
| "This v-bind directive is missing an argument." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "v-bind directives require an argument to specify which attribute to bind to." | ||
| }).note(markup! { | ||
| "For example, use " <Emphasis>"v-bind:foo"</Emphasis> " to bind to the " <Emphasis>"foo"</Emphasis> " attribute." | ||
| }), | ||
| ViolationKind::InvalidModifier(invalid_range) => | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| invalid_range, | ||
| markup! { | ||
| "This v-bind directive has an invalid modifier." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Only the following modifiers are allowed on v-bind directives: "<Emphasis>"prop"</Emphasis>", "<Emphasis>"camel"</Emphasis>", "<Emphasis>"sync"</Emphasis>", and "<Emphasis>"attr"</Emphasis>"." | ||
| }).note(markup! { | ||
| "Remove or correct the invalid modifier." | ||
| }), | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fn find_invalid_modifiers(modifiers: &VueModifierList) -> Option<TextRange> { | ||
| for modifier in modifiers { | ||
| if !VALID_MODIFIERS.contains(&modifier.modifier_token().ok()?.text()) { | ||
| return Some(modifier.range()); | ||
| } | ||
| } | ||
| None | ||
| } | ||
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
33 changes: 33 additions & 0 deletions
33
crates/biome_html_analyze/tests/specs/nursery/useVueValidVBind/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,33 @@ | ||
| <!-- should generate diagnostics --> | ||
|
|
||
| <template> | ||
| <!-- Missing argument: long-form without an argument --> | ||
| <div v-bind></div> | ||
| <div v-bind /> | ||
| <Foo v-bind /> | ||
|
|
||
| <!-- Missing value --> | ||
| <Foo v-bind:foo /> | ||
| <Foo :foo /> | ||
|
|
||
| <!-- Missing argument with modifier --> | ||
| <div v-bind.prop></div> | ||
|
|
||
| <!-- Invalid single modifier on long-form --> | ||
| <div v-bind:foo.invalid="bar"></div> | ||
|
|
||
| <!-- Invalid modifier on shorthand --> | ||
| <span :bar.badModifier="baz"></span> | ||
|
|
||
| <!-- Mixed valid and invalid modifiers: 'prop' is valid, 'wrong' is not --> | ||
| <p :baz.prop.wrong="value"></p> | ||
|
|
||
| <!-- Dynamic argument is present but modifier is invalid --> | ||
| <p v-bind:[dynamic].notAValidModifier="value"></p> | ||
|
|
||
| <!-- Multiple invalid modifiers --> | ||
| <button :disabled.once="true"></button> | ||
|
|
||
| <!-- Component binding with unknown modifier --> | ||
| <MyComponent v-bind:propName.weird="someValue"></MyComponent> | ||
| </template> |
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.