Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,13 @@ ruleTester.run(RULE_NAME, rule, {
tsx`
<div><p key='static-child-key' /></div>
`,
// Valid: not a list rendering context
tsx`
things.map(thing => {
function NestedComponent() {
return <span key='foo'><span key='bar' /></span>;
}
})
`,
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,14 @@ export default createRule<[], MessageID>({
});

export function create(context: RuleContext<MessageID, []>): RuleListener {
if (!context.sourceCode.getText().includes("key=")) {
return {};
}

if (!context.sourceCode.text.includes("key=")) return {};
return {
JSXAttribute(node: TSESTree.JSXAttribute) {
if (node.name.name !== "key") return;
const jsxElement = node.parent.parent;
const initialScope = context.sourceCode.getScope(jsxElement);
const pMapCallback = AST.findParentNode(jsxElement, isMapCallback);
if (pMapCallback == null) return;
if (pMapCallback == null || context.sourceCode.getScope(pMapCallback) !== initialScope) return;
Copy link

Copilot AI Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The scope comparison logic could be clearer. Consider extracting this into a separate variable or adding a comment explaining why scope equality is checked to prevent false positives in nested components.

Suggested change
if (pMapCallback == null || context.sourceCode.getScope(pMapCallback) !== initialScope) return;
// Ensure the map callback is in the same scope as the JSX element to prevent false positives in nested components
const isSameScope = pMapCallback != null && context.sourceCode.getScope(pMapCallback) === initialScope;
if (!isSameScope) return;

Copilot uses AI. Check for mistakes.
const pKeyedElementOrElse = AST.findParentNode(
jsxElement,
(n) => {
Expand Down