简体   繁体   English

警告 ESLint 规则下的意外未命名函数 func-names

[英]Warning Unexpected unnamed function func-names under, ESLint rule

My eslint version is 4.18.2, it would give a warning like this:我的eslint版本是 4.18.2,它会给出这样的警告:

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))), []);

These are two different issues…这是两个不同的问题……

Unexpected unnamed function意外的未命名函数

This is, as pointed out by Constantin, the ESLint rule func-names .正如康斯坦丁所指出的,这就是 ESLint 规则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: ESLint 抱怨这一行,它确实是一个表达式,而不是赋值或函数调用:

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

You can rewrite it as an if statement:您可以将其重写为if语句:

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:将上面的代码示例放在一起,这段代码应该可以在没有 ESLint 抱怨的情况下运行:

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.请注意,我还删除了第一个箭头函数主体周围的花括号,这是箭头的一个很好的附加功能,可以使代码更加简洁。

If we need to export without declaring, it can be used as below如果我们需要在不声明的情况下导出,可以如下使用

export default () => {

// your code goes here

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 eslint警告意外的未命名的异步函数,无论如何要摆脱它? - eslint Warning Unexpected unnamed async function , anyway to get rid if it? 带TypeScript的函数名称的ESlint规则(首字母小写的camelCase) - ESlint rule for function names with TypeScript (camelCase with lower case for first letter) 调试:ESLint 警告“循环中声明的函数包含对变量的不安全引用...no-loop-func” - Debugging: ESLint Warning "Function declared in a loop contains unsafe references to variable(s)...no-loop-func" 自定义eslint规则引发意外保留字 - Custom eslint rule throws with unexpected reserved word 多行函数调用的eslint规则 - eslint rule for multiline function calls 当“缩进”规则设置为“制表符”时,ESLint“意外的制表符” - ESLint "Unexpected tab character" when "indent" rule set to "tab" 为eslint一致指定其他别名吗? - Designate additional alias names for eslint consistent-this rule? 如何处理 Unexpected use of逗号运算符 no-sequences EsLINT 警告 - How to deal with Unexpected use of comma operator no-sequences EsLINT warning 《ESLint no-loop-func 规则》回调中需要循环变量时怎么办 - “ESLint no-loop-func rule” What to do when loop variables are required in a callback 哪个 ESLint 规则适用于 Promise/Arrow 函数缩进? - Which ESLint rule applies to Promise/Arrow function indent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM