This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 650
feat(rome_js_analyzer): add noPositiveTabindex
rule
#3336
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2f7b1a8
initial rule implementation
kaioduarte a619ede
add tests/snapshots
kaioduarte a7fcdfb
chore: update snapshots
kaioduarte 42e5ba1
chore: clean up code
kaioduarte 7db415a
chore: update doc string
kaioduarte d9a7dbf
chore: add more valid cases
kaioduarte 3792124
chore: add missing config for the rule
kaioduarte 9c96054
chore: update tests and snapshots
kaioduarte 092b741
chore: handle unary expression
kaioduarte cab94ac
fix: lint issues
kaioduarte b4231ca
chore: add missing codegen files
kaioduarte c041434
chore: address PR review
kaioduarte 3f7b93b
chore: update tests/snapshots
kaioduarte 4639066
Update crates/rome_js_analyze/src/semantic_analyzers/nursery/no_posit…
kaioduarte 8249ac2
Update crates/rome_js_analyze/src/semantic_analyzers/nursery/no_posit…
kaioduarte ceabdcf
Update crates/rome_js_analyze/src/semantic_analyzers/nursery/no_posit…
kaioduarte 697b254
Update crates/rome_js_analyze/src/semantic_analyzers/nursery/no_posit…
kaioduarte 2dc5dcc
fix: show diagnostic on the property value
kaioduarte b2da645
chore: update snapshots to match with diagnostic changes
kaioduarte 50b9cee
chore: update doc files
kaioduarte 2e2ea17
chore: add Options to rule
kaioduarte 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
198 changes: 198 additions & 0 deletions
198
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_positive_tabindex.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,198 @@ | ||
use crate::react::{ReactApiCall, ReactCreateElementCall}; | ||
use crate::semantic_services::Semantic; | ||
use rome_analyze::context::RuleContext; | ||
use rome_analyze::{declare_rule, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_diagnostics::Severity; | ||
use rome_js_semantic::SemanticModel; | ||
use rome_js_syntax::{ | ||
JsCallExpression, JsNumberLiteralExpression, JsPropertyObjectMember, JsStringLiteralExpression, | ||
JsUnaryExpression, JsxAnyAttributeValue, JsxAttribute, JsxOpeningElement, | ||
JsxSelfClosingElement, TextRange, | ||
}; | ||
use rome_rowan::{declare_node_union, AstNode}; | ||
|
||
declare_rule! { | ||
/// Prevent the usage of positive integers on `tabIndex` property | ||
/// | ||
/// Avoid positive `tabIndex` property values to synchronize the flow of the page with keyboard tab order. | ||
/// ## Accessibility guidelines | ||
/// | ||
/// [WCAG 2.4.3](https://www.w3.org/WAI/WCAG21/Understanding/focus-order) | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <div tabIndex={1}>foo</div> | ||
/// ``` | ||
/// | ||
/// ```jsx,expect_diagnostic | ||
/// <div tabIndex={"1"} /> | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// React.createElement("div", { tabIndex: 1 }) | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```jsx | ||
/// <div tabIndex="0" /> | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// React.createElement("div", { tabIndex: -1 }) | ||
/// ``` | ||
pub(crate) NoPositiveTabindex { | ||
version: "0.10.0", | ||
name: "noPositiveTabindex", | ||
recommended: false, | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
pub(crate) TabindexProp = JsxAttribute | JsPropertyObjectMember | ||
} | ||
|
||
declare_node_union! { | ||
pub(crate) NoPositiveTabindexQuery = JsxOpeningElement | JsxSelfClosingElement | JsCallExpression | ||
} | ||
|
||
declare_node_union! { | ||
/// Subset of expressions supported by this rule. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// - `JsStringLiteralExpression` — `"5"` | ||
/// - `JsNumberLiteralExpression` — `5` | ||
/// - `JsUnaryExpression` — `+5` | `-5` | ||
/// | ||
pub(crate) AnyNumberLikeExpression = JsStringLiteralExpression | JsNumberLiteralExpression | JsUnaryExpression | ||
} | ||
|
||
impl NoPositiveTabindexQuery { | ||
fn find_tabindex_attribute(&self, model: &SemanticModel) -> Option<TabindexProp> { | ||
match self { | ||
NoPositiveTabindexQuery::JsxOpeningElement(jsx) => jsx | ||
.find_attribute_by_name("tabIndex") | ||
.ok()? | ||
.map(TabindexProp::from), | ||
NoPositiveTabindexQuery::JsxSelfClosingElement(jsx) => jsx | ||
.find_attribute_by_name("tabIndex") | ||
.ok()? | ||
.map(TabindexProp::from), | ||
NoPositiveTabindexQuery::JsCallExpression(expression) => { | ||
let react_create_element = | ||
ReactCreateElementCall::from_call_expression(expression, model)?; | ||
react_create_element | ||
.find_prop_by_name("tabIndex") | ||
.map(TabindexProp::from) | ||
} | ||
} | ||
} | ||
} | ||
|
||
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 NoPositiveTabindex { | ||
type Query = Semantic<NoPositiveTabindexQuery>; | ||
type State = TextRange; | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let model = ctx.model(); | ||
let tabindex_attribute = node.find_tabindex_attribute(model)?; | ||
|
||
match tabindex_attribute { | ||
TabindexProp::JsxAttribute(jsx_attribute) => { | ||
let jsx_any_attribute_value = jsx_attribute.initializer()?.value().ok()?; | ||
|
||
if !attribute_has_valid_tabindex(&jsx_any_attribute_value)? { | ||
return Some(jsx_any_attribute_value.syntax().text_trimmed_range()); | ||
} | ||
} | ||
TabindexProp::JsPropertyObjectMember(js_object_member) => { | ||
let expression = js_object_member.value().ok()?; | ||
let expression_syntax_node = expression.syntax(); | ||
let expression_value = | ||
AnyNumberLikeExpression::cast_ref(expression_syntax_node)?.value()?; | ||
|
||
if !is_tabindex_valid(&expression_value) { | ||
return Some(expression_syntax_node.text_trimmed_range()); | ||
} | ||
} | ||
} | ||
|
||
None | ||
} | ||
|
||
fn diagnostic(_ctx: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
let diagnostic = RuleDiagnostic::new( | ||
rule_category!(), | ||
state, | ||
markup!{"Avoid positive values for the "<Emphasis>"tabIndex"</Emphasis>" prop."}.to_owned(), | ||
) | ||
.footer( | ||
Severity::Note, | ||
markup!{ | ||
"Elements with a positive "<Emphasis>"tabIndex"</Emphasis>" override natural page content order. This causes elements without a positive tab index to come last when navigating using a keyboard." | ||
}.to_owned(), | ||
); | ||
|
||
Some(diagnostic) | ||
} | ||
} | ||
|
||
/// Verify that a JSX attribute value has a valid tab index, meaning it is not positive. | ||
fn attribute_has_valid_tabindex(jsx_any_attribute_value: &JsxAnyAttributeValue) -> Option<bool> { | ||
match jsx_any_attribute_value { | ||
JsxAnyAttributeValue::JsxString(jsx_string) => { | ||
let value = jsx_string.inner_string_text().ok()?.to_string(); | ||
Some(is_tabindex_valid(&value)) | ||
} | ||
JsxAnyAttributeValue::JsxExpressionAttributeValue(value) => { | ||
let expression = value.expression().ok()?; | ||
let expression_value = | ||
AnyNumberLikeExpression::cast_ref(expression.syntax())?.value()?; | ||
|
||
Some(is_tabindex_valid(&expression_value)) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
/// Verify if number string is an integer less than equal zero. Non-integer numbers | ||
/// are considered valid. | ||
fn is_tabindex_valid(number_like_string: &str) -> bool { | ||
let number_string_result = number_like_string.trim().parse::<i32>(); | ||
|
||
match number_string_result { | ||
Ok(number) => number <= 0, | ||
Err(_) => true, | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
crates/rome_js_analyze/tests/specs/nursery/noPositiveTabindex/invalidJsx.jsx
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 @@ | ||
<> | ||
<div tabIndex={1} /> | ||
<div tabIndex={"1"} /> | ||
<div tabIndex={'5'} /> | ||
<div tabIndex="1" /> | ||
<div tabIndex={1}>foo</div> | ||
<div tabIndex={"1"}>foo</div> | ||
<div tabIndex={'5'}>foo</div> | ||
<div tabIndex={+5}>foo</div> | ||
<div tabIndex={+05}>foo</div> | ||
<div tabIndex="1">foo</div> | ||
</> |
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.