Skip to content

ESLint 6.2.0 + babel-eslint + no-unused-vars false positive with for-in loop #12117

@feross

Description

@feross

There's a new issue in ESLint 6.2.0 just caught by the standard test suite. Issue did not exist in ESLint 6.1.0.

Tell us about your environment

  • ESLint Version: 6.2.0
  • Node Version: 10.16.3
  • npm Version: 6.9.0

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

Please show your full configuration:

Configuration
{
  "parserOptions": {
    "ecmaVersion": 2019,
    "ecmaFeatures": {
      "jsx": true
    },
    "sourceType": "module"
  },
  "parser": "babel-eslint",
  "rules": {
    "no-unused-vars": ["error", { "vars": "all", "args": "none", "ignoreRestSiblings": true }]
  }
}

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

function scoreAudioCoverFile (imgFile) {
  const fileName = path.basename(imgFile.name, path.extname(imgFile.name)).toLowerCase()
  const relevanceScore = {
    cover: 80,
    folder: 80,
    album: 80,
    front: 80,
    back: 20,
    spectrogram: -80
  }

  for (const keyword in relevanceScore) {
    if (fileName === keyword) {
      return relevanceScore[keyword]
    }
    if (fileName.indexOf(keyword) !== -1) {
      return relevanceScore[keyword]
    }
  }
  return 0
}
npx eslint --config eslintrc.json t.js

What did you expect to happen?

Just one error:

  1:10  error  'scoreAudioCoverFile' is defined but never used  no-unused-vars

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

Two errors:

   1:10  error  'scoreAudioCoverFile' is defined but never used  no-unused-vars
  12:14  error  'keyword' is defined but never used              no-unused-vars

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 not happen when the default parser is used. However, this issue did not exist with babel-eslint + 6.1.0 and does exist with babel-eslint + 6.2.0.

Issue opened on babel-eslint: babel/babel-eslint#791

Are you willing to submit a pull request to fix this bug?
Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    archived due to ageThis issue has been archived; please open a new issue for any further discussionblockedThis change can't be completed until another issue is resolvedbugESLint is working incorrectlyruleRelates to ESLint's core rules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions