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

prefer-const object destructuring false positive #9108

Closed
dwelle opened this issue Aug 14, 2017 · 12 comments · Fixed by #10368
Closed

prefer-const object destructuring false positive #9108

dwelle opened this issue Aug 14, 2017 · 12 comments · Fixed by #10368
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

@dwelle
Copy link
Contributor

dwelle commented Aug 14, 2017

Tell us about your environment

  • ESLint Version: 4.4.0

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

Please show your full configuration:

/* eslint prefer-const: [ "warn", { "destructuring": "all", "ignoreReadBeforeAssign": true } ] */

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

/* eslint prefer-const: [ "warn", { "destructuring": "all", "ignoreReadBeforeAssign": true } ] */
/* global someFunc */
(function ({ a }) {
    let b;
    ({ a, b } = someFunc({ a }));
    return { a, b };
})()

What did you expect to happen?

Should pass since neither let nor const can be specified at the start of the variable assignment statement.

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

5:11 - 'b' is never reassigned. Use 'const' instead. (prefer-const)
@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Aug 14, 2017
@not-an-aardvark
Copy link
Member

Thanks for the report. I can reproduce this issue.

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Aug 23, 2017
@Jamesernator
Copy link

It also happens in array destructuring e.g.:

// This isn't global where it's used but the error still happens
let currentScanner = this
const result = []
for (let i = 0 ; i < value ; i++) {
    let char
    ;[currentScanner, char] = currentScanner.consumeOne()
    result.push(char)
}

@Orlandster
Copy link
Contributor

I will make it in the next few days.

@Orlandster
Copy link
Contributor

@not-an-aardvark I'm just going into to it, but there is something I'm curious about. Is the case described above really a reassignment?

Or is it just a reassignment if I explicit assign a value to b:

/* global someFunc */
(function ({ a }) {
  let b = 12;
  ({ a, b } = someFunc({ a }));
  return { a, b };
})()

Because the case below throws the same message:

function foo() {
  let b;
  b = 10;
}

So this is probably also a bug right?

@dwelle
Copy link
Contributor Author

dwelle commented Oct 17, 2017

Strictly speaking, I'd say that in both cases, it is a reassignment (in 2nd case, the let b implicitly assigns undefined, but for the purpose of eslint, it's a good heuristic to say it's not been assigned anything).

Either way, the first case (I reported) is definitely explicit re-assignment.

But.. the nomenclature isn't really important here. The case I reported is a bug because a user wouldn't be able to use destructuring without setting eslint-disable-line

@not-an-aardvark
Copy link
Member

Is the case described above really a reassignment?

Which case? I don't understand the question.

This is a reassignment:

let b = 10;
b = 20;

This is not a reassignment, since b could just be declared in a different place:

let b;
b = 20;

So this is probably also a bug right?

I'm not sure what case you're referring to. Could you clarify:

  • What code does this issue happen with?
  • What are you expecting the rule to do for that code?
  • What does the rule actually do?

@jrpool
Copy link
Contributor

jrpool commented Oct 17, 2017

I was thinking of working on this, but I am happy to yield to @Orlandster1998. I have a question about this issue. It is labeled as a rule-bug issue, but does it not imply that there is also a test-coverage issue? If I were working on this, I would guess that the proper course of action is (1) to write a new test that fails at present, (2) change the rule to correct the bug, (3) show that all the tests now pass. Is that incorrect?

@platinumazure
Copy link
Member

@jrpool Yes, you got it. We always ask for tests in pull requests (exception: when code is being refactored and behavior isn't expected to change), so it's also fair to note that a rule bug usually means we missed a case in testing.

@ericuldall
Copy link

ericuldall commented Dec 2, 2017

Any update on this? I'm currently affected by...

  const organizationId = organization.get('_id');
  ctx.body = { success, organizationId };

I'm happy to work on a PR if this has fallen through the cracks.

@jrpool
Copy link
Contributor

jrpool commented Dec 2, 2017

@Orlandster1998 said he was going to do it. Is that happening?

@ericuldall
Copy link

Actually... just realized my issue was due to a typo. x-/

@platinumazure
Copy link
Member

@Orlandster1998 Friendly ping: Are you still working on this? (If not, totally okay, just let us know if you intend to finish this or if someone else should start looking at this.) Thanks!

btmills pushed a commit that referenced this issue May 28, 2018
…10368)

<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).
-->
close #9108

**What is the purpose of this pull request? (put an "X" next to item)**

[ ] Documentation update
[x] Bug fix ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/bug-report.md))
[ ] New rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-proposal.md))
[ ] Changes an existing rule ([template](https://raw.githubusercontent.com/eslint/eslint/master/templates/rule-change-proposal.md))
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

<!--
    If the item you've checked above has a template, please paste the template questions below and answer them. (If this pull request is addressing an issue, you can just paste a link to the issue here instead.)
-->
#9108

<!--
    Please ensure your pull request is ready:

    - Read the pull request guide (https://eslint.org/docs/developer-guide/contributing/pull-requests)
    - Include tests for this change
    - Update documentation for this change (if appropriate)
-->

<!--
    The following is required for all pull requests:
-->

**What changes did you make? (Give an overview)**
Allow:
```js
(function ({ a }) {
  let b;
  ({ a, b } = obj);
})();
```
or
```js
let a;
{
  let b;
  ({ a, b } = obj);
}
```
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 26, 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 Nov 26, 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
Development

Successfully merging a pull request may close this issue.

8 participants