39 lines
1.1 KiB
Plaintext

== Why is this an issue?
Optional chaining allows to safely access nested properties or methods of an object without having to check for the existence of each intermediate property manually. It provides a concise and safe way to access nested properties or methods without having to write complex and error-prone `null`/`undefined` checks.
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.
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
----
function foo(param) {
if (param && param.value) {
bar(param.value);
}
}
----
==== Compliant solution
[source,javascript,diff-id=1,diff-type=compliant]
----
function foo(param) {
if (param?.value) {
bar(param.value);
}
}
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining[Optional chaining]