mirror of
https://github.com/danbulant/oxc
synced 2026-05-21 21:29:01 +00:00
feat(linter): use settings for eslint-plugin-jsx-a11y/html_has_lang (#1843)
Use eslint configuration settings e.g. defining a custom component `components` for eslint-plugin-jsx-a11y/html_has_lang rule
This commit is contained in:
parent
b9bdf361e6
commit
4c1673c2ca
2 changed files with 46 additions and 19 deletions
|
|
@ -11,7 +11,12 @@ use oxc_diagnostics::{
|
|||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
|
||||
use crate::{context::LintContext, rule::Rule, utils::has_jsx_prop_lowercase, AstNode};
|
||||
use crate::{
|
||||
context::LintContext,
|
||||
rule::Rule,
|
||||
utils::{get_element_type, has_jsx_prop_lowercase},
|
||||
AstNode,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error, Diagnostic)]
|
||||
enum HtmlHasLangDiagnostic {
|
||||
|
|
@ -64,15 +69,19 @@ impl Rule for HtmlHasLang {
|
|||
let AstKind::JSXOpeningElement(jsx_el) = node.kind() else {
|
||||
return;
|
||||
};
|
||||
let JSXElementName::Identifier(identifier) = &jsx_el.name else {
|
||||
|
||||
let Some(element_type) = get_element_type(ctx, jsx_el) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let name = identifier.name.as_str();
|
||||
if name != "html" {
|
||||
if element_type != "html" {
|
||||
return;
|
||||
}
|
||||
|
||||
let JSXElementName::Identifier(identifier) = &jsx_el.name else {
|
||||
return;
|
||||
};
|
||||
|
||||
has_jsx_prop_lowercase(jsx_el, "lang").map_or_else(
|
||||
|| ctx.diagnostic(HtmlHasLangDiagnostic::MissingLangProp(identifier.span)),
|
||||
|lang_prop| {
|
||||
|
|
@ -99,25 +108,35 @@ fn is_valid_lang_prop(item: &JSXAttributeItem) -> bool {
|
|||
fn test() {
|
||||
use crate::tester::Tester;
|
||||
|
||||
fn settings() -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"jsx-a11y": {
|
||||
"components": {
|
||||
"HTMLTop": "html",
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let pass = vec![
|
||||
(r"<div />;", None),
|
||||
(r#"<html lang="en" />"#, None),
|
||||
(r#"<html lang="en-US" />"#, None),
|
||||
(r"<html lang={foo} />;", None),
|
||||
(r"<html lang />;", None),
|
||||
(r"<HTML />;", None),
|
||||
// TODO: When polymorphic components are supported
|
||||
// (r#"<HTMLTop lang="en" />"#, None),
|
||||
(r"<div />;", None, None),
|
||||
(r#"<html lang="en" />"#, None, None),
|
||||
(r#"<html lang="en-US" />"#, None, None),
|
||||
(r"<html lang={foo} />;", None, None),
|
||||
(r"<html lang />;", None, None),
|
||||
(r"<HTML />;", None, None),
|
||||
("<HTMLTop lang='en' />", None, Some(settings())),
|
||||
];
|
||||
|
||||
let fail = vec![
|
||||
(r"<html />;", None),
|
||||
(r"<html {...props} />;", None),
|
||||
(r"<html lang={undefined} />;", None),
|
||||
(r#"<html lang="" />;"#, None),
|
||||
// TODO: When polymorphic components are supported
|
||||
// (r"<HTMLTop />;", None),
|
||||
(r"<html />;", None, None),
|
||||
(r"<html {...props} />;", None, None),
|
||||
(r"<html lang={undefined} />;", None, None),
|
||||
(r#"<html lang="" />;"#, None, None),
|
||||
("<HTMLTop />", None, Some(settings())),
|
||||
];
|
||||
|
||||
Tester::new(HtmlHasLang::NAME, pass, fail).with_jsx_a11y_plugin(true).test_and_snapshot();
|
||||
Tester::new_with_settings(HtmlHasLang::NAME, pass, fail)
|
||||
.with_jsx_a11y_plugin(true)
|
||||
.test_and_snapshot();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
---
|
||||
source: crates/oxc_linter/src/tester.rs
|
||||
assertion_line: 144
|
||||
expression: html_has_lang
|
||||
---
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing lang attribute.
|
||||
|
|
@ -30,4 +31,11 @@ expression: html_has_lang
|
|||
╰────
|
||||
help: Must have meaningful value for `lang` prop.
|
||||
|
||||
⚠ eslint-plugin-jsx-a11y(html-has-lang): Missing lang attribute.
|
||||
╭─[html_has_lang.tsx:1:1]
|
||||
1 │ <HTMLTop />
|
||||
· ───────
|
||||
╰────
|
||||
help: Add a lang attribute to the html element whose value represents the primary language of document.
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue