From 2228aa8ced7394798419e2ff26691c4896029752 Mon Sep 17 00:00:00 2001 From: msdlisper <1170167213@qq.com> Date: Mon, 22 Jan 2024 02:31:20 +0800 Subject: [PATCH] fix(linter): jsx_a11y/img-redundant linter enable test case(#2112) Enables the case where `settings` is used. Part of #1141 --- .../src/rules/jsx_a11y/img_redundant_alt.rs | 142 ++++++++++-------- .../src/snapshots/img_redundant_alt.snap | 9 ++ 2 files changed, 85 insertions(+), 66 deletions(-) diff --git a/crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs b/crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs index e6b9362ca6db4..d296a6f69a3c0 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs @@ -2,8 +2,8 @@ use regex::Regex; use oxc_ast::{ ast::{ - Expression, JSXAttributeItem, JSXAttributeName, JSXAttributeValue, JSXElementName, - JSXExpression, JSXExpressionContainer, + Expression, JSXAttributeItem, JSXAttributeName, JSXAttributeValue, JSXExpression, + JSXExpressionContainer, }, AstKind, }; @@ -14,7 +14,9 @@ use oxc_diagnostics::{ use oxc_macros::declare_oxc_lint; use oxc_span::Span; -use crate::utils::{get_prop_value, has_jsx_prop_lowercase, is_hidden_from_screen_reader}; +use crate::utils::{ + get_element_type, get_prop_value, has_jsx_prop_lowercase, is_hidden_from_screen_reader, +}; use crate::{context::LintContext, rule::Rule, AstNode}; #[derive(Debug, Error, Diagnostic)] @@ -107,10 +109,11 @@ impl Rule for ImgRedundantAlt { } fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { let AstKind::JSXOpeningElement(jsx_el) = node.kind() else { return }; - let JSXElementName::Identifier(iden) = &jsx_el.name else { return }; - let name = iden.name.as_str(); + let Some(element_type) = get_element_type(ctx, jsx_el) else { + return; + }; - if !self.types_to_validate.iter().any(|comp| comp == name) { + if !self.types_to_validate.iter().any(|comp| comp == &element_type) { return; } @@ -196,71 +199,78 @@ fn test() { }]) } + fn settings() -> serde_json::Value { + serde_json::json!({ + "jsx-a11y": { + "components": { + "Image": "img", + } + } + }) + } + let pass = vec![ - (r"foo;", None), - (r"picture of me taking a photo of an image", None), - (r"photo of image", None), - (r"foo;", None), - (r"foo", None), - (r"{'foo'}", None), - (r"{alt}", None), - (r"", None), - (r"", None), - (r"", None), - (r"{undefined}", None), - (r"{`this", None), - (r"{`this", None), - (r"{`this", None), - (r"{`this", None), - (r"{`${photo}`}", None), - (r"{`${image}`}", None), - (r"{`${picture}`}", None), - (r"{'undefined'}", None), - (r"{() {}} />", None), - (r"{function(e){}}", None), - (r"Doing cool things.", None), - (r"photo of cool person", None), - (r"test", None), - (r"", None), - (r"{imageAlt}", None), - (r"{imageAlt.name}", None), - (r"{imageAlt?.name}", None), - (r"Doing cool things", None), - (r"Photography;", None), - (r"ImageMagick;", None), - (r"Photo of a friend", None), - // TODO we need components_settings to test this - // (r"Foo", settings: Some(components_settings)) + (r"foo;", None, None), + (r"picture of me taking a photo of an image", None, None), + (r"photo of image", None, None), + (r"foo;", None, None), + (r"foo", None, None), + (r"{'foo'}", None, None), + (r"{alt}", None, None), + (r"", None, None), + (r"", None, None), + (r"", None, None), + (r"{undefined}", None, None), + (r"{`this", None, None), + (r"{`this", None, None), + (r"{`this", None, None), + (r"{`this", None, None), + (r"{`${photo}`}", None, None), + (r"{`${image}`}", None, None), + (r"{`${picture}`}", None, None), + (r"{'undefined'}", None, None), + (r"{() {}} />", None, None), + (r"{function(e){}}", None, None), + (r"Doing cool things.", None, None), + (r"photo of cool person", None, None), + (r"test", None, None), + (r"", None, None), + (r"{imageAlt}", None, None), + (r"{imageAlt.name}", None, None), + (r"{imageAlt?.name}", None, None), + (r"Doing cool things", None, None), + (r"Photography;", None, None), + (r"ImageMagick;", None, None), + (r"Photo of a friend", None, None), + (r"Foo", None, Some(settings())), ]; let fail = vec![ - (r"Photo of friend.;", None), - (r"Picture of friend.;", None), - (r"Image of friend.;", None), - (r"PhOtO of friend.;", None), - (r"{'photo'};", None), - (r"piCTUre of friend.;", None), - (r"imAGE of friend.;", None), - (r"photo of cool person", None), - (r"picture of cool person", None), - (r"image of cool person", None), - (r"photo", None), - (r"image", None), - (r"picture", None), - (r"{`picture", None), - (r"{`photo", None), - (r"{`image", None), - (r"{`picture", None), - (r"{`photo", None), - (r"{`image", None), - // TODO we need components_settings to test this - // (r"Photo of a friend", Some(components_settings), - + (r"Photo of friend.;", None, None), + (r"Picture of friend.;", None, None), + (r"Image of friend.;", None, None), + (r"PhOtO of friend.;", None, None), + (r"{'photo'};", None, None), + (r"piCTUre of friend.;", None, None), + (r"imAGE of friend.;", None, None), + (r"photo of cool person", None, None), + (r"picture of cool person", None, None), + (r"image of cool person", None, None), + (r"photo", None, None), + (r"image", None, None), + (r"picture", None, None), + (r"{`picture", None, None), + (r"{`photo", None, None), + (r"{`image", None, None), + (r"{`picture", None, None), + (r"{`photo", None, None), + (r"{`image", None, None), + (r"Photo of a friend", None, Some(settings())), // TESTS FOR ARRAY OPTION TESTS - (r"Word1;", Some(array())), - (r"Word2;", Some(array())), - (r"Word1;", Some(array())), - (r"Word2;", Some(array())), + (r"Word1;", Some(array()), None), + (r"Word2;", Some(array()), None), + (r"Word1;", Some(array()), None), + (r"Word2;", Some(array()), None), ]; Tester::new(ImgRedundantAlt::NAME, pass, fail).with_jsx_a11y_plugin(true).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/img_redundant_alt.snap b/crates/oxc_linter/src/snapshots/img_redundant_alt.snap index 42030de5ce856..bd6a6819ea2cd 100644 --- a/crates/oxc_linter/src/snapshots/img_redundant_alt.snap +++ b/crates/oxc_linter/src/snapshots/img_redundant_alt.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_linter/src/tester.rs +assertion_line: 143 expression: img_redundant_alt --- ⚠ eslint-plugin-jsx-a11y(img-redundant-alt): Redundant alt attribute. @@ -154,6 +155,14 @@ expression: img_redundant_alt help: Provide no redundant alt text for image. Screen-readers already announce `img` tags as an image. You don’t need to use the words `image`, `photo,` or `picture` (or any specified custom words) in the alt prop. + ⚠ eslint-plugin-jsx-a11y(img-redundant-alt): Redundant alt attribute. + ╭─[img_redundant_alt.tsx:1:1] + 1 │ Photo of a friend + · ─── + ╰──── + help: Provide no redundant alt text for image. Screen-readers already announce `img` tags as an image. You don’t need to use the words `image`, `photo,` or `picture` (or any specified custom + words) in the alt prop. + ⚠ eslint-plugin-jsx-a11y(img-redundant-alt): Redundant alt attribute. ╭─[img_redundant_alt.tsx:1:1] 1 │ Word1;