27

My eslint version is 4.18.2, it would give a warning like this:

Unexpected unnamed function
Expected an assignment or function call and instead saw an expression

When defining functions in such a way:

 const farmerIds = a.reduce((function (hash) {
  return function (prev, curr) {
    !hash[curr.farmerId] && (hash[curr.farmerId] = prev.push(curr));
    return prev;
  };
}(Object.create(null))), []);
slee423
  • 1,307
  • 2
  • 20
  • 34
DZS
  • 337
  • 1
  • 4
  • 13
  • You can disable the rule by following this instructions: https://eslint.org/docs/rules/func-names or you could extract the unnamed function and put it under a constant. – Constantin Chirila Oct 10 '18 at 07:46

3 Answers3

39

These are two different issues…

Unexpected unnamed function

This is, as pointed out by Constantin, the ESLint rule func-names.

If you don't want to disable this rule, you can either use names for your functions, like so:

const farmerIds = a.reduce((function reducer(hash) {
  return function fn(prev, curr) {
    !hash[curr.farmerId] && (hash[curr.farmerId] = prev.push(curr));
    return prev;
  };
}(Object.create(null))), []);

Or, and this I would recommend personally, use arrow functions:

const farmerIds = a.reduce(
  (hash => {
    return (prev, curr) => {
      !hash[curr.farmerId] && (hash[curr.farmerId] = prev.push(curr));
      return prev;
    };
  })(Object.create(null)),
  []
);

Expected an assignment or function call and instead saw an expression

ESLint is complaining about this line, which is indeed an expression, not an assignment or function call:

!hash[curr.farmerId] && (hash[curr.farmerId] = prev.push(curr));

You can rewrite it as an if statement:

if (!hash[curr.farmerId]) {
  hash[curr.farmerId] = prev.push(curr);
}

Fixing both

Putting the code examples above together, this code should run without ESLint complaining:

const farmerIds = a.reduce(
  (hash => (prev, curr) => {
    if (!hash[curr.farmerId]) {
      hash[curr.farmerId] = prev.push(curr);
    }
    return prev;
  })(Object.create(null)),
  []
);

Note that I've also removed the curly braces around the body of the first arrow function, which is a nice additional feature of arrows to keep the code more concise.

Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • i still get warning "Unexpected unnamed function" – DZS Oct 10 '18 at 07:56
  • Did you try to extract the `function (hash) {}` into `const FUNCTIONNAME = function (hash) {}` and then use it in your reduce. – Constantin Chirila Oct 10 '18 at 08:02
  • I've updated my answer, you have two different ESLint warnings – Patrick Hund Oct 10 '18 at 08:04
  • Thanks for the beginning of the fix for `Unexpected unnamed function`, but just as a heads up, newer ESLint throws `Expected parentheses around arrow function argument having a body with curly braces. (arrow-parens)` for that second suggestion. I had to change it to like `(hash) =>` and remove the trailing paranthesis you added later on. – Matthew Schuchard Oct 15 '19 at 19:27
3

Another simple way is give the underscore as the function name. For example:

const farmerIds = a.reduce((function _(hash) {...}(...);

tuq
  • 1,328
  • 2
  • 11
  • 21
0

If we need to export without declaring, it can be used as below

export default () => {

// your code goes here

}