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 S6754 (hook-use-state): Ignore state variables without a setter #4637

Merged
merged 3 commits into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

This file was deleted.

10 changes: 0 additions & 10 deletions its/ruling/src/test/expected/jsts/eigen/typescript-S6754.json
Expand Up @@ -8,19 +8,9 @@
"eigen:src/app/Components/HeaderArtworksFilter/HeaderArtworksFilter.tsx": [
59
],
"eigen:src/app/Components/PopoverMessage/PopoverMessageProvider.tsx": [
29,
30
],
"eigen:src/app/Components/StickyTabPage/StickyTabPageTabBar.tsx": [
24
],
"eigen:src/app/Components/Tag/TagArtworks.tsx": [
91
],
"eigen:src/app/Components/Toast/ToastComponent.tsx": [
38
],
"eigen:src/app/Scenes/About/About.tsx": [
16
],
Expand Down
@@ -1,6 +1,5 @@
{
"searchkit:examples/next/components/geosearch-examples/Input.jsx": [
9,
17
]
}
33 changes: 33 additions & 0 deletions packages/jsts/src/rules/S6754/decorator.ts
@@ -0,0 +1,33 @@
/*
* 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/S6754/javascript

import { Rule } from 'eslint';
import { interceptReportForReact } from '../helpers';

export function decorate(rule: Rule.RuleModule): Rule.RuleModule {
return interceptReportForReact(rule, (context, descriptor) => {
const { node } = descriptor as any;
if (node.type === 'ArrayPattern' && node.elements.length === 1) {
return;
}
context.report(descriptor);
});
}
23 changes: 23 additions & 0 deletions packages/jsts/src/rules/S6754/index.ts
@@ -0,0 +1,23 @@
/*
* 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 { rules } from 'eslint-plugin-react';
import { decorate } from './decorator';

export const rule = decorate(rules['hook-use-state']);
52 changes: 52 additions & 0 deletions packages/jsts/src/rules/S6754/unit.test.ts
@@ -0,0 +1,52 @@
/*
* 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 './';

const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 2018, sourceType: 'module' },
});
ruleTester.run(
'The return value of "useState" should be destructured and named symmetrically',
rule,
{
valid: [
{
code: `
import { useState } from 'react';
function useFoo() {
const [foo] = useState();
return [foo];
}`,
},
],
invalid: [
{
code: `
import { useState } from 'react';
function useFoo() {
const [foo, bar] = useState();
return [foo, bar];
}`,
errors: 1,
},
],
},
);
2 changes: 2 additions & 0 deletions packages/jsts/src/rules/index.ts
Expand Up @@ -103,6 +103,7 @@ import { rule as S1527 } from './S1527'; // future-reserved-words
import { rule as S3531 } from './S3531'; // generator-without-yield
import { rule as S4790 } from './S4790'; // hashing
import { rule as S5691 } from './S5691'; // hidden-files
import { rule as S6754 } from './S6754'; // hook-use-state
import { rule as S6849 } from './S6849'; // html-has-lang
import { rule as S3785 } from './S3785'; // in-operator-type-error
import { rule as S3686 } from './S3686'; // inconsistent-function-call
Expand Down Expand Up @@ -394,6 +395,7 @@ rules['future-reserved-words'] = S1527;
rules['generator-without-yield'] = S3531;
rules['hashing'] = S4790;
rules['hidden-files'] = S5691;
rules['hook-use-state'] = S6754;
rules['html-has-lang'] = S6849;
rules['in-operator-type-error'] = S3785;
rules['inconsistent-function-call'] = S3686;
Expand Down