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: noAriaHiddenonFocusable when using a JSX expression #3708

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, BatchMutationExt};
use biome_js_syntax::{
jsx_ext::AnyJsxElement, AnyJsxAttributeValue, JsNumberLiteralExpression,
JsStringLiteralExpression, JsUnaryExpression,
};
use biome_rowan::{declare_node_union, AstNode, BatchMutationExt};

declare_lint_rule! {
/// Enforce that aria-hidden="true" is not set on focusable elements.
Expand Down Expand Up @@ -33,6 +36,10 @@ declare_lint_rule! {
/// ```
///
/// ```jsx
/// <button aria-hidden="true" tabIndex={-1} />
/// ```
///
/// ```jsx
/// <div aria-hidden="true"><a href="#"></a></div>
/// ```
///
Expand All @@ -52,6 +59,40 @@ declare_lint_rule! {
}
}

declare_node_union! {
anthonyshew marked this conversation as resolved.
Show resolved Hide resolved
/// Subset of expressions supported by this rule.
///
/// ## Examples
///
/// - `JsStringLiteralExpression` &mdash; `"5"`
/// - `JsNumberLiteralExpression` &mdash; `5`
/// - `JsUnaryExpression` &mdash; `+5` | `-5`
///
pub AnyNumberLikeExpression = JsStringLiteralExpression | JsNumberLiteralExpression | JsUnaryExpression
}

impl AnyNumberLikeExpression {
/// Returns the value of a number-like expression; it returns the expression
/// text for literal expressions. However, for unary expressions, it only
/// returns the value for signed numeric expressions.
pub(crate) fn value(&self) -> Option<String> {
match self {
AnyNumberLikeExpression::JsStringLiteralExpression(string_literal) => {
return Some(string_literal.inner_string_text().ok()?.to_string());
}
AnyNumberLikeExpression::JsNumberLiteralExpression(number_literal) => {
return Some(number_literal.value_token().ok()?.to_string());
}
AnyNumberLikeExpression::JsUnaryExpression(unary_expression) => {
if unary_expression.is_signed_numeric_literal().ok()? {
return Some(unary_expression.text());
}
}
}
None
}
}

impl Rule for NoAriaHiddenOnFocusable {
type Query = Aria<AnyJsxElement>;
type State = ();
Expand All @@ -76,12 +117,34 @@ impl Rule for NoAriaHiddenOnFocusable {
}

if let Some(tabindex_attr) = node.find_attribute_by_name("tabIndex") {
if let Some(tabindex_static) = tabindex_attr.as_static_value() {
let tabindex_text = tabindex_static.text();
let tabindex_val = tabindex_text.trim().parse::<i32>();
let tabindex_val = tabindex_attr.initializer()?.value().ok()?;

if let Ok(num) = tabindex_val {
return (num >= 0).then_some(());
match tabindex_val {
AnyJsxAttributeValue::AnyJsxTag(jsx_tag) => {
let value = jsx_tag.text().parse::<i32>();
if let Ok(num) = value {
return (num >= 0).then_some(());
}
}
AnyJsxAttributeValue::JsxString(jsx_string) => {
let value = jsx_string
.inner_string_text()
.ok()?
.to_string()
.parse::<i32>();
if let Ok(num) = value {
return (num >= 0).then_some(());
}
}
AnyJsxAttributeValue::JsxExpressionAttributeValue(value) => {
let expression = value.expression().ok()?;
let expression_value =
AnyNumberLikeExpression::cast(expression.into_syntax())?
.value()?
.parse::<i32>();
if let Ok(num) = expression_value {
return (num >= 0).then_some(());
}
}
}
}
Expand All @@ -90,7 +153,6 @@ impl Rule for NoAriaHiddenOnFocusable {
return Some(());
}
}

None
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<img aria-hidden="true" />
<a aria-hidden="false" href="#" />
<button aria-hidden="true" tabIndex="-1" />
<button aria-hidden="true" tabIndex={-1} />
<a href="/" />
<div aria-hidden="true"><a href="#"></a></div>
<button />
</>
</>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
assertion_line: 86
expression: valid.jsx
---
# Input
Expand All @@ -9,10 +10,10 @@ expression: valid.jsx
<img aria-hidden="true" />
<a aria-hidden="false" href="#" />
<button aria-hidden="true" tabIndex="-1" />
<button aria-hidden="true" tabIndex={-1} />
<a href="/" />
<div aria-hidden="true"><a href="#"></a></div>
<button />
</>
```


```
Loading