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 false positive in for-in loops? still an issue?

See original GitHub issue

Tell us about your environment

Node version: v12.18.3
npm version: v6.14.8
Local ESLint version: v7.7.0 (Currently used)
Global ESLint version: Not found
Done in 10.47s.

What parser (default, @babel/eslint-parser, @typescript-eslint/parser, etc.) are you using? None?

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

I’m a contributor to https://github.com/streamich/git-cz and was trying to get this PR approved: https://github.com/streamich/git-cz/pull/171

As you can see its failing. Then I decided to reproduce the PR by upgrading the ESLint version locally, run ESLint, and see what the output was locally, and its the same as the build server:

/home/travis/build/streamich/git-cz/lib/cli.js
  81:16  error  'key' is defined but never used  no-unused-vars
/home/travis/build/streamich/git-cz/lib/getConfig.js
  16:14  error  'file' is defined but never used  no-unused-vars
✖ 2 problems (2 errors, 0 warnings)

So I decided to take a look at the problematic files/lines:

cli.js

// eslint-disable-next-line guard-for-in
    for (const key in passThroughParams) {
      const value = passThroughParams[key];

      if (key.length === 1) {
        appendedArgs.push('-' + key);
      } else {
        appendedArgs.push('--' + key);
      }

      if (value !== true) {
        appendedArgs.push(value);
      }
    }

getConfig.js

for (const file of configFiles) {
    const filename = path.resolve(dir, file);

    if (fs.existsSync(filename) && fs.statSync(filename).isFile()) {
      return require(filename);
    }
  }

as you can see 👀 both variables are definitely used right after being declared.

What did you expect to happen?

Not to get those 2 errors. everything should pass.

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

/home/travis/build/streamich/git-cz/lib/cli.js
  81:16  error  'key' is defined but never used  no-unused-vars
/home/travis/build/streamich/git-cz/lib/getConfig.js
  16:14  error  'file' is defined but never used  no-unused-vars
✖ 2 problems (2 errors, 0 warnings)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The command "yarn lint" exited with 1.

Are you willing to submit a pull request to fix this bug? not really because supposedly this was already fixed in #12117 and I have no idea why it’s happening again.

Some extra steps that I think you can run to have a good example of this happening:

git clone https://github.com/streamich/git-cz
cd git-cz
npm i
npm i eslint@latest #to update from eslint 4 to 7
npm run eslint #runs the eslint task/script
#get errors :joy: 

Then you can open the files it complains about and play with them to try to fix it.

Related issues: #13163 and #12117 Supposedly this was fixed but somehow it’s happening in this code/project as you can see… and we don’t use babel…

Have a nice day! 🤗

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Aug 27, 2020

It seems that the project is using an older version of babel-eslint.

.eslintrc.json in streamich/git-cz extends mailonline config, which sets "parser": "babel-eslint", and has "babel-eslint": "^8.0.2" as a dependency.

Upgrading babel-eslint will probably solve the issue.

0reactions
rodrigograca31commented, Aug 27, 2020

Thank you. Sorry for creating a duplicated issue. I wasn’t aware that this project required another one that required babel-eslint. 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint 6.2.0 + babel-eslint + no-unused-vars false positive ...
The variable keyword from the for-in loop is seen as not used. This may be a bug in babel-eslint since the issue does...
Read more >
Why am I getting a 'no-unused-vars' warning in a for... ...
This will most likely fix those false-positives where vars are used inside loops and wrongly reported as unused.
Read more >
no-unused-vars - TypeScript ESLint
Disallow unused variables. ... This rule extends the base eslint/no-unused-vars rule. It adds support for TypeScript features, such as types.
Read more >
node_modules/eslint/CHANGELOG.md · master · tbrousso ...
39dfe08 Update: false positives in function-call-argument-newline (fixes ... type condition from no-unused-vars (#12055) (Milos Djermanovic) ...
Read more >
Warning Options (Using the GNU Compiler Collection (GCC))
To suppress this warning use the unused attribute (see Variable Attributes). ... May still have many false positives (not as many as level...
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