Skip to content

Commit

Permalink
Modify S1440: Migrate To LayC (#3231)
Browse files Browse the repository at this point in the history
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
  • Loading branch information
yassin-kammoun-sonarsource authored Oct 10, 2023
1 parent 50aa10e commit daff220
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions rules/S1440/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
== Why is this an issue?

include::../description.adoc[]
In JavaScript, there are two types of comparison operators: strict and non-strict.

include::../noncompliant.adoc[]
* Strict operators: These operators compare both value and type. They are represented as `===` (strict equality) and `!==` (strict inequality). For example, `5 === "5"` would return `false` because, although the values are the same, the types are different (one is a number, the other is a string).
include::../compliant.adoc[]
* Non-Strict operators: These operators compare only value, not type. They are represented as `==` (equality) and `!=` (inequality). For example, `5 == "5"` would return `true` because the values are the same, even though the types are different.
=== Exceptions
It's generally recommended to use strict operators in JavaScript to avoid unexpected results due to JavaScript's type coercion. This is because non-strict operators can lead to some counter-intuitive results. For example, `0 == false` would return `true`, which might not be the expected outcome.

[source,javascript,diff-id=1,diff-type=noncompliant]
----
function checkEqual(a, b) {
if (a == b) { // Noncompliant: using non-strict equality '=='
return "Equal";
} else {
return "Not equal";
}
}
Even if testing the equality of a variable against null doesn't do exactly what most JavaScript developers believe, usage of ``++==++`` or ``++!=++`` is tolerated in such context. In the following case, if ``++foo++`` hasn't been initialized, its default value is not ``++null++`` but ``++undefined++``. Nevertheless ``++undefined == null++``, so JavaScript developers get the expected behavior.
console.log(checkEqual(0, false)); // Output: "Equal"
----

You should use the strict equality and inequality operators to prevent type coercion, avoid unexpected outcomes when comparing values of different types, and provide more predictable results.

[source,javascript]
[source,javascript,diff-id=1,diff-type=compliant]
----
if(foo == null) {...}
function checkEqual(a, b) {
if (a === b) {
return "Equal";
} else {
return "Not equal";
}
}
console.log(checkEqual(0, false)); // Output: "Not equal
----

=== Exceptions

The rule does not report on these cases:

* Comparing two literal values
* Evaluating the value of `typeof`
* Comparing against `null`

== Resources
=== Documentation

* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality[MDN - Strict equality]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality[MDN - Strict inequality]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality[MDN - Equality]
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality[MDN - Inequality]
* https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion[MDN - Type coercion]
* https://developer.mozilla.org/en-US/docs/Glossary/Truthy[MDN - Truthy]
* https://developer.mozilla.org/en-US/docs/Glossary/Falsy[MDN - Falsy]

ifdef::env-github,rspecator-view[]

'''
Expand Down

0 comments on commit daff220

Please sign in to comment.