Skip to content

Commit

Permalink
feat(linter) eslint-plugin-react/jsx-key (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 authored Oct 20, 2023
1 parent 06efb77 commit 3f06335
Show file tree
Hide file tree
Showing 5 changed files with 630 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/oxc_ast/src/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub enum AstKind<'a> {
// JSX
// Please make sure to add these to `is_jsx` below.
JSXElement(&'a JSXElement<'a>),
JSXFragment(&'a JSXFragment<'a>),
JSXOpeningElement(&'a JSXOpeningElement<'a>),
JSXElementName(&'a JSXElementName<'a>),

Expand Down Expand Up @@ -231,7 +232,13 @@ impl<'a> AstKind<'a> {
}

pub fn is_jsx(self) -> bool {
matches!(self, Self::JSXElement(_) | Self::JSXOpeningElement(_) | Self::JSXElementName(_))
matches!(
self,
Self::JSXElement(_)
| Self::JSXOpeningElement(_)
| Self::JSXElementName(_)
| Self::JSXFragment(_)
)
}

pub fn is_specific_id_reference(&self, name: &str) -> bool {
Expand Down Expand Up @@ -350,6 +357,7 @@ impl<'a> GetSpan for AstKind<'a> {
Self::JSXOpeningElement(x) => x.span,
Self::JSXElementName(x) => x.span(),
Self::JSXElement(x) => x.span,
Self::JSXFragment(x) => x.span,

Self::TSModuleBlock(x) => x.span,

Expand Down Expand Up @@ -513,6 +521,7 @@ impl<'a> AstKind<'a> {
Self::JSXOpeningElement(_) => "JSXOpeningElement".into(),
Self::JSXElementName(_) => "JSXElementName".into(),
Self::JSXElement(_) => "JSXElement".into(),
Self::JSXFragment(_) => "JSXFragment".into(),

Self::TSModuleBlock(_) => "TSModuleBlock".into(),

Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,12 @@ pub trait Visit<'a>: Sized {
}

fn visit_jsx_fragment(&mut self, elem: &JSXFragment<'a>) {
let kind = AstKind::JSXFragment(self.alloc(elem));
self.enter_node(kind);
for child in &elem.children {
self.visit_jsx_child(child);
}
self.leave_node(kind);
}

fn visit_jsx_child(&mut self, child: &JSXChild<'a>) {
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ mod jest {
pub mod valid_title;
}

mod react {
pub mod jsx_key;
}

mod unicorn {
pub mod catch_error_name;
pub mod error_message;
Expand Down Expand Up @@ -243,6 +247,7 @@ oxc_macros::declare_all_lint_rules! {
unicorn::no_thenable,
unicorn::throw_new_error,
unicorn::prefer_array_flat_map,
react::jsx_key,
import::named,
import::no_cycle,
import::no_self_import,
Expand Down
Loading

0 comments on commit 3f06335

Please sign in to comment.