Skip to content
← Back to rules

eslint/func-style スタイル

An auto-fix is available for this rule.

何をするか

関数宣言または変数に割り当てられた関数式のいずれかを一貫して使用することを強制します。

なぜ問題なのか

このルールは、関数スタイルの特定の形式(関数宣言または変数に割り当てられた関数式)を強制します。 設定でどちらを好むかを指定できます。

js
// 関数宣言
function doSomething() {
  // ...
}

// 変数に割り当てられたアロー関数式
const doSomethingElse = () => {
  // ...
};

// 変数に割り当てられた関数式
const doSomethingAgain = function () {
  // ...
};

デフォルトの "expression" オプションを使用した場合の誤ったコードの例:

js
/* func-style: ["error", "expression"] */

function foo() {
  // ...
}

"declaration" オプションを使用した場合の誤ったコードの例:

js
/* func-style: ["error", "declaration"] */
var foo = function () {
  // ...
};

var foo = () => {};

"declaration" および {"overrides": { "namedExports": "expression" }} オプションを使用した場合の誤ったコードの例:

js
/* func-style: ["error", "declaration", { "overrides": { "namedExports": "expression" } }] */
export function foo() {
  // ...
}

"expression" および {"overrides": { "namedExports": "declaration" }} オプションを使用した場合の誤ったコードの例:

js
/* func-style: ["error", "expression", { "overrides": { "namedExports": "declaration" } }] */
export var foo = function () {
  // ...
};

export var bar = () => {};

デフォルトの "expression" オプションを使用した場合の正しいコードの例:

js
/* func-style: ["error", "expression"] */
var foo = function () {
  // ...
};

"declaration" オプションを使用した場合の正しいコードの例:

js
/* func-style: ["error", "declaration"] */
function foo() {
  // ...
}
// メソッド(オブジェクトに割り当てられた関数)はこのルールのチェック対象外です
SomeObject.foo = function () {
  // ...
};

"declaration" および { "allowArrowFunctions": true } オプションを使用した場合の追加の正しいコードの例:

js
/* func-style: ["error", "declaration", { "allowArrowFunctions": true }] */
var foo = () => {};

"declaration" および {"overrides": { "namedExports": "expression" }} オプションを使用した場合の正しいコードの例:

js
/* func-style: ["error", "declaration", { "overrides": { "namedExports": "expression" } }] */
export var foo = function () {
  // ...
};
export var bar = () => {};

"expression" および {"overrides": { "namedExports": "declaration" }} オプションを使用した場合の正しいコードの例:

js
/* func-style: ["error", "expression", { "overrides": { "namedExports": "declaration" } }] */
export function foo() {
  // ...
}

{"overrides": { "namedExports": "ignore" }} オプションを使用した場合の正しいコードの例:

js
/* func-style: ["error", "expression", { "overrides": { "namedExports": "ignore" } }] */
export var foo = function () {
  // ...
};

export var bar = () => {};
export function baz() {
  // ...
}

設定

1 番目のオプション

type: "expression" | "declaration"

2 番目のオプション

このオプションは以下のプロパティを持つオブジェクトです:

allowArrowFunctions

type: boolean

default: false

true の場合、スタイル設定に関係なくアロー関数が許可されます。

allowTypeAnnotation

type: boolean

default: false

true の場合、型注釈付きの関数がスタイル設定に関係なく許可されます。

overrides

type: object

overrides.namedExports

type: "ignore" | "expression" | "declaration"

default: null

使用方法

このルールを設定ファイルまたは CLI で有効化するには、次のように使用できます:

json
{
  "rules": {
    "func-style": "error"
  }
}
bash
oxlint --deny func-style

参照