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 0000000000..73c413f2ad --- /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 e5d940449b..733e4473b4 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 0000000000..febaba44ca --- /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 0000000000..76f3b68667 --- /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 0000000000..3804730846 --- /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 37ef31d06b..0ce7fdc6d5 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" ] }