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

[no-unused-vars] flagged though it is used inside an object #13327

Closed
gforcada opened this issue May 19, 2020 · 6 comments
Closed

[no-unused-vars] flagged though it is used inside an object #13327

gforcada opened this issue May 19, 2020 · 6 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion works as intended The behavior described in this issue is working correctly

Comments

@gforcada
Copy link

Tell us about your environment

  • ESLint Version: 6.6.0 as well as 7.0.0
  • Node Version: 12.16.3
  • npm Version: 6.14.4

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

Please show your full configuration:

Configuration
{
  "env": {
    "browser": true,
    "es6": true,
    "jquery": true
  },
  "extends": [
    "airbnb-base",
    "plugin:prettier/recommended"
  ],
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module"
  },
  "plugins": ["prettier"],
  "rules": {
    "camelcase": "off",
  }
}

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

  let placedAt = 0;
  let placedAt = myFunction({
    previous: placedAt
  });
eslint js/

What did you expect to happen?
eslint finishing without an error.

What actually happened? Please include the actual, raw output from ESLint.
eslint complains with error 'placedAt' is assigned a value but never used no-unused-vars.

While the placedAt variable is used inside the object given to myFunction function

Are you willing to submit a pull request to fix this bug?
I could try, though my time and JS-foo is not so good, but given some guidance I can give it a try.

@gforcada gforcada added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels May 19, 2020
@platinumazure
Copy link
Member

platinumazure commented May 19, 2020

Are you sure you want to redeclare placedAt?

Seems this would make more sense:

  let placedAt = 0;
  placedAt = myFunction({
    previous: placedAt
  });

Also, no-unused-vars is expecting another use of placedAt after the reassignment. With the code you have provided, you could call myFunction without assigning the result, and this would both satisfy the rule and have no observable effect on behavior:

  let placedAt = 0;
   myFunction({
    previous: placedAt
  });
  // Since nothing else is reading placedAt, this is equivalent behavior

If you do intend to reassign to placedAt, simply use it later in the code, and the rule will be satisfied.

@kaicataldo kaicataldo added works as intended The behavior described in this issue is working correctly and removed bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels May 19, 2020
@gforcada
Copy link
Author

@platinumazure thanks for the quick feedback!

I see that I removed too much to simplify the example, the code looks like this:

  let placedAt = 0;
  placedAt = myFunction({
    previous: placedAt
  });
  placedAt = myFunction({
    previous: placedAt
  });
  placedAt = myFunction({
    previous: placedAt
  });

So the myFunction returns a position, which is needed for the next call.

@platinumazure
Copy link
Member

Hi @gforcada,

The logic is the same as before: On the last reassignment, you could just call the function for its side effects, without reassigning the variable.

  let placedAt = 0;
  placedAt = myFunction({
    previous: placedAt
  });
  placedAt = myFunction({
    previous: placedAt
  });
  /* placedAt = */ myFunction({
    previous: placedAt
  });

Or you can add another usage:

  let placedAt = 0;
  placedAt = myFunction({
    previous: placedAt
  });
  placedAt = myFunction({
    previous: placedAt
  });
  placedAt = myFunction({
    previous: placedAt
  });

  doSomethingWith(placedAt);

Basically, the last assignment is what is unused here. Either drop that reassignment, or use the variable again, and the rule will be satisfied.

@gforcada
Copy link
Author

Oh I see know 🤦 sorry for the noise and thanks for the (very quick 👍 ) explanations!! 🙇

@gforcada
Copy link
Author

Final minor disclaimer that might be worthy: I did report it as eslint was complaining of no-unused-vars on the line where placedAt was being defined, not on the last usage that is indeed superfluous. Maybe that could be updated/improved (the error line where it is reported)? 🤔

@mdjermanovic
Copy link
Member

Final minor disclaimer that might be worthy: I did report it as eslint was complaining of no-unused-vars on the line where placedAt was being defined, not on the last usage that is indeed superfluous. Maybe that could be updated/improved (the error line where it is reported)?

There's an enhancement proposal for this: #13181

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Nov 17, 2020
@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 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion works as intended The behavior described in this issue is working correctly
Projects
None yet
Development

No branches or pull requests

4 participants