SonarJS
SonarJS copied to clipboard
Fix FP S3403 (`different-types-comparison`): Ignore when comparing to `null` or `undefined`
Examples:
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCI8V8rsgv4ohPCTc
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCwbC8rsgv4ohPC25
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLCwbD8rsgv4ohPC26
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYfLDJOU8rsgv4ohPPir
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPL6JA8vuoRjT2MwJ
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLyCA8vuoRjT2MUf
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLyCA8vuoRjT2MUg
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLsuA8vuoRjT2MCq
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPLsuA8vuoRjT2MCr
- https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYtK6aGR4lU6anUcOmw8
Can we re-discuss this issue just to make sure that it is legit to add an exception?
- With types, if
strictNullChecks
is disabled, the rule never triggers becauseT
is analogous toT
|null
|undefined
in this case - With types, if
strictNullChecks
is enabled, the rule never triggers because the compiler would not accept a variable of typeT
to be assignednull
orundefined
rendering the comparison body not reachable anyway - Without types and without JSDoc annotations, the rule is never triggered because
T
is analogous toT
|null
|undefined
in this case - With JSDoc annotations, the rule is triggered when the JSDoc-declared type does not intersect with
null
, when compared tonull,
orundefined
when compared toundefined
. Hence, in this case, the issue comes from an incorrect JSDoc annotation instead of a rule false positive (for example, about https://peach.aws-prd.sonarsource.com/issues?resolved=false&rules=javascript%3AS3403&open=AYpEPL6JA8vuoRjT2MwJ, see https://github.com/WordPress/gutenberg/blob/trunk/packages/rich-text/src/types.ts) - With pure JavaScript - no
tsconfig.json
file, the rule is never triggered.
Migrated to Jira. New ticket number: JS-36