Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FP S6479 (no-array-index-key) - Allow keys combining index and other values #4556

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions packages/jsts/src/rules/S6479/index.ts
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';
48 changes: 48 additions & 0 deletions packages/jsts/src/rules/S6479/rule.ts
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/S6486/javascript
ericmorand-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

// 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);
},
);
109 changes: 109 additions & 0 deletions packages/jsts/src/rules/S6479/unit.test.ts
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, {
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,
},
],
});
Loading