Modify rule S3800: Adapt to LaYC (#2484)

This commit is contained in:
Victor 2023-07-13 15:48:04 +02:00 committed by GitHub
parent c5599eca6e
commit 6c110347fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,12 @@
== Why is this an issue?
Unlike strongly typed languages, JavaScript does not enforce a return type on a function. This means that different paths through a function can return different types of values, which can be very confusing to the user and significantly harder to maintain.
Unlike strongly typed languages, JavaScript does not enforce a return type on a function. This means that different paths through a function can return different types of values.
Returning different types from a function can make the code less readable and harder to understand. Maintainers may have to spend more time figuring out how the function works and what it returns. Additionally, it can be harder to ensure that the code is free of type-related errors.
=== Noncompliant code example
[source,javascript]
[source,javascript,diff-id=1,diff-type=noncompliant]
----
function foo(a) { // Noncompliant
function foo(a) { // Noncompliant: function returns 'boolean' or 'number'
if (a === 1) {
return true;
}
@ -15,10 +14,9 @@ function foo(a) { // Noncompliant
}
----
Rework the function so that it always returns the same type. This makes the code more consistent and easier to understand. Maintainers can rely on the function to behave in a predictable way.
=== Compliant solution
[source,javascript]
[source,javascript,diff-id=1,diff-type=compliant]
----
function foo(a) {
if (a === 1) {
@ -31,7 +29,8 @@ function foo(a) {
=== Exceptions
Functions returning ``++this++`` are ignored.
* Functions returning ``++this++`` are ignored.
[source,javascript]
----
@ -41,7 +40,13 @@ function foo() {
}
----
Functions returning expressions having type ``++any++`` are ignored.
* Functions returning expressions having type ``++any++`` are ignored.
== Resources
=== Documentation
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return[MDN - `return`]
ifdef::env-github,rspecator-view[]