1

I am currently using eslint func-names.

What I want to achieve is as follow. Note that I do not want to use arrow function to mitigate the issue on (2).

How can this be configure?

// 1: Error unamed on es-lint (correct)
var foo = function () {};

// 4: No error (correct)
var foo = function foo() {};

// 2: Unexpected unamed function
// i want eslint ignore this unamed error completely for such usage of function
// Do not want to replace with arrow function
callbackFunction.then(function (res) { return res; });
array.map(function (o) { return o * 2} );

I do not see the justification where the eslint allows arrow function to pass the checking but not otherwise, since both (2) and (3) are also anonymous function. Glad if anyone can justified the reasoning behind as well.

// 3: No error
callbackFunction.then((res) => { return res; });
array.map(o => o * 2);
eulercode
  • 1,107
  • 4
  • 16
  • 29
  • I don't believe this is possible with ESLint out of the box. You'll likely have to look at creating custom rules, for which you probably should use ["generator-esllint"](https://github.com/eslint/generator-eslint). As far as: "Is it correct for me to disable func-name completely for such behaviour?" is concerned, this is completely up to you. – Yannick K Jun 06 '20 at 13:30
  • @YannickK i am surprised that there is no such rules available. Anyone raise before such issue and what is the correct term of such function? AFAIK, arrow function is essentially also anonymous function. So if arrow function pass eslint without naming, so should my example be. – eulercode Jun 06 '20 at 13:39
  • 1
    It's really unclear why you are using the `func-names` rule at all. Where *do* you want it to require names? – Bergi Jun 06 '20 at 13:46
  • "*I do not see the justification [why it] allows arrow function to pass the checking*" - because arrow functions cannot be named, and because [they are not (always) exchangeable with `function` expressions](https://stackoverflow.com/q/34361379/1048572). – Bergi Jun 06 '20 at 13:50
  • @Bergi i have edited the question. I want to enforce named function on function declaration but not enforcing named function when it is inside callback and array. eg: `array.map(function (res) { return res * 2} )` – eulercode Jun 06 '20 at 13:55
  • 1
    That's not a function declaration, rather a `var` initialisation (I would actually recommend to use [func-style: declaration](https://eslint.org/docs/rules/func-style)). And it's one of the cases where the `.name` of the function is inferred automatically, so I don't see any good reason to duplicate the identifier there. What is your goal? – Bergi Jun 06 '20 at 14:08

0 Answers0