Skip to content
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

fix(useValidAriaValues): correctly check property types #3768

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,15 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

- [noRedeclare](https://biomejs.dev/linter/rules/no-redeclare/) no longer report a variable named as the function expression where it is declared. Contributed by @Conaclos

- [noMultipleSpacesInRegularExpressionLiterals](https://biomejs.dev/linter/rules/no-multiple-spaces-in-regular-expression-literals/) now correctly provides a code fix when unicode characters are used.
- [noMultipleSpacesInRegularExpressionLiterals](https://biomejs.dev/linter/rules/no-multiple-spaces-in-regular-expression-literals/) now correctly provides a code fix when Unicode characters are used. Contributed by @Conaclos

- [useValidAriaValues](https://biomejs.dev/linter/rules/use-valid-aria-values/) now correctly check property types ([3748](https://github.com/biomejs/biome/issues/3748)).

Properties that expect a string now accept arbitrary text.
An identifiers can now be made up of any characters except ASCII whitespace.
An identifier list can now be separated by any ASCII whitespace.

Contributed by @Conaclos

- `useAdjacentOverloadSignatures` no longer reports a `#private` class member and a public class member that share the same name ([#3309](https://github.com/biomejs/biome/issues/3309)).

Expand Down
54 changes: 32 additions & 22 deletions crates/biome_aria/src/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ define_property! {

define_property! {
AriaExpanded {
PROPERTY_TYPE: "boolean",
VALUES: [],
PROPERTY_TYPE: "token",
VALUES: ["true", "false", "undefined"],
}
}

Expand All @@ -125,8 +125,8 @@ define_property! {

define_property! {
AriaGrabbed {
PROPERTY_TYPE: "boolean",
VALUES: [],
PROPERTY_TYPE: "token",
VALUES: ["true", "false", "undefined"],
}
}

Expand All @@ -139,8 +139,8 @@ define_property! {

define_property! {
AriaHidden {
PROPERTY_TYPE: "boolean",
VALUES: [],
PROPERTY_TYPE: "token",
VALUES: ["true", "false", "undefined"],
}
}

Expand Down Expand Up @@ -291,8 +291,8 @@ define_property! {

define_property! {
AriaSelected {
PROPERTY_TYPE: "boolean",
VALUES: [],
PROPERTY_TYPE: "token",
VALUES: ["true", "false", "undefined"],
}
}

Expand Down Expand Up @@ -337,6 +337,7 @@ define_property! {
VALUES: [],
}
}

/// A collection of ARIA properties with their metadata, necessary to perform various operations.
#[derive(Debug, Default)]
pub struct AriaProperties;
Expand Down Expand Up @@ -427,28 +428,37 @@ pub trait AriaPropertyDefinition: Debug {
return false;
}
match self.property_type() {
AriaPropertyTypeEnum::String | AriaPropertyTypeEnum::Id => {
input_value.parse::<f32>().is_err()
AriaPropertyTypeEnum::String => true,
AriaPropertyTypeEnum::Id => is_valid_html_id(input_value),
AriaPropertyTypeEnum::Idlist => {
input_value.split_ascii_whitespace().all(is_valid_html_id)
}
AriaPropertyTypeEnum::Idlist => input_value
.split(' ')
.any(|piece| piece.parse::<f32>().is_err()),
// A numerical value without a fractional component.
AriaPropertyTypeEnum::Integer => input_value.parse::<u32>().is_ok(),
AriaPropertyTypeEnum::Number => input_value.parse::<f32>().is_ok(),
AriaPropertyTypeEnum::Boolean => {
matches!(input_value, "false" | "true")
}
AriaPropertyTypeEnum::Boolean => matches!(input_value, "false" | "true"),
AriaPropertyTypeEnum::Token => self
.values()
.any(|allowed_token| *allowed_token == input_value),
AriaPropertyTypeEnum::Tokenlist => input_value.split(' ').all(|input_token| {
self.values()
.any(|allowed_token| allowed_token.trim() == input_token)
}),
AriaPropertyTypeEnum::Tristate => {
matches!(input_value, "false" | "true" | "mixed")
AriaPropertyTypeEnum::Tokenlist => {
input_value.split_ascii_whitespace().all(|input_token| {
self.values()
.any(|allowed_token| allowed_token.trim() == input_token)
})
}
AriaPropertyTypeEnum::Tristate => matches!(input_value, "false" | "true" | "mixed"),
}
}
}

/// Is `id` a valid HTML identifier?
///
/// Browsers allows arbitrary sequence of characters for identifiers.
/// However, it is commonly accepted that an identifier should not contain
/// characters recognized as whitespaces by HTML.
/// Whitespaces are usedd to separate two identifier in a list of identifiers.
///
/// See https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id#syntax
fn is_valid_html_id(id: &str) -> bool {
!id.is_empty() && !id.bytes().any(|b| b.is_ascii_whitespace())
}
16 changes: 14 additions & 2 deletions crates/biome_js_analyze/src/lint/a11y/use_valid_aria_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,20 @@ impl Rule for UseValidAriaValues {
}
)
}
AriaPropertyTypeEnum::Id |
AriaPropertyTypeEnum::Idlist |
AriaPropertyTypeEnum::Id => {
diagnostic.note(
markup!{
"The only supported value is an HTML identifier."
}
)
}
AriaPropertyTypeEnum::Idlist => {
diagnostic.note(
markup!{
"The only supported value is a space-separated list of HTML identifiers."
}
)
}
AriaPropertyTypeEnum::String => {
diagnostic.note(
markup!{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ invalid.jsx:6:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━
7 │ var a = <span aria-relevant="fancy"></span>;
8 │ var a = <span aria-labelledby=""></span>;

i The only supported value is text.
i The only supported value is an HTML identifier.


```
Expand Down Expand Up @@ -158,7 +158,7 @@ invalid.jsx:8:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━
9 │ var a = <span aria-labelledby={``}></span>;
10 │ var a = <span aria-labelledby={""}></span>;

i The only supported value is text.
i The only supported value is a space-separated list of HTML identifiers.


```
Expand All @@ -175,7 +175,7 @@ invalid.jsx:9:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━
10 │ var a = <span aria-labelledby={""}></span>;
11 │ var a = <span aria-details=""></span>;

i The only supported value is text.
i The only supported value is a space-separated list of HTML identifiers.


```
Expand All @@ -192,7 +192,7 @@ invalid.jsx:10:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━
11 │ var a = <span aria-details=""></span>;
12 │ var a = <span aria-setsize="hey"></span>;

i The only supported value is text.
i The only supported value is a space-separated list of HTML identifiers.


```
Expand All @@ -209,7 +209,7 @@ invalid.jsx:11:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━
12 │ var a = <span aria-setsize="hey"></span>;
13 │ var a = <span aria-valuemax="hey"></span>;

i The only supported value is text.
i The only supported value is an HTML identifier.


```
Expand Down Expand Up @@ -291,5 +291,3 @@ invalid.jsx:15:15 lint/a11y/useValidAriaValues ━━━━━━━━━━━


```


Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var a = <span aria-invalid={false}></span>;
var a = <span role="checkbox" aria-errormessage="someid" ></span>;
var a = <span role="checkbox" aria-relevant="additions" ></span>;
var a = <span role="checkbox" aria-relevant="additions all" ></span>;
var a = <span role="checkbox" aria-relevant=" additions all " ></span>;
var a = <span aria-labelledby="id" ></span>;
var a = <span aria-labelledby="fooId barId" ></span>;
var a = <span aria-details="someid" ></span>;
var a = <button type="button" aria-keyshortcuts="1">Click Me</button>;
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ var a = <span aria-invalid={false}></span>;
var a = <span role="checkbox" aria-errormessage="someid" ></span>;
var a = <span role="checkbox" aria-relevant="additions" ></span>;
var a = <span role="checkbox" aria-relevant="additions all" ></span>;
var a = <span role="checkbox" aria-relevant=" additions all " ></span>;
var a = <span aria-labelledby="id" ></span>;
var a = <span aria-labelledby="fooId barId" ></span>;
var a = <span aria-details="someid" ></span>;
var a = <button type="button" aria-keyshortcuts="1">Click Me</button>;

```


Loading