Skip to content

Commit

Permalink
Add rule S6750 (no-render-return-value): The return value of \"Reac…
Browse files Browse the repository at this point in the history
…tDOM.render\" should not be used
  • Loading branch information
yassin-kammoun-sonarsource committed Sep 11, 2023
1 parent 2afbb04 commit 31ac005
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
5 changes: 5 additions & 0 deletions its/ruling/src/test/expected/ts/Joust/typescript-S6750.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Joust:ts/Launcher.tsx": [
432
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoRedundantTypeConstituentsCheck.class,
NoReferrerPolicyCheck.class,
NoRegexSpacesCheck.class,
NoRenderReturnValueCheck.class,
NoReturnAwaitCheck.class,
NoReturnTypeAnyCheck.class,
NoSameArgumentAssertCheck.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 = "S6750")
public class NoRenderReturnValueCheck implements EslintBasedCheck {

@Override
public String eslintKey() {
return "no-render-return-value";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h2>Why is this an issue?</h2>
<p>In React, the <code>ReactDOM.render()</code> method is used to render a React component into a DOM element. It has a return value, but it’s
generally recommended not to use it. The method might return a reference to the root <code>ReactComponent</code> instance, but it can be unpredictable
and may not always be useful. Indeed, the return value can vary depending on the version of React you’re using and the specific circumstances in which
it’s called.</p>
<pre>
const instance = ReactDOM.render(&lt;App /&gt;, document.body); // Noncompliant: using the return value of 'ReactDOM.render'
doSomething(instance);
</pre>
<pre>
ReactDOM.render(&lt;App /&gt;, document.body);
</pre>
<p>Alternatively, if you really need a reference to the root <code>ReactComponent</code> instance, the preferred solution is to attach a "callback
ref" to the root element.</p>
<pre>
ReactDOM.render(&lt;App /&gt;, document.body, callbackRef);
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> <a href="https://legacy.reactjs.org/docs/react-dom.html#render">React - ReactDom#render</a> </li>
<li> <a href="https://legacy.reactjs.org/docs/refs-and-the-dom.html#callback-refs">React - Callback Refs</a> </li>
</ul>

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"title": "The return value of \"ReactDOM.render\" should not be used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"react"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6750",
"sqKey": "S6750",
"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 @@ -277,6 +277,7 @@
"S6746",
"S6747",
"S6748",
"S6749"
"S6749",
"S6750"
]
}

0 comments on commit 31ac005

Please sign in to comment.