feat(linter): refine jsx-a11y settings (#1816)

refine jsx-a11y settings for #1141
This commit is contained in:
msdlisper 2023-12-26 15:55:52 +08:00 committed by GitHub
parent 7d9d920148
commit f0ad356108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 9 deletions

View file

@ -42,7 +42,7 @@ itertools = { workspace = true }
dashmap = { workspace = true }
convert_case = { workspace = true }
language-tags = { workspace = true }
mime_guess = { workspace = true }
mime_guess = { workspace = true }
rust-lapper = "1.1.0"
once_cell = "1.19.0"

View file

@ -151,8 +151,7 @@ fn test() {
("<html lang='zz-LL' />", None, None),
("<html lang={undefined} />", None, None),
("<Foo lang={undefined} />", None, Some(settings())),
// TODO: wait polymorphicPropName complete in next PR
// ("<Box as='html' lang='foo' />", None, None),
("<Box as='html' lang='foo' />", None, Some(settings())),
];
Tester::new_with_settings(Lang::NAME, pass, fail).test_and_snapshot();

View file

@ -31,4 +31,11 @@ expression: lang
╰────
help: Set a valid value for lang attribute.
⚠ eslint-plugin-jsx-a11y(lang): Lang attribute must have a valid value.
╭─[lang.tsx:1:1]
1 │ <Box as='html' lang='foo' />
· ──────────
╰────
help: Set a valid value for lang attribute.

View file

@ -163,20 +163,28 @@ pub fn get_parent_es6_component<'a, 'b>(ctx: &'b LintContext<'a>) -> Option<&'b
None
})
}
pub fn get_element_type(context: &LintContext, element: &JSXOpeningElement) -> Option<String> {
let JSXElementName::Identifier(ident) = &element.name else {
return None;
};
let mut element_type = String::from(ident.name.as_str());
let LintSettings { jsx_a11y } = context.settings();
let JsxA11y { polymorphic_prop_name: _, components } = jsx_a11y;
if let Some(val) = components.get(&element_type) {
element_type = String::from(val);
let JsxA11y { polymorphic_prop_name, components } = jsx_a11y;
if let Some(polymorphic_prop_name_value) = polymorphic_prop_name {
if let Some(as_tag) = has_jsx_prop_lowercase(element, &polymorphic_prop_name_value) {
if let Some(JSXAttributeValue::StringLiteral(str)) = get_prop_value(as_tag) {
return Some(String::from(str.value.as_str()));
}
}
}
Some(element_type)
let element_type = ident.name.as_str();
if let Some(val) = components.get(element_type) {
return Some(String::from(val));
}
Some(String::from(element_type))
}
pub fn parse_jsx_value(value: &JSXAttributeValue) -> Result<f64, ()> {