Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
notmd committed Dec 7, 2022
1 parent 9110f85 commit 3574307
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 146 deletions.
3 changes: 2 additions & 1 deletion crates/rome_js_analyze/src/analyzers/nursery.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use rome_console::markup;
use rome_js_syntax::jsx_ext::AnyJsxElement;
use rome_js_syntax::{
AnyJsExpression, AnyJsLiteralExpression, AnyJsTemplateElement, AnyJsxAttributeValue,
JsSyntaxToken,
};
use rome_rowan::AstNode;

Expand Down Expand Up @@ -55,34 +54,29 @@ impl Rule for NoRedundantAlt {

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
if node
.name()
.ok()?
.as_jsx_name()?
.value_token()
.ok()?
.text_trimmed()
!= "img"
{
if node.name_value_token()?.text_trimmed() != "img" {
return None;
}
let hidden = node.find_attribute_by_name("aria-hidden");
let aria_hidden_attribute = node.find_attribute_by_name("aria-hidden");
if let Some(aria_hidden) = aria_hidden_attribute {
let is_false = match aria_hidden.initializer()?.value().ok()? {
AnyJsxAttributeValue::AnyJsxTag(_) => false,
AnyJsxAttributeValue::JsxExpressionAttributeValue(aria_hidden) => {
aria_hidden
.expression()
.ok()?
.as_any_js_literal_expression()?
.as_js_boolean_literal_expression()?
.value_token()
.ok()?
.text_trimmed()
== "false"
}
AnyJsxAttributeValue::JsxString(aria_hidden) => {
aria_hidden.inner_string_text().ok()? == "false"
}
};

if hidden.is_some() {
let is_false = hidden
.unwrap()
.initializer()?
.value()
.ok()?
.as_jsx_expression_attribute_value()?
.expression()
.ok()?
.as_any_js_literal_expression()?
.as_js_boolean_literal_expression()?
.value_token()
.ok()?
.text_trimmed()
== "false";
if !is_false {
return None;
}
Expand All @@ -101,35 +95,30 @@ impl Rule for NoRedundantAlt {
AnyJsExpression::AnyJsLiteralExpression(
AnyJsLiteralExpression::JsStringLiteralExpression(expr),
) => {
let token = expr.value_token().ok()?;

is_redundant_alt(trim_quote(&token)).map(|_| alt)
is_redundant_alt(expr.inner_string_text().ok()?.to_string()).then_some(alt)
}
AnyJsExpression::JsTemplateExpression(expr) => {
let contain_redundant_alt = expr.elements().into_iter().any(|x| match x {
AnyJsTemplateElement::JsTemplateChunkElement(node) => {
node.template_chunk_token().ok().map_or(false, |token| {
is_redundant_alt(token.text_trimmed().to_string())
.map(|_| true)
.unwrap_or(false)
})
}
AnyJsTemplateElement::JsTemplateElement(_) => false,
});
let contain_redundant_alt =
expr.elements().into_iter().any(|template_element| {
match template_element {
AnyJsTemplateElement::JsTemplateChunkElement(node) => {
node.template_chunk_token().ok().map_or(false, |token| {
is_redundant_alt(token.text_trimmed().to_string())
})
}
AnyJsTemplateElement::JsTemplateElement(_) => false,
}
});

if contain_redundant_alt {
Some(alt)
} else {
None
}
contain_redundant_alt.then_some(alt)
}

_ => None,
}
}
AnyJsxAttributeValue::JsxString(ref value) => {
let text = value.inner_string_text().ok()?.to_string();
is_redundant_alt(text).map(|_| alt)
is_redundant_alt(text).then_some(alt)
}
}
}
Expand All @@ -152,20 +141,8 @@ impl Rule for NoRedundantAlt {

const REDUNDANT_WORDS: [&str; 3] = ["image", "photo", "picture"];

fn is_redundant_alt(alt: String) -> Option<()> {
let is_redundant = REDUNDANT_WORDS
fn is_redundant_alt(alt: String) -> bool {
REDUNDANT_WORDS
.into_iter()
.any(|word| alt.split_whitespace().any(|x| x.to_lowercase() == word));

if is_redundant {
Some(())
} else {
None
}
}

fn trim_quote(token: &JsSyntaxToken) -> String {
let trimmed_string = token.text_trimmed().to_string();

trimmed_string[1..trimmed_string.len() - 1].to_string()
.any(|word| alt.split_whitespace().any(|x| x.to_lowercase() == word))
}
3 changes: 1 addition & 2 deletions crates/rome_js_analyze/src/aria_analyzers/nursery.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Generated file, do not edit by hand, see `xtask/codegen`

use rome_analyze::declare_group;
mod no_redundant_alt;
mod use_aria_prop_types;
mod use_aria_props_for_role;
declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: no_redundant_alt :: NoRedundantAlt , self :: use_aria_prop_types :: UseAriaPropTypes , self :: use_aria_props_for_role :: UseAriaPropsForRole ,] } }
declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: use_aria_prop_types :: UseAriaPropTypes , self :: use_aria_props_for_role :: UseAriaPropsForRole ,] } }
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let a = <img alt="imAGE of friend." />;
let a = <img alt="photo of cool person" aria-hidden={false} />;
let a = <img alt="picture of cool person" aria-hidden={false} />;
let a = <img alt="image of cool person" aria-hidden={false} />;
let a = <img alt="image of cool person" aria-hidden="false" />;
let a = <img alt="photo" {...this.props} />;
let a = <img alt="image" {...this.props} />;
let a = <img alt="picture" {...this.props} />;
Expand Down
Loading

0 comments on commit 3574307

Please sign in to comment.