Modify rule S3403: LaYC format (#2765)
This commit is contained in:
parent
5de743ed99
commit
bd0e169466
@ -1,9 +1,15 @@
|
|||||||
|
This rule raises an issue when a strict equality operator is used to compare objects of different types.
|
||||||
|
|
||||||
== Why is this an issue?
|
== Why is this an issue?
|
||||||
|
|
||||||
The strict equality operator in JavaScript is represented by three equal signs (``++===++``), the strict inequality with (``++!==++``). It is used to compare two values for equality, but with an important difference from the regular equality operator (``++==++``). The strict equality operator compares both value and type, while the regular equality operator only compares values after performing type coercion if necessary.
|
The strict equality operator in JavaScript is represented by three equal signs (``++===++``), the strict inequality with (``++!==++``). It is used to compare two values for equality, but with an important difference from the regular equality operator (``++==++``). The strict equality operator compares both value and type, while the regular equality operator only compares values after performing type coercion if necessary.
|
||||||
|
|
||||||
The problem with using the strict equality operator (``++===++``) with operands of dissimilar types lies in the way JavaScript handles the comparison. When you use ``++===++`` to compare two values of different types, it will always return false since their types are different, regardless of whether the values could be considered equal under certain conditions.
|
The problem with using the strict equality operator (``++===++``) with operands of dissimilar types lies in the way JavaScript handles the comparison. When you use ``++===++`` to compare two values of different types, it will always return false since their types are different, regardless of whether the values could be considered equal under certain conditions.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
|
||||||
[source,javascript,diff-id=1,diff-type=noncompliant]
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
||||||
----
|
----
|
||||||
let a = 8;
|
let a = 8;
|
||||||
@ -14,8 +20,8 @@ if (a === b) { // Noncompliant: Always false since 'a' is a number and 'b' a str
|
|||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
To address this issue, you can use the loose equality operator (``++==++``), which performs type coercion.
|
To address this issue, you can use the loose equality operator (``++==++``), which performs type coercion.
|
||||||
|
|
||||||
[source,javascript,diff-id=1,diff-type=compliant]
|
[source,javascript,diff-id=1,diff-type=compliant]
|
||||||
----
|
----
|
||||||
let a = 8;
|
let a = 8;
|
||||||
@ -41,14 +47,14 @@ if (a === Number(b)) {
|
|||||||
== Resources
|
== Resources
|
||||||
=== Documentation
|
=== Documentation
|
||||||
|
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality[MDN - Strict equality (``++===++``)]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality[Strict equality (``++===++``)]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality[MDN - Strict inequality (``++!==++``)]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_inequality[Strict inequality (``++!==++``)]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality[MDN - Equality (``++==++``)]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality[Equality (``++==++``)]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality[MDN - Inequality (``++!=++``)]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Inequality[Inequality (``++!=++``)]
|
||||||
* https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion[MDN - Type coercion]
|
* MDN - https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion[Type coercion]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number[MDN - ``++Number()++`` constructor]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number[``++Number()++`` constructor]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String[MDN - ``++String()++`` constructor]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/String[``++String()++`` constructor]
|
||||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean[MDN - ``++Boolean()++`` constructor]
|
* MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean[``++Boolean()++`` constructor]
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
ifdef::env-github,rspecator-view[]
|
||||||
|
|
||||||
|
@ -1,17 +1,37 @@
|
|||||||
|
This rule raises an issue when an identity comparison operator is used to compare objects of different types.
|
||||||
|
|
||||||
== Why is this an issue?
|
== Why is this an issue?
|
||||||
|
|
||||||
Operators https://docs.python.org/3/reference/expressions.html#is-not[``++is++``] and https://docs.python.org/3/reference/expressions.html#is-not[``++is not++``] check if their operands point to the same instance, thus they will always return respectively True and False when they are used to compare objects of different type. Such comparisons can only be bugs.
|
Operators https://docs.python.org/3/reference/expressions.html#is-not[``++is++``] and https://docs.python.org/3/reference/expressions.html#is-not[``++is not++``] check if their operands point to the same instance, thus they will always return respectively ``++False++`` and ``++True++`` when they are used to compare objects of different types.
|
||||||
|
|
||||||
=== Noncompliant code example
|
=== Code examples
|
||||||
|
|
||||||
[source,python]
|
==== Noncompliant code example
|
||||||
|
|
||||||
|
[source,python,diff-id=1,diff-type=noncompliant]
|
||||||
----
|
----
|
||||||
myint = 1
|
a = 1
|
||||||
mystring = "1"
|
b = "1"
|
||||||
value = myint is mystring # Noncompliant. Always False
|
value = a is b # Noncompliant. Always False
|
||||||
value = myint is not mystring # Noncompliant. Always True
|
value = a is not b # Noncompliant. Always True
|
||||||
----
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,python,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
a = 1
|
||||||
|
b = 1
|
||||||
|
value = a is b
|
||||||
|
value = a is not b
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Python documentation - https://docs.python.org/3/reference/expressions.html#is-not[Identity comparisons]
|
||||||
|
|
||||||
ifdef::env-github,rspecator-view[]
|
ifdef::env-github,rspecator-view[]
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
Loading…
x
Reference in New Issue
Block a user