sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.28k stars 367 forks source link

`consistent-function-scoping` false positive with an arrow function using `this` #2088

Open yvele opened 1 year ago

yvele commented 1 year ago

With the following rule:

"unicorn/consistent-function-scoping" : ["error", {
  checkArrowFunctions : true
}]

(I do really want arrow function to move upper scope when they do not use any variable or upper this).

This is wrongly reported as a problem:

class Foo {
  public bar = new FinalizationRegistry(() => {
    console.log(this); // <- `this` is used in this arrow function
  })
}

error Move arrow function to the outer scope unicorn/consistent-function-scoping

Same for:

function foo(this) {
  const bar = () => console.log(this); // <- `this` is used in this arrow function
  return [].map(() => bar);
}

error Move arrow function 'bar' to the outer scope

The cases below should be OK when an arrow function only consuming this from outer scope.

silverwind commented 6 months ago

Just had this false-positive with this in a class show up after switching eslint's parser from espree to @typescript-eslint/parser. So the bug looks to be parser-related.