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 S6763 (no-redundant-should-component-update): "shouldComponentUpdate" should not be defined when extending "React.PureComponent" #4160

Merged
merged 2 commits into from
Sep 13, 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
14 changes: 14 additions & 0 deletions its/ruling/src/test/expected/ts/console/typescript-S6763.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"console:src/components/ProjectSelection/ProjectSelection.tsx": [
160
],
"console:src/views/ProjectRootView/ProjectRootView.tsx": [
64
],
"console:src/views/ProjectRootView/SideNav.tsx": [
147
],
"console:src/views/models/DatabrowserView/DatabrowserView.tsx": [
134
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoProtoCheck.class,
NoRedundantJumpCheck.class,
NoRedundantOptionalCheck.class,
NoRedundantShouldComponentUpdateCheck.class,
NoRedundantTypeConstituentsCheck.class,
NoReferrerPolicyCheck.class,
NoRegexSpacesCheck.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;

@JavaScriptRule
@TypeScriptRule
@Rule(key = "S6763")
public class NoRedundantShouldComponentUpdateCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "no-redundant-should-component-update";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2>Why is this an issue?</h2>
<p>In React, a <code>PureComponent</code> is a class component that is optimized for performance by implementing a shallow comparison of props and
state. It is a subclass of the regular React <code>Component</code> class and provides a default implementation of the
<code>shouldComponentUpdate</code> method.</p>
<p>The <code>shouldComponentUpdate</code> method is responsible for determining whether a component should re-render or not. By default, it returns
true and triggers a re-render every time the component receives new props or state. However, <code>PureComponent</code> overrides this method and
performs a shallow comparison of the current and next props and state. If there are no changes, it prevents unnecessary re-renders by returning
false.</p>
<p>Therefore, defining a <code>shouldComponentUpdate</code> method while extending <code>PureComponent</code> is redundant and should be avoided. By
not defining <code>shouldComponentUpdate</code>, you allow <code>React.PureComponent</code> to handle the optimization for you.</p>
<pre data-diff-id="1" data-diff-type="noncompliant">
class MyComponent extends React.PureComponent { // Noncompliant
shouldComponentUpdate() {
// does something
}

render() {
return &lt;div&gt;Hello!&lt;/div&gt;
}
}
</pre>
<p>You should either remove the redundant method <code>shouldComponentUpdate</code> or turn your component into a regular one by extending
<code>Component</code>.</p>
<pre data-diff-id="1" data-diff-type="compliant">
class MyComponent extends React.Component {
shouldComponentUpdate() {
// does something
}

render() {
return &lt;div&gt;Hello!&lt;/div&gt;
}
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://react.dev/reference/react/Component#shouldcomponentupdate">React - shouldComponentUpdate</a> </li>
<li> <a href="https://react.dev/reference/react/PureComponent">React - PureComponent</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"title": "\"shouldComponentUpdate\" should not be defined when extending \"React.PureComponent\"",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6763",
"sqKey": "S6763",
"scope": "All",
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"SECURITY": "LOW"
},
"attribute": "CONVENTIONAL"
},
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
"S6754",
"S6756",
"S6757",
"S6759"
"S6759",
"S6763"
]
}