-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FP S6479 (
no-array-index-key
) - Allow keys combining index and …
…other values (#4556) Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
- Loading branch information
1 parent
8cf4d4f
commit 371e71e
Showing
10 changed files
with
180 additions
and
33 deletions.
There are no files selected for viewing
3 changes: 0 additions & 3 deletions
3
its/ruling/src/test/expected/jsts/Joust/typescript-S6479.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
{ | ||
"Joust:ts/components/EventLog.tsx": [ | ||
108 | ||
], | ||
"Joust:ts/components/Timeline.tsx": [ | ||
144 | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
its/ruling/src/test/expected/jsts/desktop/typescript-S6479.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 1 addition & 12 deletions
13
its/ruling/src/test/expected/jsts/react-cloud-music/javascript-S6479.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
export { rule } from './rule'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
// https://sonarsource.github.io/rspec/#/rspec/S6479/javascript | ||
|
||
// inspired from `no-array-index` from `eslint-plugin-react`: | ||
// https://github.com/jsx-eslint/eslint-plugin-react/blob/0a2f6b7e9df32215fcd4e3061ec69ea3f2eef793/lib/rules/no-array-index-key.js#L16 | ||
|
||
import { rules } from 'eslint-plugin-react'; | ||
import { interceptReportForReact } from '../helpers'; | ||
import { Rule } from 'eslint'; | ||
|
||
export const rule = interceptReportForReact( | ||
rules['no-array-index-key'], | ||
(context, reportDescriptor) => { | ||
const { node } = reportDescriptor as Rule.ReportDescriptor & { | ||
node: Rule.Node; | ||
}; | ||
|
||
if (node.type === 'BinaryExpression') { | ||
return; | ||
} | ||
|
||
// we got a report from ESLint, hence one of the expressions included in the literal _is_ the array index | ||
// we can then safely bail if there is another expression in the literal | ||
if (node.type === 'TemplateLiteral' && node.expressions.length > 1) { | ||
return; | ||
} | ||
|
||
context.report(reportDescriptor); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
import { JavaScriptRuleTester } from '../tools'; | ||
import { rule } from './rule'; | ||
|
||
const ruleTester = new JavaScriptRuleTester(); | ||
|
||
ruleTester.run('Rule S6479 - no-array-index-key', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
return <>{items.map((item, index) => { | ||
return <div key={item.id + '' + index}/>; | ||
})}</>; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
return <>{items.map((item, index) => { | ||
return <div key={\`\${item.id}-\${index}\`}/>; | ||
})}</>; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
const renderItems = () => { | ||
let i = 0; | ||
return items.map(() => { | ||
return <div key={i++}/>; | ||
}); | ||
} | ||
return <>{renderItems()}</>; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
const computeKey = (item, index) => { | ||
return item.id + '' + index; | ||
} | ||
return <>{items.map((item, index) => { | ||
return <div key={computeKey(item, index)}/>; | ||
})}</>; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
const computeKey = (index) => { | ||
return index; | ||
} | ||
return <>{items.map((_item, index) => { | ||
return <div key={computeKey(index)}/>; | ||
})}</>; | ||
} // this test should trigger the rule but it seems ESLint is missing it | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
return <>{items.map((item, index) => { | ||
return <div key={index}>{item.id}</div>; | ||
})}</>; | ||
} | ||
`, | ||
errors: 1, | ||
}, | ||
{ | ||
code: ` | ||
export const MyComponent = ({items}) => { | ||
return <>{items.map((item, index) => { | ||
return <div key={\`\${index}\`}>{item.id}</div>; | ||
})}</>; | ||
} | ||
`, | ||
errors: 1, | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters