babel/babel

[Bug]: sourceCode.getScope is not a function (`@babel/eslint-plugin`)

01taylop opened this issue · 5 comments

💻

  • Would you like to work on a fix?

How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

Input code

// na

Configuration file name

No response

Configuration

No response

Current and expected behavior

This issue is concerning eslint and @babel/eslint-plugin.

When upgrading ESLint to 8.37.0, the following lint error can be observed. When using 8.36.0 linting works as expected.

sourceCode.getScope is not a function
Occurred while linting /Users/username/folder/index.js:1
Rule: "@babel/no-invalid-this"

Release notes for 8.37.0

I suspect the cause of the issue could be feat: Copy getScope() to SourceCode.

Environment

package.json

"@babel/core": "7.24.0",
"@babel/eslint-parser": "7.23.10",
"@babel/eslint-plugin": "7.23.5",
"@babel/preset-react": "7.23.3",
"eslint": "8.37.0",

.eslintrc

{
  "rules": {
    "new-cap": 0,
    "@babel/new-cap": 2,
      
    "no-invalid-this": 0,
    "@babel/no-invalid-this": 2,
      
    "no-unused-expressions": 0,
    "@babel/no-unused-expressions": 2,
      
    "object-curly-spacing": 0,
    "@babel/object-curly-spacing": [2, "always"],
      
    "semi": 0,
    "@babel/semi": [2, "never"],
  },
  "plugins": [
    "@babel"
  ]
}

Possible solution

No response

Additional context

No response

Hey @01taylop! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.

When using with ESLint 8, @babel/no-invalid-this delegates the rule to ESLint's built-in no-invalid-this rule since ESLint 8 supports class properties well:

if (parseInt(eslintVersion, 10) >= 8) {
// ESLint 8 supports class properties / private methods natively
// so we simply forward the original rule
module.exports = noInvalidThisRule;
} else {

You can try to remove the @babel/no-invalid-this rule and use no-invalid-this. If the error persists, that would be more like an ESLint regression.

That's good to know, thank you! The same applies to @babel/semi so I made a similar change to my config.

I must have missed this because I upgraded @babel/eslint-plugin before upgrading to ESLint 8. We could look at updating the docs/README.md to better highlight the rules being delegated.

Unfortunately, however, changing these two rules exposes a similar issue in the @babel/object-curly-spacing rule:

Cannot read properties of undefined (reading 'getFirstToken')

This error occurs from ESLint version 8.40.0 onwards, 8.39.0 is fine. Still investigating the cause of this - there is nothing obvious that stands out in the Release Notes.

Do you have a repo we can clone to reproduce the error?

I found the issue I was having; multiple versions of eslint in my dependencies. I am using gulp and gulp-eslint-new which pulls in an older version of eslint 8. Setting the resolutions resolved the issue. Will close the issue.

"resolutions": {
  "eslint": "8.57.0"
},
"dependencies": {
  "eslint": "8.57.0",