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

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

There is a newer version: 10.17.0.28100
Show newest version

The usage of non-null assertion in front of assignments or comparators (! =, ! == or ! ===) can be too easily confused with a negative comparison.

The rule raises an issue when ! =, ! == and ! === are used with a whitespace between the two operators.

Why is this an issue?

The usage of non-null assertion on the left-hand side of an assignment or comparison can be misread and produce unexpected results when one would expect a negative comparator.

How to fix it

If you really meant to use the non-null assertion on the variable on which the issue is raised, you can simply move the non-null assertion to another expression, or define a new one. Otherwise, if an extra whitespace slipped in the middle of a negative comparator, you can simply remove it.

Code examples

Noncompliant code example

let foo: number | undefined;
foo = bar! = 12; // Noncompliant; Is that really what's meant?
if (foo! == bar) { // Noncompliant; Is that really what's meant?
  // ...
}
if (foo! === bar) { // Noncompliant; Is that really what's meant?
  // ...
}

Compliant solution

let foo: number | undefined;
foo = bar = 12;
if (foo == bar) {
  // ...
}
if (foo === bar) {
  // ...
}

Resources





© 2015 - 2024 Weber Informatics LLC | Privacy Policy