All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S6582.html Maven / Gradle / Ivy

There is a newer version: 5.0.0.6962
Show newest version

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, the element of an array, or calling a function.

Code examples

Noncompliant code example

function foo(obj, arr, fn) {
    if (obj && obj.value) {}
    if (arr && arr[0])    {}
    if (fn && fn(42))     {}
}

Compliant solution

function foo(obj, arr, fn) {
    if (obj?.value) {}
    if (arr?.[0])   {}
    if (fn?.(42))   {}
}

Resources

Documentation





© 2015 - 2025 Weber Informatics LLC | Privacy Policy