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.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
platinumazurecommented, May 20, 2020

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.

1reaction
platinumazurecommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unused-vars - 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 >
typescript-eslint/no-unused-vars does not work correctly
Add these line to your eslintrc.js file under rules : "no-use-before-define": "off", "@typescript-eslint/no-use-before-define": ["error"],.
Read more >
How to disable "no-unused-vars" in Visual Studio Code (or at ...
One thing that annoys the heck out of me, though, is Visual Studio Code's ESLint plugin marking any unused variable or argument as...
Read more >
12 essential ESLint rules for React - LogRocket Blog
If the project is using the no-unused-vars rule, this results in an error since React is imported but not used anywhere.
Read more >
Turning on checking for a rule - ESLint - LinkedIn
It's common to use a style guide as a starting point and then customize it ... I see it also in the pop-up...
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