org.sonar.l10n.javascript.rules.javascript.S6568.html Maven / Gradle / Ivy
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