Skip to content

Commit

Permalink
fix(linter/jsx-a11y): reduce false negatives for html-has-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 12, 2024
1 parent f45ee7f commit 1f3b143
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
26 changes: 22 additions & 4 deletions crates/oxc_linter/src/rules/jsx_a11y/html_has_lang.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use oxc_ast::{
ast::{JSXAttributeItem, JSXAttributeValue, JSXElementName},
ast::{JSXAttributeItem, JSXAttributeValue, JSXElementName, JSXExpression},
AstKind,
};
use oxc_diagnostics::OxcDiagnostic;
Expand Down Expand Up @@ -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,
}
Expand All @@ -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),
Expand All @@ -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),
];
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/no_redundant_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ declare_oxc_lint!(
/// <nav />
/// ```
NoRedundantRoles,
correctness
correctness,
pending
);

static DEFAULT_ROLE_EXCEPTIONS: phf::Map<&'static str, &'static str> = phf_map! {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ declare_oxc_lint!(
/// <th scope={scope} />
/// ```
Scope,
correctness
correctness,
pending
);

impl Rule for Scope {
Expand Down
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/jsx_a11y/tabindex_no_positive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ declare_oxc_lint!(
/// <span tabIndex="-1">bar</span>
/// ```
TabindexNoPositive,
correctness
correctness,
pending
);

impl Rule for TabindexNoPositive {
Expand Down
35 changes: 35 additions & 0 deletions crates/oxc_linter/src/snapshots/html_has_lang.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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="" />;
Expand Down

0 comments on commit 1f3b143

Please sign in to comment.