Skip to content

Commit

Permalink
reactor(linter): Refactor jsx key rule to use static_property_info (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Oct 24, 2023
1 parent a2e40ef commit a48e880
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions crates/oxc_linter/src/rules/react/jsx_key.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use oxc_ast::{
ast::{
Expression, JSXAttributeItem, JSXAttributeName, JSXElement, JSXFragment, MemberExpression,
},
ast::{Expression, JSXAttributeItem, JSXAttributeName, JSXElement, JSXFragment},
AstKind,
};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_span::{Atom, GetSpan, Span};
use oxc_span::{GetSpan, Span};

use oxc_macros::declare_oxc_lint;

Expand Down Expand Up @@ -103,8 +101,8 @@ fn is_in_array_or_iter<'a, 'b>(
let callee = &v.callee.without_parenthesized();

if let Expression::MemberExpression(v) = callee {
if let Some((x, span)) = get_member_expression_name_and_span(v.0) {
if TARGET_METHODS.contains(x.as_str()) {
if let Some((span, ident)) = v.static_property_info() {
if TARGET_METHODS.contains(ident) {
return Some(InsideArrayOrIterator::Iterator(span));
}
}
Expand All @@ -119,28 +117,6 @@ fn is_in_array_or_iter<'a, 'b>(
}
}

fn get_member_expression_name_and_span<'a>(
member_expr: &'a MemberExpression<'a>,
) -> Option<(&'a Atom, Span)> {
match member_expr {
MemberExpression::StaticMemberExpression(expr) => {
Some((&expr.property.name, expr.property.span))
}
MemberExpression::ComputedMemberExpression(expr) => match &expr.expression {
Expression::StringLiteral(lit) => Some((&lit.value, lit.span)),
Expression::TemplateLiteral(lit) => {
if lit.expressions.is_empty() && lit.quasis.len() == 1 {
Some((&lit.quasis[0].value.raw, lit.quasis[0].span))
} else {
None
}
}
_ => None,
},
MemberExpression::PrivateFieldExpression(_) => None,
}
}

fn check_jsx_element<'a>(node: &AstNode<'a>, jsx_elem: &JSXElement<'a>, ctx: &LintContext<'a>) {
if let Some(outer) = is_in_array_or_iter(node, ctx) {
if !jsx_elem.opening_element.attributes.iter().any(|attr| {
Expand Down

0 comments on commit a48e880

Please sign in to comment.