From de1ed7b8890794edfcd6316da480fb8b1aaf2bc1 Mon Sep 17 00:00:00 2001 From: yassin-kammoun-sonarsource Date: Thu, 8 Feb 2024 14:04:06 +0100 Subject: [PATCH] Fix FP S6582 (`prefer-optional-chain`): Update description and severity --- .../javascript/rules/javascript/S6582.html | 18 +++++++++--------- .../javascript/rules/javascript/S6582.json | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.html b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.html index 7227916e207..250bc5f77e0 100644 --- a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.html +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.html @@ -5,22 +5,22 @@

Why is this an issue?

This rule flags logical operations that can be safely replaced with the ?. optional chaining operator.

How to fix it

Replace with ?. optional chaining the logical expression that checks for null/undefined before accessing the -property of an object.

+property of an object, the element of an array, or calling a function.

Code examples

Noncompliant code example

-function foo(param) {
-    if (param && param.value) {
-        bar(param.value);
-    }
+function foo(obj, arr, fn) {
+    if (obj && obj.value) {}
+    if (arr && arr[0])    {}
+    if (fn && fn(42))     {}
 }
 

Compliant solution

-function foo(param) {
-    if (param?.value) {
-        bar(param.value);
-    }
+function foo(obj, arr, fn) {
+    if (obj?.value) {}
+    if (arr?.[0])   {}
+    if (fn?.(42))   {}
 }
 

Resources

diff --git a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.json b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.json index e1660a9adcd..1bb343b50ad 100644 --- a/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.json +++ b/sonar-plugin/javascript-checks/src/main/resources/org/sonar/l10n/javascript/rules/javascript/S6582.json @@ -13,7 +13,7 @@ "constantCost": "5min" }, "tags": [], - "defaultSeverity": "Major", + "defaultSeverity": "Minor", "ruleSpecification": "RSPEC-6582", "sqKey": "S6582", "scope": "All",