Skip to content

Commit

Permalink
JS-228 Update S6477 (jsx-key): Enable the rule only on React projec…
Browse files Browse the repository at this point in the history
…ts (#4845)
  • Loading branch information
yassin-kammoun-sonarsource authored Sep 23, 2024
1 parent c693042 commit 76fe18b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 69 deletions.
63 changes: 0 additions & 63 deletions its/ruling/src/test/expected/jsts/vuetify/typescript-S6477.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 { RuleTester } from 'eslint';
import { rule } from '../../';
import { clearPackageJsons, loadPackageJsons } from '../../../helpers';

clearPackageJsons();
loadPackageJsons(__dirname, []);

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2018, sourceType: 'module', ecmaFeatures: { jsx: true } } });
ruleTester.run('S6477 turns into a noop on non-React projects', rule, {
valid: [
{
code: `
function Blog(props) {
return (
<ul>
{props.posts.map((post) =>
<li>
{post.title}
</li>
)}
</ul>
);
}
`,
},
],
invalid: [],
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { check } from '../../../tests/tools';
import { rule } from './';
import { check } from '../../../../../tests/tools';
import { clearPackageJsons, loadPackageJsons } from '../../../helpers';
import { rule } from '../../';
import path from 'path';

const sonarId = path.basename(__dirname);

describe('Rule S6477', () => {
beforeEach(() => {
loadPackageJsons(__dirname, []);
});
afterAll(() => {
clearPackageJsons();
});
check(sonarId, rule, __dirname);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"react": "1.2.3"
}
}
5 changes: 1 addition & 4 deletions packages/jsts/src/rules/S6477/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { rules } from 'eslint-plugin-react';
import { decorate } from './decorator';

export const rule = decorate(rules['jsx-key']);
export { rule } from './rule';
43 changes: 43 additions & 0 deletions packages/jsts/src/rules/S6477/rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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/S6477/javascript

import { Rule } from 'eslint';
import { rules } from 'eslint-plugin-react';
import { generateMeta, getDependencies } from '../helpers';
import { decorate } from './decorator';
import { meta } from './meta';

const decoratedJsxKey = decorate(rules['jsx-key']);

export const rule: Rule.RuleModule = {
meta: generateMeta(meta as Rule.RuleMetaData, {
messages: {
...decoratedJsxKey.meta!.messages,
},
}),
create(context: Rule.RuleContext) {
const dependencies = getDependencies(context.filename);
if (!dependencies.has('react')) {
return {};
}
return decoratedJsxKey.create(context);
},
};

0 comments on commit 76fe18b

Please sign in to comment.