From 3574307c0fcb6bdee4765c984fde30d276a72d16 Mon Sep 17 00:00:00 2001 From: notmd Date: Wed, 7 Dec 2022 20:42:50 +0700 Subject: [PATCH] code review --- .../rome_js_analyze/src/analyzers/nursery.rs | 3 +- .../nursery/no_redundant_alt.rs | 97 +++++------ .../src/aria_analyzers/nursery.rs | 3 +- .../specs/nursery/noRedundantAlt/invalid.jsx | 1 + .../nursery/noRedundantAlt/invalid.jsx.snap | 156 ++++++++++-------- .../specs/nursery/noRedundantAlt/valid.jsx | 1 + .../nursery/noRedundantAlt/valid.jsx.snap | 1 + .../src/configuration/linter/rules.rs | 2 +- editors/vscode/configuration_schema.json | 2 +- npm/backend-jsonrpc/src/workspace.ts | 2 +- npm/rome/configuration_schema.json | 2 +- website/src/pages/lint/rules/index.mdx | 2 - .../src/pages/lint/rules/noRedundantAlt.md | 19 ++- 13 files changed, 145 insertions(+), 146 deletions(-) rename crates/rome_js_analyze/src/{aria_analyzers => analyzers}/nursery/no_redundant_alt.rs (61%) diff --git a/crates/rome_js_analyze/src/analyzers/nursery.rs b/crates/rome_js_analyze/src/analyzers/nursery.rs index 5b280382161..9c35ae21049 100644 --- a/crates/rome_js_analyze/src/analyzers/nursery.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery.rs @@ -14,6 +14,7 @@ mod no_header_scope; mod no_invalid_constructor_super; mod no_non_null_assertion; mod no_precision_loss; +mod no_redundant_alt; mod no_redundant_use_strict; mod no_setter_return; mod no_string_case_mismatch; @@ -24,4 +25,4 @@ mod use_default_switch_clause_last; mod use_enum_initializers; mod use_exponentiation_operator; mod use_numeric_literals; -declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: no_access_key :: NoAccessKey , self :: no_banned_types :: NoBannedTypes , self :: no_conditional_assignment :: NoConditionalAssignment , self :: no_const_enum :: NoConstEnum , self :: no_constructor_return :: NoConstructorReturn , self :: no_distracting_elements :: NoDistractingElements , self :: no_duplicate_object_keys :: NoDuplicateObjectKeys , self :: no_empty_interface :: NoEmptyInterface , self :: no_extra_non_null_assertion :: NoExtraNonNullAssertion , self :: no_header_scope :: NoHeaderScope , self :: no_invalid_constructor_super :: NoInvalidConstructorSuper , self :: no_non_null_assertion :: NoNonNullAssertion , self :: no_precision_loss :: NoPrecisionLoss , self :: no_redundant_use_strict :: NoRedundantUseStrict , self :: no_setter_return :: NoSetterReturn , self :: no_string_case_mismatch :: NoStringCaseMismatch , self :: no_unsafe_finally :: NoUnsafeFinally , self :: no_void_type_return :: NoVoidTypeReturn , self :: use_default_parameter_last :: UseDefaultParameterLast , self :: use_default_switch_clause_last :: UseDefaultSwitchClauseLast , self :: use_enum_initializers :: UseEnumInitializers , self :: use_exponentiation_operator :: UseExponentiationOperator , self :: use_numeric_literals :: UseNumericLiterals ,] } } +declare_group! { pub (crate) Nursery { name : "nursery" , rules : [self :: no_access_key :: NoAccessKey , self :: no_banned_types :: NoBannedTypes , self :: no_conditional_assignment :: NoConditionalAssignment , self :: no_const_enum :: NoConstEnum , self :: no_constructor_return :: NoConstructorReturn , self :: no_distracting_elements :: NoDistractingElements , self :: no_duplicate_object_keys :: NoDuplicateObjectKeys , self :: no_empty_interface :: NoEmptyInterface , self :: no_extra_non_null_assertion :: NoExtraNonNullAssertion , self :: no_header_scope :: NoHeaderScope , self :: no_invalid_constructor_super :: NoInvalidConstructorSuper , self :: no_non_null_assertion :: NoNonNullAssertion , self :: no_precision_loss :: NoPrecisionLoss , self :: no_redundant_alt :: NoRedundantAlt , self :: no_redundant_use_strict :: NoRedundantUseStrict , self :: no_setter_return :: NoSetterReturn , self :: no_string_case_mismatch :: NoStringCaseMismatch , self :: no_unsafe_finally :: NoUnsafeFinally , self :: no_void_type_return :: NoVoidTypeReturn , self :: use_default_parameter_last :: UseDefaultParameterLast , self :: use_default_switch_clause_last :: UseDefaultSwitchClauseLast , self :: use_enum_initializers :: UseEnumInitializers , self :: use_exponentiation_operator :: UseExponentiationOperator , self :: use_numeric_literals :: UseNumericLiterals ,] } } diff --git a/crates/rome_js_analyze/src/aria_analyzers/nursery/no_redundant_alt.rs b/crates/rome_js_analyze/src/analyzers/nursery/no_redundant_alt.rs similarity index 61% rename from crates/rome_js_analyze/src/aria_analyzers/nursery/no_redundant_alt.rs rename to crates/rome_js_analyze/src/analyzers/nursery/no_redundant_alt.rs index df188a5bcef..6ef9abef814 100644 --- a/crates/rome_js_analyze/src/aria_analyzers/nursery/no_redundant_alt.rs +++ b/crates/rome_js_analyze/src/analyzers/nursery/no_redundant_alt.rs @@ -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; @@ -55,34 +54,29 @@ impl Rule for NoRedundantAlt { fn run(ctx: &RuleContext) -> 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; } @@ -101,27 +95,22 @@ 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, @@ -129,7 +118,7 @@ impl Rule for NoRedundantAlt { } 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) } } } @@ -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)) } diff --git a/crates/rome_js_analyze/src/aria_analyzers/nursery.rs b/crates/rome_js_analyze/src/aria_analyzers/nursery.rs index 434f0e8b26e..2653da16241 100644 --- a/crates/rome_js_analyze/src/aria_analyzers/nursery.rs +++ b/crates/rome_js_analyze/src/aria_analyzers/nursery.rs @@ -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 ,] } } diff --git a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx index 12d8776f341..b51aed50663 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx +++ b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx @@ -8,6 +8,7 @@ let a = imAGE of friend.; let a = photo of cool person; let a = picture of cool person; let a = image of cool person; +let a = image of cool person; let a = photo; let a = image; let a = picture; diff --git a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx.snap b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx.snap index 32dc67fa5ce..d68eb673c67 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/invalid.jsx.snap @@ -14,6 +14,7 @@ let a = imAGE of friend.; let a = photo of cool person; let a = picture of cool person; let a = image of cool person; +let a = image of cool person; let a = photo; let a = image; let a = picture; @@ -30,7 +31,7 @@ let a = {`image; ``` invalid.jsx:1:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. > 1 │ let a = Photo of friend.; │ ^^^^^^^^^^^^^^^^^^ @@ -45,7 +46,7 @@ invalid.jsx:1:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:2:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 1 │ let a = Photo of friend.; > 2 │ let a = Picture of friend.; @@ -61,7 +62,7 @@ invalid.jsx:2:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:3:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 1 │ let a = Photo of friend.; 2 │ let a = Picture of friend.; @@ -78,7 +79,7 @@ invalid.jsx:3:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:4:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 2 │ let a = Picture of friend.; 3 │ let a = Image of friend.; @@ -95,7 +96,7 @@ invalid.jsx:4:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:5:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 3 │ let a = Image of friend.; 4 │ let a = PhOtO of friend.; @@ -112,7 +113,7 @@ invalid.jsx:5:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:6:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 4 │ let a = PhOtO of friend.; 5 │ let a = {"photo"}; @@ -129,7 +130,7 @@ invalid.jsx:6:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:7:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 5 │ let a = {"photo"}; 6 │ let a = piCTUre of friend.; @@ -146,7 +147,7 @@ invalid.jsx:7:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:8:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 6 │ let a = piCTUre of friend.; 7 │ let a = imAGE of friend.; @@ -163,14 +164,14 @@ invalid.jsx:8:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:9:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 7 │ let a = imAGE of friend.; 8 │ let a = photo of cool person; > 9 │ let a = picture of cool person; │ ^^^^^^^^^^^^^^^^^^^^^^^^ 10 │ let a = image of cool person; - 11 │ let a = photo; + 11 │ let a = image of cool person; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -180,14 +181,14 @@ invalid.jsx:9:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:10:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 8 │ let a = photo of cool person; 9 │ let a = picture of cool person; > 10 │ let a = image of cool person; │ ^^^^^^^^^^^^^^^^^^^^^^ - 11 │ let a = photo; - 12 │ let a = image; + 11 │ let a = image of cool person; + 12 │ let a = photo; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -197,14 +198,14 @@ invalid.jsx:10:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:11:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 9 │ let a = picture of cool person; 10 │ let a = image of cool person; - > 11 │ let a = photo; - │ ^^^^^^^ - 12 │ let a = image; - 13 │ let a = picture; + > 11 │ let a = image of cool person; + │ ^^^^^^^^^^^^^^^^^^^^^^ + 12 │ let a = photo; + 13 │ let a = image; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -214,14 +215,14 @@ invalid.jsx:11:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:12:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. 10 │ let a = image of cool person; - 11 │ let a = photo; - > 12 │ let a = image; + 11 │ let a = image of cool person; + > 12 │ let a = photo; │ ^^^^^^^ - 13 │ let a = picture; - 14 │ let a = {`picture; + 13 │ let a = image; + 14 │ let a = picture; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -231,14 +232,14 @@ invalid.jsx:12:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:13:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 11 │ let a = photo; - 12 │ let a = image; - > 13 │ let a = picture; - │ ^^^^^^^^^ - 14 │ let a = {`picture; - 15 │ let a = {`photo; + 11 │ let a = image of cool person; + 12 │ let a = photo; + > 13 │ let a = image; + │ ^^^^^^^ + 14 │ let a = picture; + 15 │ let a = {`picture; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -248,14 +249,14 @@ invalid.jsx:13:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:14:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 12 │ let a = image; - 13 │ let a = picture; - > 14 │ let a = {`picture; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - 15 │ let a = {`photo; - 16 │ let a = {`image; + 12 │ let a = photo; + 13 │ let a = image; + > 14 │ let a = picture; + │ ^^^^^^^^^ + 15 │ let a = {`picture; + 16 │ let a = {`photo; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -265,14 +266,14 @@ invalid.jsx:14:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:15:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 13 │ let a = picture; - 14 │ let a = {`picture; - > 15 │ let a = {`photo; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^ - 16 │ let a = {`image; - 17 │ let a = {`picture; + 13 │ let a = image; + 14 │ let a = picture; + > 15 │ let a = {`picture; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 16 │ let a = {`photo; + 17 │ let a = {`image; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -282,14 +283,14 @@ invalid.jsx:15:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:16:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 14 │ let a = {`picture; - 15 │ let a = {`photo; - > 16 │ let a = {`image; + 14 │ let a = picture; + 15 │ let a = {`picture; + > 16 │ let a = {`photo; │ ^^^^^^^^^^^^^^^^^^^^^^^^^ - 17 │ let a = {`picture; - 18 │ let a = {`photo; + 17 │ let a = {`image; + 18 │ let a = {`picture; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -299,14 +300,14 @@ invalid.jsx:16:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:17:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 15 │ let a = {`photo; - 16 │ let a = {`image; - > 17 │ let a = {`picture; - │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - 18 │ let a = {`photo; - 19 │ let a = {`image; + 15 │ let a = {`picture; + 16 │ let a = {`photo; + > 17 │ let a = {`image; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^ + 18 │ let a = {`picture; + 19 │ let a = {`photo; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -316,14 +317,14 @@ invalid.jsx:17:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:18:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 16 │ let a = {`image; - 17 │ let a = {`picture; - > 18 │ let a = {`photo; - │ ^^^^^^^^^^^^^^^^^^^^^^^^ - 19 │ let a = {`image; - 20 │ + 16 │ let a = {`photo; + 17 │ let a = {`image; + > 18 │ let a = {`picture; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + 19 │ let a = {`photo; + 20 │ let a = {`image; i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. @@ -333,13 +334,30 @@ invalid.jsx:18:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━ ``` invalid.jsx:19:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Avoid the words "image", "picture", or "photo" inimg element alt text. + ! Avoid the words "image", "picture", or "photo" in img element alt text. + + 17 │ let a = {`image; + 18 │ let a = {`picture; + > 19 │ let a = {`photo; + │ ^^^^^^^^^^^^^^^^^^^^^^^^ + 20 │ let a = {`image; + 21 │ + + i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. + + +``` + +``` +invalid.jsx:20:18 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Avoid the words "image", "picture", or "photo" in img element alt text. - 17 │ let a = {`picture; - 18 │ let a = {`photo; - > 19 │ let a = {`image; + 18 │ let a = {`picture; + 19 │ let a = {`photo; + > 20 │ let a = {`image; │ ^^^^^^^^^^^^^^^^^^^^^^^^ - 20 │ + 21 │ i Screen readers announce img elements as "images", so it is not necessary to redeclare this in alternative text. diff --git a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx index 79f873dcdb9..24ed8a446ce 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx +++ b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx @@ -1,6 +1,7 @@ let a = foo; let a = picture of me taking a photo of an image; let a = photo of image; +let a = ; let a = foo; let a = foo; let a = {"foo"}; diff --git a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx.snap b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx.snap index 3d14e974549..8a87ae75cb9 100644 --- a/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx.snap +++ b/crates/rome_js_analyze/tests/specs/nursery/noRedundantAlt/valid.jsx.snap @@ -7,6 +7,7 @@ expression: valid.jsx let a = foo; let a = picture of me taking a photo of an image; let a = photo of image; +let a = ; let a = foo; let a = foo; let a = {"foo"}; diff --git a/crates/rome_service/src/configuration/linter/rules.rs b/crates/rome_service/src/configuration/linter/rules.rs index f2f55a37613..3143f77f005 100644 --- a/crates/rome_service/src/configuration/linter/rules.rs +++ b/crates/rome_service/src/configuration/linter/rules.rs @@ -749,7 +749,7 @@ struct NurserySchema { no_non_null_assertion: Option, #[doc = "Disallow literal numbers that lose precision"] no_precision_loss: Option, - #[doc = "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\". The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed."] + #[doc = "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\"."] no_redundant_alt: Option, #[doc = "Prevents from having redundant \"use strict\"."] no_redundant_use_strict: Option, diff --git a/editors/vscode/configuration_schema.json b/editors/vscode/configuration_schema.json index f383f17e256..9184638d564 100644 --- a/editors/vscode/configuration_schema.json +++ b/editors/vscode/configuration_schema.json @@ -721,7 +721,7 @@ ] }, "noRedundantAlt": { - "description": "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\". The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed.", + "description": "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\".", "anyOf": [ { "$ref": "#/definitions/RuleConfiguration" diff --git a/npm/backend-jsonrpc/src/workspace.ts b/npm/backend-jsonrpc/src/workspace.ts index 262539c14e8..4d37342c610 100644 --- a/npm/backend-jsonrpc/src/workspace.ts +++ b/npm/backend-jsonrpc/src/workspace.ts @@ -338,7 +338,7 @@ export interface Nursery { */ noPrecisionLoss?: RuleConfiguration; /** - * Enforce img alt prop does not contain the word "image", "picture", or "photo". The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed. + * Enforce img alt prop does not contain the word "image", "picture", or "photo". */ noRedundantAlt?: RuleConfiguration; /** diff --git a/npm/rome/configuration_schema.json b/npm/rome/configuration_schema.json index f383f17e256..9184638d564 100644 --- a/npm/rome/configuration_schema.json +++ b/npm/rome/configuration_schema.json @@ -721,7 +721,7 @@ ] }, "noRedundantAlt": { - "description": "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\". The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is hidden, then rule will always succeed.", + "description": "Enforce img alt prop does not contain the word \"image\", \"picture\", or \"photo\".", "anyOf": [ { "$ref": "#/definitions/RuleConfiguration" diff --git a/website/src/pages/lint/rules/index.mdx b/website/src/pages/lint/rules/index.mdx index 4489bf70ce5..c32c1b95559 100644 --- a/website/src/pages/lint/rules/index.mdx +++ b/website/src/pages/lint/rules/index.mdx @@ -558,8 +558,6 @@ Disallow literal numbers that lose precision noRedundantAlt Enforce img alt prop does not contain the word "image", "picture", or "photo". -The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is -hidden, then rule will always succeed.

diff --git a/website/src/pages/lint/rules/noRedundantAlt.md b/website/src/pages/lint/rules/noRedundantAlt.md index 24fdb48075f..8d2bb4a4615 100644 --- a/website/src/pages/lint/rules/noRedundantAlt.md +++ b/website/src/pages/lint/rules/noRedundantAlt.md @@ -6,8 +6,9 @@ parent: lint/rules/index # noRedundantAlt (since v12.0.0) Enforce `img` alt prop does not contain the word "image", "picture", or "photo". -The rule will first check if aria-hidden is true to determine whether to enforce the rule. If the image is -hidden, then rule will always succeed. + +The rule will first check if `aria-hidden` is truthy to determine whether to enforce the rule. If the image is +hidden, then the rule will always succeed. ## Examples @@ -19,7 +20,7 @@ hidden, then rule will always succeed.
nursery/noRedundantAlt.js:1:20 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
-   Avoid the words "image", "picture", or "photo" inimg element alt text.
+   Avoid the words "image", "picture", or "photo" in img element alt text.
   
   > 1 │ <img src="src" alt="photo content" />;
                       ^^^^^^^^^^^^^^^
@@ -35,7 +36,7 @@ hidden, then rule will always succeed.
 
 
nursery/noRedundantAlt.js:1:10 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
-   Avoid the words "image", "picture", or "photo" inimg element alt text.
+   Avoid the words "image", "picture", or "photo" in img element alt text.
   
   > 1 │ <img alt={`picture doing ${things}`} {...this.props} />;
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -51,7 +52,7 @@ hidden, then rule will always succeed.
 
 
nursery/noRedundantAlt.js:1:10 lint/nursery/noRedundantAlt ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
-   Avoid the words "image", "picture", or "photo" inimg element alt text.
+   Avoid the words "image", "picture", or "photo" in img element alt text.
   
   > 1 │ <img alt="picture of cool person" aria-hidden={false} />;
             ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -64,8 +65,10 @@ hidden, then rule will always succeed.
 ### Valid
 
 ```jsx
-alt;
-{photo};
-Picture of me taking a photo of an image;
+<>
+	alt
+	{photo}
+	Picture of me taking a photo of an image
+
 ```