mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
fix(linter/jsx-a11y): reduce false negatives for html-has-lang (#4855)
This commit is contained in:
parent
13c7b1b9d2
commit
a08d7a7f94
6 changed files with 65 additions and 8 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use oxc_ast::{
|
||||
ast::{JSXAttributeItem, JSXAttributeValue, JSXElementName},
|
||||
ast::{JSXAttributeItem, JSXAttributeValue, JSXElementName, JSXExpression},
|
||||
AstKind,
|
||||
};
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
|
|
@ -83,9 +83,19 @@ impl Rule for HtmlHasLang {
|
|||
|
||||
fn is_valid_lang_prop(item: &JSXAttributeItem) -> bool {
|
||||
match get_prop_value(item) {
|
||||
Some(JSXAttributeValue::ExpressionContainer(container)) => {
|
||||
!container.expression.is_expression() || !container.expression.is_undefined()
|
||||
}
|
||||
Some(JSXAttributeValue::ExpressionContainer(container)) => match &container.expression {
|
||||
JSXExpression::EmptyExpression(_)
|
||||
| JSXExpression::NullLiteral(_)
|
||||
| JSXExpression::BooleanLiteral(_)
|
||||
| JSXExpression::NumericLiteral(_) => false,
|
||||
JSXExpression::Identifier(id) => id.name != "undefined",
|
||||
JSXExpression::StringLiteral(str) => !str.value.as_str().is_empty(),
|
||||
JSXExpression::TemplateLiteral(t) => {
|
||||
!t.expressions.is_empty()
|
||||
|| t.quasis.iter().filter(|q| !q.value.raw.is_empty()).count() > 0
|
||||
}
|
||||
_ => true,
|
||||
},
|
||||
Some(JSXAttributeValue::StringLiteral(str)) => !str.value.as_str().is_empty(),
|
||||
_ => true,
|
||||
}
|
||||
|
|
@ -109,6 +119,9 @@ fn test() {
|
|||
(r"<div />;", None, None, None),
|
||||
(r#"<html lang="en" />"#, None, None, None),
|
||||
(r#"<html lang="en-US" />"#, None, None, None),
|
||||
(r#"<html lang={"en-US"} />"#, None, None, None),
|
||||
(r"<html lang={`en-US`} />", None, None, None),
|
||||
(r"<html lang={`${foo}`} />", None, None, None),
|
||||
(r"<html lang={foo} />;", None, None, None),
|
||||
(r"<html lang />;", None, None, None),
|
||||
(r"<HTML />;", None, None, None),
|
||||
|
|
@ -119,6 +132,11 @@ fn test() {
|
|||
(r"<html />;", None, None, None),
|
||||
(r"<html {...props} />;", None, None, None),
|
||||
(r"<html lang={undefined} />;", None, None, None),
|
||||
(r"<html lang={null} />;", None, None, None),
|
||||
(r"<html lang={false} />;", None, None, None),
|
||||
(r"<html lang={1} />;", None, None, None),
|
||||
(r"<html lang={''} />;", None, None, None),
|
||||
(r"<html lang={``} />;", None, None, None),
|
||||
(r#"<html lang="" />;"#, None, None, None),
|
||||
("<HTMLTop />", None, Some(settings()), None),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ use crate::{
|
|||
};
|
||||
|
||||
fn img_redundant_alt_diagnostic(span0: Span) -> OxcDiagnostic {
|
||||
OxcDiagnostic::warn("Redundant alt attribute.").with_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.").with_label(span0)
|
||||
OxcDiagnostic::warn("Redundant alt attribute.")
|
||||
.with_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.").with_label(span0)
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ declare_oxc_lint!(
|
|||
/// <nav />
|
||||
/// ```
|
||||
NoRedundantRoles,
|
||||
correctness
|
||||
correctness,
|
||||
pending
|
||||
);
|
||||
|
||||
static DEFAULT_ROLE_EXCEPTIONS: phf::Map<&'static str, &'static str> = phf_map! {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,8 @@ declare_oxc_lint!(
|
|||
/// <th scope={scope} />
|
||||
/// ```
|
||||
Scope,
|
||||
correctness
|
||||
correctness,
|
||||
pending
|
||||
);
|
||||
|
||||
impl Rule for Scope {
|
||||
|
|
|
|||
|
|
@ -42,7 +42,8 @@ declare_oxc_lint!(
|
|||
/// <span tabIndex="-1">bar</span>
|
||||
/// ```
|
||||
TabindexNoPositive,
|
||||
correctness
|
||||
correctness,
|
||||
pending
|
||||
);
|
||||
|
||||
impl Rule for TabindexNoPositive {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,41 @@ source: crates/oxc_linter/src/tester.rs
|
|||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang={null} />;
|
||||
· ────────────────────
|
||||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang={false} />;
|
||||
· ─────────────────────
|
||||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang={1} />;
|
||||
· ─────────────────
|
||||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang={''} />;
|
||||
· ──────────────────
|
||||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang={``} />;
|
||||
· ──────────────────
|
||||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing value for lang attribute
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <html lang="" />;
|
||||
|
|
|
|||
Loading…
Reference in a new issue