Skip to content

Commit 737f2f4

Browse files
authored
fix: fixed jsx detection method not respect SkipEmptyArray hint, closes #1122 (#1124)
1 parent 9cb9c22 commit 737f2f4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

packages/core/src/jsx/jsx-detection.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export function isJsxLike(
5353
return !(hint & JSXDetectionHint.SkipStringLiteral);
5454
}
5555
case T.ArrayExpression: {
56+
if (node.elements.length === 0) {
57+
return !(hint & JSXDetectionHint.SkipEmptyArray);
58+
}
5659
if (hint & JSXDetectionHint.StrictArray) {
5760
return node.elements.every((n) => isJsxLike(code, n, hint));
5861
}

packages/plugins/eslint-plugin-react-x/src/rules/prefer-read-only-props.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,5 +686,46 @@ ruleTesterWithTypes.run(RULE_NAME, rule, {
686686
687687
export { ItemsListElementSkeleton }
688688
`,
689+
// https://github.com/Rel1cx/eslint-react/issues/1122
690+
tsx`
691+
import { useState } from 'react';
692+
693+
type Book = {
694+
title: string,
695+
author: string,
696+
}
697+
export default function MyComponent() {
698+
const [books, setBooks] = useState<Book[]>([]);
699+
const dropFirstBook = () => {
700+
setBooks(
701+
// Error in next line: A function component's props should be read-only.
702+
// Source: @eslint-react/prefer-read-only-props
703+
(currentBooks: Book[]) => {
704+
const newBooks: Book[] = [];
705+
// Considerable additional logic is found here in a non-toy example;
706+
// suggestions to just rewrite this as e.g.,
707+
// return [...currentBooks.slice(1)];
708+
// which gets the above line reported as compliant with the rule
709+
// are not nearly as helpful as they might seem,
710+
// though knowing about them might help find the root cause of the bug.
711+
newBooks.push(...currentBooks.slice(1));
712+
return newBooks;
713+
}
714+
);
715+
};
716+
//Please assume there's more that was simplified away for this bug demonstration.
717+
if(books.length > 0) {
718+
return(<>
719+
The first book is {books[0].title}.
720+
<button
721+
type='button'
722+
onClick={dropFirstBook}
723+
> Remove it. </button>
724+
</>);
725+
} else {
726+
return null;
727+
}
728+
}
729+
`,
689730
],
690731
});

0 commit comments

Comments
 (0)