Skip to content

Commit ee26b44

Browse files
authored
feat(linter): enhance get_element_type to resolve more element types (#7885)
I think it should if makes sense. _Originally posted by @Boshen in #7881 (comment) Since `jsx-a11y` supports these names, I have aligned them accordingly.
1 parent 7637aac commit ee26b44

27 files changed

Lines changed: 103 additions & 111 deletions

crates/oxc_linter/src/rules/jsx_a11y/alt_text.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ impl Rule for AltText {
172172
let AstKind::JSXOpeningElement(jsx_el) = node.kind() else {
173173
return;
174174
};
175-
let Some(name) = &get_element_type(ctx, jsx_el) else {
176-
return;
177-
};
175+
176+
let name = &get_element_type(ctx, jsx_el);
178177

179178
// <img>
180179
if let Some(custom_tags) = &self.img {

crates/oxc_linter/src/rules/jsx_a11y/anchor_ambiguous_text.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ impl Rule for AnchorAmbiguousText {
107107
return;
108108
};
109109

110-
let Some(name) = get_element_type(ctx, &jsx_el.opening_element) else {
111-
return;
112-
};
110+
let name = get_element_type(ctx, &jsx_el.opening_element);
113111

114112
if name != "a" {
115113
return;
@@ -167,15 +165,14 @@ fn get_accessible_text<'a, 'b>(
167165
};
168166
}
169167

170-
if let Some(name) = get_element_type(ctx, &jsx_el.opening_element) {
171-
if name == "img" {
172-
if let Some(alt_text) = has_jsx_prop_ignore_case(&jsx_el.opening_element, "alt") {
173-
if let Some(text) = get_string_literal_prop_value(alt_text) {
174-
return Some(Cow::Borrowed(text));
175-
};
168+
let name = get_element_type(ctx, &jsx_el.opening_element);
169+
if name == "img" {
170+
if let Some(alt_text) = has_jsx_prop_ignore_case(&jsx_el.opening_element, "alt") {
171+
if let Some(text) = get_string_literal_prop_value(alt_text) {
172+
return Some(Cow::Borrowed(text));
176173
};
177-
}
178-
};
174+
};
175+
}
179176

180177
if is_hidden_from_screen_reader(ctx, &jsx_el.opening_element) {
181178
return None;

crates/oxc_linter/src/rules/jsx_a11y/anchor_has_content.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ declare_oxc_lint!(
6464
impl Rule for AnchorHasContent {
6565
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
6666
if let AstKind::JSXElement(jsx_el) = node.kind() {
67-
let Some(name) = &get_element_type(ctx, &jsx_el.opening_element) else {
68-
return;
69-
};
67+
let name = get_element_type(ctx, &jsx_el.opening_element);
68+
7069
if name == "a" {
7170
if is_hidden_from_screen_reader(ctx, &jsx_el.opening_element) {
7271
return;

crates/oxc_linter/src/rules/jsx_a11y/anchor_is_valid.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ impl Rule for AnchorIsValid {
129129

130130
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
131131
if let AstKind::JSXElement(jsx_el) = node.kind() {
132-
let Some(name) = &get_element_type(ctx, &jsx_el.opening_element) else {
133-
return;
134-
};
132+
let name = get_element_type(ctx, &jsx_el.opening_element);
133+
135134
if name != "a" {
136135
return;
137136
};

crates/oxc_linter/src/rules/jsx_a11y/aria_activedescendant_has_tabindex.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ impl Rule for AriaActivedescendantHasTabindex {
6464
return;
6565
};
6666

67-
let Some(element_type) = get_element_type(ctx, jsx_opening_el) else {
68-
return;
69-
};
67+
let element_type = get_element_type(ctx, jsx_opening_el);
7068

7169
if !HTML_TAG.contains(&element_type) {
7270
return;

crates/oxc_linter/src/rules/jsx_a11y/aria_role.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ impl Rule for AriaRole {
143143
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
144144
if let AstKind::JSXElement(jsx_el) = node.kind() {
145145
if let Some(aria_role) = has_jsx_prop(&jsx_el.opening_element, "role") {
146-
let Some(element_type) = get_element_type(ctx, &jsx_el.opening_element) else {
147-
return;
148-
};
146+
let element_type = get_element_type(ctx, &jsx_el.opening_element);
149147

150148
if self.ignore_non_dom && !HTML_TAG.contains(&element_type) {
151149
return;

crates/oxc_linter/src/rules/jsx_a11y/aria_unsupported_elements.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ fn aria_unsupported_elements_diagnostic(span: Span, x1: &str) -> OxcDiagnostic {
4949
impl Rule for AriaUnsupportedElements {
5050
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
5151
if let AstKind::JSXOpeningElement(jsx_el) = node.kind() {
52-
let Some(el_type) = get_element_type(ctx, jsx_el) else {
53-
return;
54-
};
52+
let el_type = get_element_type(ctx, jsx_el);
5553
if RESERVED_HTML_TAG.contains(&el_type) {
5654
for attr in &jsx_el.attributes {
5755
let attr = match attr {

crates/oxc_linter/src/rules/jsx_a11y/autocomplete_valid.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ impl Rule for AutocompleteValid {
181181

182182
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
183183
if let AstKind::JSXOpeningElement(jsx_el) = node.kind() {
184-
let Some(name) = &get_element_type(ctx, jsx_el) else {
185-
return;
186-
};
184+
let name = &get_element_type(ctx, jsx_el);
185+
187186
if !self.input_components.contains(name.as_ref()) {
188187
return;
189188
}

crates/oxc_linter/src/rules/jsx_a11y/click_events_have_key_events.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ impl Rule for ClickEventsHaveKeyEvents {
5959
};
6060

6161
// Check only native DOM elements or custom component via settings
62-
let Some(element_type) = get_element_type(ctx, jsx_opening_el) else {
63-
return;
64-
};
62+
let element_type = get_element_type(ctx, jsx_opening_el);
63+
6564
if !HTML_TAG.contains(&element_type) {
6665
return;
6766
};

crates/oxc_linter/src/rules/jsx_a11y/heading_has_content.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ impl Rule for HeadingHasContent {
8989
// };
9090

9191
// let name = iden.name.as_str();
92-
let Some(name) = &get_element_type(ctx, jsx_el) else {
93-
return;
94-
};
92+
let name = &get_element_type(ctx, jsx_el);
9593

9694
if !DEFAULT_COMPONENTS.iter().any(|&comp| comp == name)
9795
&& !self

0 commit comments

Comments
 (0)