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";", None),
- (r"", None),
- (r"", None),
- (r";", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r" {}} />", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"test", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r";", None),
- (r";", None),
- (r"", None),
- // TODO we need components_settings to test this
- // (r"", settings: Some(components_settings))
+ (r";", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r";", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r" {}} />", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"test", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r"", None, None),
+ (r"", None, Some(settings())),
];
let fail = vec![
- (r";", None),
- (r";", None),
- (r";", None),
- (r";", None),
- (r";", None),
- (r";", None),
- (r";", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- (r"", None),
- // TODO we need components_settings to test this
- // (r"", Some(components_settings),
-
+ (r";", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r";", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, None),
+ (r"", None, Some(settings())),
// TESTS FOR ARRAY OPTION TESTS
- (r";", Some(array())),
- (r";", Some(array())),
- (r";", Some(array())),
- (r";", Some(array())),
+ (r";", Some(array()), None),
+ (r";", Some(array()), None),
+ (r";", Some(array()), None),
+ (r";", 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 │
+ · ───
+ ╰────
+ 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 │ ;