From 31ac0059b4e2e123fe75e7ceb63fc34484b8d755 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Wed, 6 Sep 2023 14:54:50 +0200 Subject: [PATCH] Add rule S6750 (`no-render-return-value`): The return value of \"ReactDOM.render\" should not be used --- .../expected/ts/Joust/typescript-S6750.json | 5 +++ .../sonar/javascript/checks/CheckList.java | 1 + .../checks/NoRenderReturnValueCheck.java | 36 +++++++++++++++++++ .../javascript/rules/javascript/S6750.html | 24 +++++++++++++ .../javascript/rules/javascript/S6750.json | 29 +++++++++++++++ .../rules/javascript/Sonar_way_profile.json | 3 +- 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 its/ruling/src/test/expected/ts/Joust/typescript-S6750.json create mode 100644 sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoRenderReturnValueCheck.java create mode 100644 sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.html create mode 100644 sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.json diff --git a/its/ruling/src/test/expected/ts/Joust/typescript-S6750.json b/its/ruling/src/test/expected/ts/Joust/typescript-S6750.json new file mode 100644 index 00000000000..73c413f2add --- /dev/null +++ b/its/ruling/src/test/expected/ts/Joust/typescript-S6750.json @@ -0,0 +1,5 @@ +{ +"Joust:ts/Launcher.tsx": [ +432 +] +} diff --git a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java index e5d940449b8..733e4473b43 100644 --- a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java +++ b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/CheckList.java @@ -284,6 +284,7 @@ public static List> getAllChecks() { NoRedundantTypeConstituentsCheck.class, NoReferrerPolicyCheck.class, NoRegexSpacesCheck.class, + NoRenderReturnValueCheck.class, NoReturnAwaitCheck.class, NoReturnTypeAnyCheck.class, NoSameArgumentAssertCheck.class, diff --git a/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoRenderReturnValueCheck.java b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoRenderReturnValueCheck.java new file mode 100644 index 00000000000..febaba44ca0 --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/java/org/sonar/javascript/checks/NoRenderReturnValueCheck.java @@ -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"; + } +} diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.html b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.html new file mode 100644 index 00000000000..76f3b686672 --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.html @@ -0,0 +1,24 @@ +

Why is this an issue?

+

In React, the ReactDOM.render() 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 ReactComponent 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.

+
+const instance = ReactDOM.render(<App />, document.body); // Noncompliant: using the return value of 'ReactDOM.render'
+doSomething(instance);
+
+
+ReactDOM.render(<App />, document.body);
+
+

Alternatively, if you really need a reference to the root ReactComponent instance, the preferred solution is to attach a "callback +ref" to the root element.

+
+ReactDOM.render(<App />, document.body, callbackRef);
+
+

Resources

+

Documentation

+ + diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.json b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.json new file mode 100644 index 00000000000..3804730846e --- /dev/null +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6750.json @@ -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" + ] +} diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json index 37ef31d06b6..0ce7fdc6d5a 100644 --- a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/Sonar_way_profile.json @@ -277,6 +277,7 @@ "S6746", "S6747", "S6748", - "S6749" + "S6749", + "S6750" ] }