eslint中只有箭头功能吗?

时间:2018-10-05 15:50:50

标签: javascript eslint

我更喜欢这个:

const foo = x => x + 1;

对此:

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

是否存在规则来强制执行此规则?

2 个答案:

答案 0 :(得分:3)

您可以使用此ESLint prefer-arrow-callback规则,该规则会在可能使用箭头函数而不是函数表达式的任何地方标记错误/警告

答案 1 :(得分:1)

基于@JohannesEwald评论和@KevinAmiranoff答案的分析器。

我正在使用以下规则:

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

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

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

npm install -D eslint-plugin-prefer-arrow

.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 }
  ]
}

我认为这真的很好。如果您需要为特定行禁用规则,请按以下步骤操作:

// eslint-disable-next-line func-style, prefer-arrow/prefer-arrow-functions
相关问题