简体   繁体   English

eslint 中只有箭头函数吗?

[英]Only arrow functions in eslint?

I prefer this:我更喜欢这个:

const foo = x => x + 1;

to this:对此:

function foo(x) {
  return x + 1;
}

Is there an rule to enforce this?是否有规则来强制执行此操作?

您可以使用此 ESLint首选箭头回调规则,该规则将在任何可以使用箭头函数而不是函数表达式的地方标记错误/警告

Anser based on @JohannesEwald comment and @KevinAmiranoff answer. Anser 基于@JohannesEwald 评论和@KevinAmiranoff 回答。

I'm using the following rules:我正在使用以下规则:

https://www.npmjs.com/package/eslint-plugin-prefer-arrow https://www.npmjs.com/package/eslint-plugin-prefer-arrow

https://eslint.org/docs/rules/prefer-arrow-callback https://eslint.org/docs/rules/prefer-arrow-callback

https://eslint.org/docs/rules/func-style https://eslint.org/docs/rules/func-style

npm install -D eslint-plugin-prefer-arrow

.eslintrc or package.json with eslintConfig : .eslintrcpackage.jsoneslintConfig

"plugins": [
  "prefer-arrow"
],
"rules": {
  "prefer-arrow/prefer-arrow-functions": [
    "error",
    {
      "disallowPrototype": true,
      "singleReturnOnly": false,
      "classPropertiesAllowed": false
    }
  ],
  "prefer-arrow-callback": [
    "error",
    { "allowNamedFunctions": true }
  ],
  "func-style": [
    "error",
    "expression",
    { "allowArrowFunctions": true }
  ]
}

I think this works really well.我认为这非常有效。 If you need to disable the rules for a specific line I do it like this:如果您需要禁用特定行的规则,我会这样做:

// eslint-disable-next-line func-style, prefer-arrow/prefer-arrow-functions

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM