Modify rule S3800: Adapt to LaYC (#2484)
This commit is contained in:
parent
c5599eca6e
commit
6c110347fc
@ -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[]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user