簡體   English   中英

eslint 中只有箭頭函數嗎?

[英]Only arrow functions in eslint?

我更喜歡這個:

const foo = x => x + 1;

對此:

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

是否有規則來強制執行此操作?

您可以使用此 ESLint首選箭頭回調規則,該規則將在任何可以使用箭頭函數而不是函數表達式的地方標記錯誤/警告

Anser 基於@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM