-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
feat(lint): new rule useValidAriaRole
#553
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I am going to block for now while we focus only on bug fixes :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contributions! I added some comments.
crates/biome_js_analyze/src/aria_analyzers/nursery/use_valid_aria_role.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/aria_analyzers/nursery/use_valid_aria_role.rs
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,6 @@ | |||
<> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add eslint's tests as much as possible
ref: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/__tests__/src/rules/aria-role-test.js
@@ -0,0 +1,6 @@ | |||
<> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add eslint's tests as much as possible
ref: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/__tests__/src/rules/aria-role-test.js
crates/biome_js_analyze/src/aria_analyzers/nursery/use_valid_aria_role.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/aria_analyzers/nursery/use_valid_aria_role.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/aria_analyzers/nursery/use_valid_aria_role.rs
Outdated
Show resolved
Hide resolved
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the phrase you want in the documentation, not here. Here, you want to have something like "The role <ROLE_NAME> doesn't exist", or `"The role <ROLE_NAME> is abstract and can't be applied.*
}, | ||
) | ||
.note(markup! { | ||
"Check "<Hyperlink href="https://www.w3.org/TR/wai-aria/#namefromauthor">"wai-aria"</Hyperlink>" for valid roles or provide options accordingly." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Check "<Hyperlink href="https://www.w3.org/TR/wai-aria/#namefromauthor">"wai-aria"</Hyperlink>" for valid roles or provide options accordingly." | |
"Check "<Hyperlink href="https://www.w3.org/TR/wai-aria/#namefromauthor">"WAI-ARIA"</Hyperlink>" for valid roles or provide options accordingly." |
pub(crate) UseValidAriaRole { | ||
version: "next", | ||
name: "useValidAriaRole", | ||
recommended: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be recommended, I think. ARIA rules are very important.
pub struct UseValidAriaRoleOptions { | ||
allowed_invalid_roles: Vec<String>, | ||
ignore_non_dom: bool, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You created an option, but it seems we are not using it. In order to "wire" an option to Biome, you have to:
- Add it to the list of rules that have options:
biome/crates/biome_js_analyze/src/options.rs
Lines 30 to 42 in e6c12c0
pub enum PossibleOptions { /// Options for `noExcessiveComplexity` rule Complexity(#[bpaf(external(complexity_options), hide)] ComplexityOptions), /// Options for `useExhaustiveDependencies` and `useHookAtTopLevel` rule Hooks(#[bpaf(external(hooks_options), hide)] HooksOptions), /// Options for `useNamingConvention` rule NamingConvention(#[bpaf(external(naming_convention_options), hide)] NamingConventionOptions), /// Options for `noRestrictedGlobals` rule RestrictedGlobals(#[bpaf(external(restricted_globals_options), hide)] RestrictedGlobalsOptions), /// No options available #[default] NoOptions, }
match rule_key.rule_name() { |
biome/crates/biome_js_analyze/src/options.rs
Line 155 in e6c12c0
"useExhaustiveDependencies" | "useHookAtTopLevel" => { |
-
You must apply all the correct macros, here's an example:
biome/crates/biome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs
Lines 62 to 70 in e6c12c0
#[derive(Default, Deserialize, Serialize, Eq, PartialEq, Debug, Clone, Bpaf)] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct RestrictedGlobalsOptions { /// A list of names that should trigger the rule #[serde(skip_serializing_if = "Option::is_none")] #[bpaf(hide, argument::<String>("NUM"), many, optional)] denied_globals: Option<Vec<String>>, } -
Apply
FromStr
, with no fancy implementation
biome/crates/biome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs
Line 77 in e6c12c0
impl FromStr for RestrictedGlobalsOptions { -
And apply our internal deserialisation
biome/crates/biome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs
Lines 86 to 109 in e6c12c0
impl VisitNode<JsonLanguage> for RestrictedGlobalsOptions { fn visit_member_name( &mut self, node: &SyntaxNode<JsonLanguage>, diagnostics: &mut Vec<DeserializationDiagnostic>, ) -> Option<()> { has_only_known_keys(node, Self::KNOWN_KEYS, diagnostics) } fn visit_map( &mut self, key: &SyntaxNode<JsonLanguage>, value: &SyntaxNode<JsonLanguage>, diagnostics: &mut Vec<DeserializationDiagnostic>, ) -> Option<()> { let (name, value) = self.get_key_and_value(key, value, diagnostics)?; let name_text = name.text(); if name_text == "deniedGlobals" { self.denied_globals = self.map_to_array_of_strings(&value, name_text, diagnostics); } Some(()) } }
For testing, the guide should help you: https://github.com/biomejs/biome/blob/main/crates/biome_analyze/CONTRIBUTING.md#rule-configuration
Once you've done this, there are two missing things:
- add documentation comments for
allowed_invalid_roles
andignore_non_dom
and explain what they do - update the documentation of this rule by adding the options and explaining them:
biome/crates/biome_js_analyze/src/semantic_analyzers/style/no_restricted_globals.rs
Lines 35 to 50 in e6c12c0
/// ## Options /// /// Use the options to specify additional globals that you want to restrict in your /// source code. /// /// ```json /// { /// "//": "...", /// "options": { /// "deniedGlobals": ["$", "MooTools"] /// } /// } /// ``` /// /// In the example above, the rule will emit a diagnostics if tried to use `$` or `MooTools` without /// creating a local variable.
b9cef7d
to
212a447
Compare
fn visit_member_name( | ||
&mut self, | ||
node: &SyntaxNode<JsonLanguage>, | ||
diagnostics: &mut Vec<DeserializationDiagnostic>, | ||
) -> Option<()> { | ||
has_only_known_keys(node, Self::KNOWN_KEYS, diagnostics) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget to rebase your work on main. I pushed some changes related to config parsing. In the process I removed visit_member_name
. I introduced report_unknown_map_key
that you can use directly in visit_map
.
useValidAriaRole
useValidAriaRole
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can merge, we should resolve the conflicts
eddb5a9
to
1a4c382
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Summary
Closes #526
Tasks
Test Plan