question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

prefer-destructuring and reassignment of variables

See original GitHub issue

For example, we have code like:

let error;

if (data.detail) {
    if (data.detail.error) {
        error = data.detail.error;
    } else {
        doSomething();
        error = data.detail;
    }
} else {
    error = data.error;
}

doSomethingWith(error);

prefer-destructuring rule reports 2 issues for this code. I have no ideas how to beautifully refactor this code for following this rule, only renaming a variable and it does not look like a good solution.

So I propose to add an option for reporting about it only for variable declarations, not for assignment expressions.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:25
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

17reactions
fasttimecommented, Jan 8, 2017

Maybe the documentation for this new rule could use some more examples, but in this case, all it really takes to apply destructuring are parentheses:

let error;

if (data.detail) {
    if (data.detail.error) {
        ({ error } = data.detail);
    } else {
        doSomething();
        error = data.detail;
    }
} else {
    ({ error } = data);
}

doSomethingWith(error);
13reactions
not-an-aardvarkcommented, Jan 8, 2017

Seems like a good idea to me. 👍

Maybe we could have two separate options (one for variable declarations and one for assignment expressions):

prefer-destructuring: [error, {VariableDeclarator: true, AssignmentExpression: true}] # current behavior
prefer-destructuring: [error, {VariableDeclarator: true, AssignmentExpression: false}] # only check declarations
# ... etc
Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-destructuring - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
How to fix Eslint error "prefer-destructuring"? - Stack Overflow
this syntax create an array from variables: var x = [y, z] . destructuring assignment uses similar syntax, but on the left-hand side...
Read more >
prefer-destructuring - Rules - ESLint - GitHub Pages
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces...
Read more >
eslint-browser/prefer-destructuring.md at master - GitHub
A fully pluggable tool for identifying and reporting on patterns in JavaScript. - eslint-browser/prefer-destructuring.md at master ...
Read more >
Destructuring assignment - JavaScript - MDN Web Docs
In an array destructuring from an array of length N specified on the right-hand side of the assignment, if the number of variables...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found