eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

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

Open yvele opened this issue 1 year ago • 1 comments

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.

yvele avatar May 09 '23 10:05 yvele

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.

silverwind avatar May 02 '24 21:05 silverwind