Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destructuring two variables and reassigning the value of one is hard with let and const #8187

Closed
ericandrewlewis opened this issue Mar 2, 2017 · 5 comments · Fixed by singapore/lint-condo#244 or renovatebot/renovate#147 · May be fixed by iamhunter/teammates#4
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@ericandrewlewis
Copy link

Tell us about your environment

  • ESLint Version: 3.16.1
  • Node Version: v6.6.0
  • npm Version: 3.10.3

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

Please show your full configuration:

module.exports = {
  "env": {
    "browser": true,
    "es6": true
  },
  "extends": "eslint:recommended",
  "parserOptions": {
    "sourceType": "module",
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true
    }
  },
  "rules": {
    "prefer-const": ["error", {
      "destructuring": "any",
      "ignoreReadBeforeAssign": false
    }],
    "no-console": "off"
  }
};

What did you do? Please include the actual source code causing the issue.

I have a basic test case of the problem in this code:

const props = {
  name: "Eric",
  age: 30,
  occupation: "Artist"
}

let { name, age } = props;
age = 3;

console.log(name);
console.log(age);

ESLint says that let { name, age } = props; is a problem and should use const.

Here, two variables are created via destructuring syntax, and one is later reassigned but the other is not.

Fixing the code as the rule suggests means using const, which creates a syntax error.

What did you expect to happen?

I'm not sure :)

What actually happened? Please include the actual, raw output from ESLint.

$ ./node_modules/.bin/eslint app.js

/Users/eric/Desktop/eslint/app.js
  7:7  error  'name' is never reassigned. Use 'const' instead  prefer-const

✖ 1 problem (1 error, 0 warnings)
@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Mar 2, 2017
@ljharb
Copy link
Sponsor Contributor

ljharb commented Mar 2, 2017

The proper way to fix this is:

const { name } = props;
let { age } = props;

@not-an-aardvark
Copy link
Member

Does the destructuring: "all" option solve your issue?

@platinumazure platinumazure added question This issue asks a question about ESLint and removed triage An ESLint team member will look at this issue soon labels Mar 2, 2017
@ericandrewlewis
Copy link
Author

Sorry, I made an over-simplified example that missed my problem.

In the case of using a rest operator, where you want to get an object that is only some properties of an object.

const props = {
  name: "Eric",
  age: 30,
  occupation: "Artist"
}

let { name, ...otherProps } = props;
otherProps = 3;

console.log(name);
console.log(otherProps);

I just tried destructuring: "all" with this example but I still get the error.

@vitorbal
Copy link
Member

@ericandrewlewis apologies for the delay. Thank you for clarifying your example. I tried it locally and it does indeed report an error, even with the destructuring: "all" option set. I believe this is a bug. I'll work on this.

@vitorbal vitorbal added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion accepted There is consensus among the team that this change meets the criteria for inclusion and removed question This issue asks a question about ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Mar 20, 2017
@mysticatea mysticatea added the rule Relates to ESLint's core rules label Mar 21, 2017
@ericandrewlewis
Copy link
Author

🎊

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
7 participants