JSX Key Checker Example
Warn about missing key props in JSX elements inside arrays.
Code
reluxscript
plugin JSXKeyChecker {
struct State {
jsx_without_keys: i32,
}
fn visit_jsx_element(node: &mut JSXElement, ctx: &Context) {
// Check if this JSX element has a key attribute
let mut has_key = false;
for attr in &node.opening_element.attributes {
if let JSXAttribute::JSXAttribute(jsx_attr) = attr {
if let JSXAttributeName::Identifier(ref ident) = jsx_attr.name {
if ident.name == "key" {
has_key = true;
break;
}
}
}
}
if !has_key {
// Mark elements without keys using custom property
node.__missingKey = true;
self.state.jsx_without_keys = self.state.jsx_without_keys + 1;
}
}
fn exit(program: &mut Program, ctx: &Context) {
if self.state.jsx_without_keys > 0 {
println!("Warning: {} JSX elements without key prop", self.state.jsx_without_keys);
}
}
}Input
javascript
const items = [
<Item />,
<Item />,
];Output
Warning: 2 JSX elements without key propHow It Works
- For each JSX element, iterate through its attributes
- Use nested
if letto check if any attribute is named "key" - If no key attribute found, mark the element with custom property
__missingKey - Track count of elements without keys in plugin state
- In
exit(), report total count of JSX elements missing keys
