diff --git a/packages/core/src/jsx/jsx-detection.ts b/packages/core/src/jsx/jsx-detection.ts index 6fe463a805..d4cff87a73 100644 --- a/packages/core/src/jsx/jsx-detection.ts +++ b/packages/core/src/jsx/jsx-detection.ts @@ -53,6 +53,9 @@ export function isJsxLike( return !(hint & JSXDetectionHint.SkipStringLiteral); } case T.ArrayExpression: { + if (node.elements.length === 0) { + return !(hint & JSXDetectionHint.SkipEmptyArray); + } if (hint & JSXDetectionHint.StrictArray) { return node.elements.every((n) => isJsxLike(code, n, hint)); } diff --git a/packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts b/packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts index 4cf6bf6727..167fe24296 100644 --- a/packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts +++ b/packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts @@ -686,5 +686,46 @@ ruleTesterWithTypes.run(RULE_NAME, rule, { export { ItemsListElementSkeleton } `, + // https://github.com/Rel1cx/eslint-react/issues/1122 + tsx` + import { useState } from 'react'; + + type Book = { + title: string, + author: string, + } + export default function MyComponent() { + const [books, setBooks] = useState([]); + const dropFirstBook = () => { + setBooks( + // Error in next line: A function component's props should be read-only. + // Source: @eslint-react/prefer-read-only-props + (currentBooks: Book[]) => { + const newBooks: Book[] = []; + // Considerable additional logic is found here in a non-toy example; + // suggestions to just rewrite this as e.g., + // return [...currentBooks.slice(1)]; + // which gets the above line reported as compliant with the rule + // are not nearly as helpful as they might seem, + // though knowing about them might help find the root cause of the bug. + newBooks.push(...currentBooks.slice(1)); + return newBooks; + } + ); + }; + //Please assume there's more that was simplified away for this bug demonstration. + if(books.length > 0) { + return(<> + The first book is {books[0].title}. + + ); + } else { + return null; + } + } + `, ], });