Skip to content

Commit 700687e

Browse files
anurag2787kasya
andauthored
Remove array indexes as keys in JSX (#2497)
* fix: update JSX comment format for NOSONAR comments and remove index as keys * update * update contributor type --------- Co-authored-by: Kate Golovanova <[email protected]>
1 parent 769efd8 commit 700687e

File tree

20 files changed

+88
-54
lines changed

20 files changed

+88
-54
lines changed

frontend/__tests__/unit/components/RecentIssues.test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ describe('<RecentIssues />', () => {
174174
})
175175

176176
it('renders with missing props gracefully', () => {
177-
render(<RecentIssues data={[{} as Issue]} showAvatar={false} />)
177+
const minimalIssue: Issue = {
178+
createdAt: 1704067200000,
179+
title: '',
180+
url: '',
181+
objectID: 'minimal-issue',
182+
}
183+
render(<RecentIssues data={[minimalIssue]} showAvatar={false} />)
178184
expect(screen.getByText('Recent Issues')).toBeInTheDocument()
179185
})
180186

frontend/__tests__/unit/pages/ChapterDetails.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ describe('chapterDetailsPage Component', () => {
104104
...mockChapterDetailsData,
105105
topContributors: [
106106
{
107-
name: 'Contributor 1',
107+
login: 'contributor1',
108+
name: '',
108109
avatarUrl: 'https://example.com/avatar1.jpg',
109110
},
110111
],
@@ -116,7 +117,7 @@ describe('chapterDetailsPage Component', () => {
116117
render(<ChapterDetailsPage />)
117118

118119
await waitFor(() => {
119-
expect(screen.getByText('Contributor 1')).toBeInTheDocument()
120+
expect(screen.getByText('Contributor1')).toBeInTheDocument()
120121
})
121122
})
122123
test('renders chapter sponsor block correctly', async () => {

frontend/__tests__/unit/pages/Header.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ jest.mock('components/NavDropDown', () => {
8181
return (
8282
<div data-testid="nav-dropdown">
8383
{link.text}
84-
{link.submenu?.map((sub: { href: string; text: string }, i: number) => (
85-
<a key={i} href={sub.href} className={pathname === sub.href ? 'active' : ''}>
84+
{link.submenu?.map((sub: { href: string; text: string }) => (
85+
<a
86+
key={`${sub.text}-${sub.href}`}
87+
href={sub.href}
88+
className={pathname === sub.href ? 'active' : ''}
89+
>
8690
{sub.text}
8791
</a>
8892
))}

frontend/src/app/about/page.tsx

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,24 @@ const leaders = {
5151
const projectKey = 'nest'
5252

5353
const About = () => {
54-
const {
55-
data: projectMetadataResponse,
56-
error: projectMetadataRequestError,
57-
loading: projectMetadataLoading,
58-
} = useQuery(GetProjectMetadataDocument, {
59-
variables: { key: projectKey },
60-
})
54+
const { data: projectMetadataResponse, error: projectMetadataRequestError } = useQuery(
55+
GetProjectMetadataDocument,
56+
{
57+
variables: { key: projectKey },
58+
}
59+
)
6160

62-
const {
63-
data: topContributorsResponse,
64-
error: topContributorsRequestError,
65-
loading: topContributorsLoading,
66-
} = useQuery(GetTopContributorsDocument, {
67-
variables: {
68-
excludedUsernames: Object.keys(leaders),
69-
hasFullName: true,
70-
key: projectKey,
71-
limit: 24,
72-
},
73-
})
61+
const { data: topContributorsResponse, error: topContributorsRequestError } = useQuery(
62+
GetTopContributorsDocument,
63+
{
64+
variables: {
65+
excludedUsernames: Object.keys(leaders),
66+
hasFullName: true,
67+
key: projectKey,
68+
limit: 24,
69+
},
70+
}
71+
)
7472

7573
const { leadersData, isLoading: leadersLoading } = useLeadersData()
7674

@@ -97,7 +95,12 @@ const About = () => {
9795
}
9896
}, [topContributorsResponse, topContributorsRequestError])
9997

100-
const isLoading = projectMetadataLoading || topContributorsLoading || leadersLoading
98+
const isLoading =
99+
!projectMetadataResponse ||
100+
!topContributorsResponse ||
101+
(projectMetadataRequestError && !projectMetadata) ||
102+
(topContributorsRequestError && !topContributors) ||
103+
leadersLoading
101104

102105
if (isLoading) {
103106
return <LoadingSpinner />
@@ -251,8 +254,15 @@ const About = () => {
251254
</SecondaryCard>
252255
)}
253256
<SecondaryCard icon={faScroll} title={<AnchorTitle title="Our Story" />}>
254-
{projectStory.map((text, index) => (
255-
<div key={`story-${index}`} className="mb-4">
257+
{projectStory.map((text) => (
258+
<div
259+
key={text
260+
.slice(0, 40)
261+
.trim()
262+
.replaceAll(' ', '-')
263+
.replaceAll(/[^\w-]/g, '')}
264+
className="mb-4"
265+
>
256266
<div>
257267
<Markdown content={text} />
258268
</div>

frontend/src/app/members/[memberKey]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const UserDetailsPage: React.FC = () => {
8282
const username = mentionMatch[1]
8383
const punctuation = mentionMatch[2] || ''
8484
return (
85-
<React.Fragment key={index}>
85+
<React.Fragment key={`mention-${username}-${index}`}>
8686
<Link
8787
href={`https://github.com/${username}`}
8888
target="_blank"
@@ -96,7 +96,7 @@ const UserDetailsPage: React.FC = () => {
9696
</React.Fragment>
9797
)
9898
}
99-
return <span key={index}>{word} </span>
99+
return <span key={`word-${word}-${index}`}>{word} </span>
100100
})
101101

102102
if (isLoading) {

frontend/src/app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ export default function Home() {
357357
</div>
358358
</SecondaryCard>
359359
<div className="grid gap-6 lg:grid-cols-5">
360-
{counterData.map((stat, index) => (
361-
<div key={index}>
360+
{counterData.map((stat) => (
361+
<div key={stat.label}>
362362
<SecondaryCard className="text-center">
363363
<div className="mb-2 text-3xl font-bold text-blue-400">
364364
<AnimatedCounter end={stat.value} duration={2} />+

frontend/src/components/CardDetailsPage.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ const DetailsCard = ({
156156
title={<AnchorTitle title="Statistics" />}
157157
className="md:col-span-2"
158158
>
159-
{stats.map((stat, index) => (
160-
<div key={index}>
159+
{stats.map((stat) => (
160+
<div key={`${stat.unit}-${stat.value}`}>
161161
<InfoBlock
162162
className="pb-1"
163163
icon={stat.icon}
@@ -313,9 +313,9 @@ export const SocialLinks = ({ urls }) => {
313313
<div>
314314
<strong>Social Links</strong>
315315
<div className="mt-2 flex flex-wrap gap-3">
316-
{urls.map((url, index) => (
316+
{urls.map((url) => (
317317
<a
318-
key={index}
318+
key={url}
319319
href={url}
320320
target="_blank"
321321
rel="noopener noreferrer"

frontend/src/components/Footer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export default function Footer() {
4848
openSection === section.title ? 'max-h-96' : 'max-h-0 lg:max-h-full'
4949
}`}
5050
>
51-
{section.links.map((link, index) => (
52-
<div key={index} className="py-1">
51+
{section.links.map((link) => (
52+
<div key={link.href || `span-${link.text}`} className="py-1">
5353
{link.isSpan ? (
5454
<span className="text-slate-600 dark:text-slate-400">{link.text}</span>
5555
) : (

frontend/src/components/Header.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export default function Header({ isGitHubAuthEnabled }: { readonly isGitHubAuthE
9191
}
9292
return true
9393
})
94-
.map((link, i) => {
94+
.map((link) => {
9595
return link.submenu ? (
96-
<NavDropdown link={link} pathname={pathname} key={i} />
96+
<NavDropdown link={link} pathname={pathname} key={`${link.text}-${link.href}`} />
9797
) : (
9898
<Link
9999
key={link.text}
@@ -193,9 +193,9 @@ export default function Header({ isGitHubAuthEnabled }: { readonly isGitHubAuthE
193193
{link.text}
194194
</div>
195195
<div className="ml-4">
196-
{link.submenu.map((sub, i) => (
196+
{link.submenu.map((sub) => (
197197
<Link
198-
key={i}
198+
key={`${sub.text}-${sub.href}`}
199199
href={sub.href || '/'}
200200
className={cn(
201201
'block w-full px-4 py-3 text-left text-sm text-slate-700 transition duration-150 ease-in-out first:rounded-t-md last:rounded-b-md hover:bg-slate-100 hover:text-slate-900 dark:text-slate-300 dark:hover:bg-slate-700 dark:hover:text-white',

frontend/src/components/ItemCardList.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ const ItemCardList = ({
4545
className={`grid ${showSingleColumn ? 'grid-cols-1' : 'gap-4 gap-y-0 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3'}`}
4646
>
4747
{data.map((item, index) => (
48-
<div key={index} className="mb-4 w-full rounded-lg bg-gray-200 p-4 dark:bg-gray-700">
48+
<div
49+
key={item.objectID || `${item.repositoryName}-${item.title || item.name}-${item.url}`}
50+
className="mb-4 w-full rounded-lg bg-gray-200 p-4 dark:bg-gray-700"
51+
>
4952
<div className="flex w-full flex-col justify-between">
5053
<div className="flex w-full items-center">
5154
{showAvatar && (

0 commit comments

Comments
 (0)