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 false-positive if already declared in upper scope

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.13.1
  • Node Version: 8.9.1
  • npm Version: 5.6.0

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

airbnb-styleguide

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const foo = { bar: 1 };
let bar;

try {
  // throws an error here
  bar = foo.bar
} catch(e) {
}
// rule setting, as of the airbnb styleguide
'prefer-destructuring': ['error', {
  VariableDeclarator: {
    array: false,
    object: true,
  },
  AssignmentExpression: {
    array: true,
    object: true,
  },
}, {
  enforceForRenamedProperties: false,
}],

What did you expect to happen? I expect that the destructing error won’t show up. As the variable bar is already declared in an upper scope not in the same.

What actually happened? Please include the actual, raw output from ESLint. The rule suggested to prefer destructing although it won’t work with destructing here, as I have to declare a new variable.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
platinumazurecommented, Dec 18, 2017

You can destructure without a declaration:

const foo = { bar: 1 };
let bar;

try {
  // throws an error here
  ({ bar } = foo)
} catch(e) {
}

Unfortunately, you need parentheses for object destructuring due to the language grammar, but otherwise destructuring does work without a declaration.

0reactions
mrchiefcommented, Apr 4, 2018

I also want to point out that I’m aware of AssignmentExpression and VariableDeclarator options. What I meant with “another option” is something like this:

should not warn for cases like these

let fields = []

if(someCondition) {
  fields = grabFromSomewhere(...)
} else {
  fields = grabbedFromSomewhereElse.fields
}

should warn for cases like these

let fields = []

if(someCondition) {
  fields = grabbedFromSomewhereElse.fields
}

because above can be potentially refactored to use destructuring.

Not a great example but I hope you understand. And probably I’m asking for a lot! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

'err' is already declared in the upper scope - Stack Overflow
My code is giving no errors just i have to meet the eslint as it is giving the error 'err' is already declared...
Read more >
@teppeis/tslint-eslint-rules | Yarn - Package Manager
You want to code in TypeScript but miss all the rules available in ESLint? Now you can combine both worlds by using this...
Read more >
Code Issues - Embold Help Center
BigIntegerInstantiation, Don't create instances of already existing BigInteger ... Fields should be declared at the top of the class, before any method ...
Read more >
Eslint Already Declared In The Upper Scope | Steel King
Declare all the upper scopes are in. They return the scope in this declared later on your business, a function scopes are already...
Read more >
demo-outil-edition - node_modules - eslint - CHANGELOG.md
8f86cca Upgrade: eslint-scope@^5.0.0 (#12011) (Kevin Partington) ... a015234 Fix: prefer-destructuring false positive on "super" (fixes ...
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