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

Add rule S1068 (no-unused-private-class-members): Unused private class members should be removed #4034

Merged
merged 4 commits into from
Jul 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"file-for-rules:S1068.js": [
2,
3
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"file-for-rules:S1068.js": [
0
],
"file-for-rules:S1534.js": [
0
],
Expand Down
5 changes: 5 additions & 0 deletions its/sources/file-for-rules/S1068.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyClass {
#privateField;
#privateMethod(){/* empty */}
otherMethod(){/* empty */}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoUnsafeOptionalChainingCheck.class,
NoUnstableNestedComponentsCheck.class,
NoUnusedClassComponentMethodsCheck.class,
NoUnusedPrivateClassMembersCheck.class,
NoUselessCallCheck.class,
NoUselessCatchCheck.class,
NoUselessConstructorCheck.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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.
*/
package org.sonar.javascript.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.javascript.api.EslintBasedCheck;
import org.sonar.plugins.javascript.api.JavaScriptRule;
import org.sonar.plugins.javascript.api.TypeScriptRule;

@TypeScriptRule
@JavaScriptRule
@Rule(key = "S1068")
public class NoUnusedPrivateClassMembersCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "no-unused-private-class-members";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Why is this an issue?</h2>
<p>If a private member of the class is declared but not used in the program, it can be considered dead code and should therefore be removed. This will
improve maintainability because developers will not wonder what the variable is used for.</p>
<p>The private class members were introduced in ES2022 and use <code>#</code> hash symbol prefix. It is possible to declare private fields, methods,
getters and setters as well as their static counterparts.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
class MyClass {
#privateField; // Noncompliant, #privateField is unused
#privateMethod(){} // Noncompliant, #privateMethod is unused
// ...
}
</pre>
<p>To fix the code remove unused private member of the class.</p>
<pre data-diff-id="1" data-diff-type="compliant">
class MyClass {
// ...
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields">MDN - private class members</a> </li>
<li> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">MDN - classes overview</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"title": "Unused private class members should be removed",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"unused"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-1068",
"sqKey": "S1068",
"scope": "All",
"quickfix": "unknown",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"S888",
"S905",
"S930",
"S1068",
"S1119",
"S1121",
"S1125",
Expand Down
1 change: 1 addition & 0 deletions src/linting/eslint/linter/quickfixes/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const quickFixRules = new Set([
'no-lonely-if',
'no-throw-literal',
'no-unreachable',
'no-unused-private-class-members',
'no-useless-call',
'no-useless-constructor',
'prefer-object-spread',
Expand Down
2 changes: 2 additions & 0 deletions src/linting/eslint/rules/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { decorateNoThrowLiteral } from './no-throw-literal-decorator';
import { decorateNoUnreachable } from './no-unreachable-decorator';
import { decorateNoUnstableNestedComponents } from './no-unstable-nested-components';
import { decorateNoUnusedExpressions } from './no-unused-expressions-decorator';
import { decorateNoUnusedPrivateClassMembers } from './no-unused-private-class-members-decorator';
import { decorateNoUselessCall } from './no-useless-call-decorator';
import { decorateNoUselessConstructor } from './no-useless-constructor-decorator';
import { decorateObjectShorthand } from './object-shorthand-decorator';
Expand Down Expand Up @@ -95,6 +96,7 @@ export const decorators: Record<string, RuleDecorator> = {
'no-unreachable': decorateNoUnreachable,
'no-unstable-nested-components': decorateNoUnstableNestedComponents,
'no-unused-expressions': decorateNoUnusedExpressions,
'no-unused-private-class-members': decorateNoUnusedPrivateClassMembers,
'no-useless-call': decorateNoUselessCall,
'no-useless-constructor': decorateNoUselessConstructor,
'no-var': decorateNoVar,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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/S1068/javascript

import { Rule } from 'eslint';
import * as estree from 'estree';
import { interceptReport } from './helpers';

// core implementation of this rule does not provide quick fixes
export function decorateNoUnusedPrivateClassMembers(rule: Rule.RuleModule): Rule.RuleModule {
rule.meta!.hasSuggestions = true;
return interceptReport(rule, (context, reportDescriptor) => {
const suggest: Rule.SuggestionReportDescriptor[] = [];
const node = (reportDescriptor as any).node as estree.Node;
suggest.push({
desc: 'Remove unused private class member',
fix: fixer => fixer.remove(node),
});
context.report({ ...reportDescriptor, suggest });
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MyClass {
#privateField;
#privateMethod(){/* empty */}
otherMethod(){/* empty */}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 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 { eslintRules } from 'linting/eslint/rules/core';
import { decorateNoUnusedPrivateClassMembers } from 'linting/eslint/rules/decorators/no-unused-private-class-members-decorator';

const rule = decorateNoUnusedPrivateClassMembers(eslintRules['no-unused-private-class-members']);
const ruleTester = new RuleTester({
parserOptions: { ecmaVersion: 2022 },
});

ruleTester.run(`Unused private class members should be removed`, rule, {
valid: [
{
code: `
class MyClass{
#foo = 123;
bar(){return this.#foo;}
}`,
},
],
invalid: [
{
code: `class MyClass{ #foo = 123; }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
{
code: `class MyClass{ #foo(){} }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
{
code: `class MyClass{ get #foo(){} }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
{
code: `class MyClass{ set #foo(v){} }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
{
code: `class MyClass{ static #foo = 123; }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
{
code: `class MyClass{ static #foo(){} }`,
errors: [
{
suggestions: [
{
desc: 'Remove unused private class member',
output: `class MyClass{ }`,
},
],
},
],
},
],
});