eslint: [no-unused-vars] `TypeError: Cannot read property 'type' of undefined` on first `import` when having private methods

Tell us about your environment

Environment Info:

Node version: v14.4.0 npm version: v6.14.5 Local ESLint version: v7.7.0 (Currently used) Global ESLint version: Not found

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

@babel/eslint-parser

Please show your full configuration:

Configuration
{
  "env": {
    "browser": true,
    "es2020": true,
    "jest": true,
    "node": true
  },
  "extends": "eslint:recommended",
  "parser": "@babel/eslint-parser"
}

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 have updated babel-eslint to new @babel/eslint-parser so I can make use of private methods (there was an error on previous ones that only allowed private properties). After doing so, I’m getting the error attached at the end.

The code at WebRtcPeerCore.js:30 is import EventEmitter from 'events', and the failing repository is https://github.com/piranna/kurento-utils-js/blob/master/src/WebRtcPeerCore.js (there’s another one, but it’s company private). It only happen when changing any of the arrow functions set to a private property to be a private method, like this:

// TODO eslint doesn't fully support private methods, replace arrow function
  #setVideoStream = stream =>
  {
    this.#videoStream = stream

    this.emit('setLocalVideo')
  }
  #setVideoStream(stream)
  {
    this.#videoStream = stream

    this.emit('setLocalVideo')
  }
eslint src/* testutils/* __tests__/*

What did you expect to happen?

Eslint parsing the file as normally does.

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

Oops! Something went wrong! :(

ESLint: 7.7.0

TypeError: Cannot read property 'type' of undefined
Occurred while linting /home/piranna/Trabajo/Veedeo.me/kurento-utils-js/src/WebRtcPeerCore.js:30
    at collectUnusedVariables (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/rules/no-unused-vars.js:569:50)
    at collectUnusedVariables (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/rules/no-unused-vars.js:603:17)
    at collectUnusedVariables (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/rules/no-unused-vars.js:603:17)
    at collectUnusedVariables (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/rules/no-unused-vars.js:603:17)
    at Program:exit (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/rules/no-unused-vars.js:615:36)
    at /home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (/home/piranna/Trabajo/Veedeo.me/kurento-utils-js/node_modules/eslint/lib/linter/node-event-generator.js:283:22)

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

Yes, I can write one with some guidance about what’s the source of this error.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (16 by maintainers)

Most upvoted comments

Checked on two different projects, I confirm @babel/eslint-parser version 7.11.4 at last fix this 😄

Thanks for the info!

We can keep this issue open to see if the new @babel/eslint-parser version fixes the problem.

I don’t think this is happening on the import. ESLint prints the line where the currently traversed node starts, which is usually very helpful, but in the case of a crash in the no-unused-vars rule it will always print the line where the whole program starts because the rule listens "Program" node only.

Make sense, probably that’s what happening here.

Anyway, I think this should be reported in the babel/babel repo.

Done at https://github.com/babel/babel/issues/11972.

Make sense… then probably the best would be to wait until it gets to stage 4. At least I’ve been able to replace the deprecated packages for the new ones 😃