Skip to content

Commit

Permalink
Fix FP S6582 (prefer-optional-chain): Update description and severi…
Browse files Browse the repository at this point in the history
…ty (#4562)
  • Loading branch information
yassin-kammoun-sonarsource committed Feb 9, 2024
1 parent a8d3908 commit 2463636
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Expand Up @@ -5,22 +5,22 @@ <h2>Why is this an issue?</h2>
<p>This rule flags logical operations that can be safely replaced with the <code>?.</code> optional chaining operator.</p>
<h2>How to fix it</h2>
<p>Replace with <code>?.</code> optional chaining the logical expression that checks for <code>null</code>/<code>undefined</code> before accessing the
property of an object.</p>
property of an object, the element of an array, or calling a function.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
function foo(param) {
if (param &amp;&amp; param.value) {
bar(param.value);
}
function foo(obj, arr, fn) {
if (obj &amp;&amp; obj.value) {}
if (arr &amp;&amp; arr[0]) {}
if (fn &amp;&amp; fn(42)) {}
}
</pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
function foo(param) {
if (param?.value) {
bar(param.value);
}
function foo(obj, arr, fn) {
if (obj?.value) {}
if (arr?.[0]) {}
if (fn?.(42)) {}
}
</pre>
<h2>Resources</h2>
Expand Down
Expand Up @@ -13,7 +13,7 @@
"constantCost": "5min"
},
"tags": [],
"defaultSeverity": "Major",
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6582",
"sqKey": "S6582",
"scope": "All",
Expand Down

0 comments on commit 2463636

Please sign in to comment.