Modify rule S3799: Adapt to LaYC (#2455)
This commit is contained in:
parent
48e6200806
commit
37b24f13ff
@ -1,32 +1,40 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Destructuring is a convenient way of extracting multiple values from data stored in (possibly nested) objects and arrays. However, it is possible to create an empty pattern that has no effect. When empty curly braces or brackets are used to the right of a property name most of the time the intent was to use a default value instead.
|
||||
Destructuring is a convenient way of extracting multiple values from data stored in (possibly nested) objects and arrays. It can make code more concise and expressive by directly extracting values or properties needed from arrays or objects. However, it is possible to define an empty pattern that has no effect, where no variables are bound to the destructured values.
|
||||
|
||||
|
||||
This rule raises an issue when empty destructuring pattern is used.
|
||||
|
||||
|
||||
=== Noncompliant code example
|
||||
|
||||
[source,javascript]
|
||||
[source,javascript,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
var {a: {}, b} = myObj; // Noncompliant
|
||||
function foo({first: [], second}) { // Noncompliant
|
||||
var {a: {}} = myObj; // Noncompliant: this does not create any variable
|
||||
function foo({p: []}) { // Noncompliant: this does not define any parameter
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
When empty curly or square brackets are bound to a pattern with a colon (`:`), like `{ pattern: [] }` or `{ pattern: {} }`, the intent is likely to define a default value. To properly define such a default value, use the assignment operator (`=`) instead.
|
||||
|
||||
=== Compliant solution
|
||||
|
||||
[source,javascript]
|
||||
[source,javascript,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
var {a = {}, b} = myObj;
|
||||
function foo({first = [], second}) {
|
||||
let {a = {}} = myObj;
|
||||
function foo({p = []}) {
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
If that is not the intention, complete the destructuring pattern to contain the variables to create.
|
||||
|
||||
[source,javascript,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let {a: {b, c}} = myObj;
|
||||
function foo({p: [a, b, c]}) {
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment[MDN - Destructuring assignment]
|
||||
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user