Skip to content
← Back to rules

eslint/eqeqeq 細部にこだわる

⚠️🛠️ A dangerous auto-fix is available for this rule for some violations.

何をするか

=== および !== 演算子の使用を必須とし、== および != の使用を禁止します。

なぜ問題か

非厳密な等価演算子を使用すると、型変換によって予期しない振る舞いが生じるため、見つけにくいバグの原因になります。

例:JSON 設定ファイル

json
{
  "eqeqeq": ["error", "always", { "null": "ignore" }]
}

"always"(デフォルト)

このルールに違反する誤りのあるコードの例:

js
/* eqeqeq: "error" */

if (x == 42) {
}
if ("" == text) {
}
if (obj.getStuff() != undefined) {
}

このルールに準拠する正しいコードの例:

js
/* eqeqeq: "error" */

if (x === 42) {
}
if ("" === text) {
}
if (obj.getStuff() !== undefined) {
}

"smart"

"smart" オプションを使用した場合のこのルールに違反する誤りのあるコードの例:

js
/* eqeqeq: ["error", "smart"] */

if (x == 42) {
}
if ("" == text) {
}

"smart" オプションを使用した場合のこのルールに準拠する正しいコードの例:

js
/* eqeqeq: ["error", "smart"] */

if (typeof foo == "undefined") {
}
if (foo == null) {
}
if (foo != null) {
}

{"null": "ignore"}"always" が最初のオプションの場合)

{ "null": "ignore" } オプションを使用した場合のこのルールに違反する誤りのあるコードの例:

js
/* eqeqeq: ["error", "always", { "null": "ignore" }] */
if (x == 42) {
}
if ("" == text) {
}

{ "null": "ignore" } オプションを使用した場合のこのルールに準拠する正しいコードの例:

js
/* eqeqeq: ["error", "always", { "null": "ignore" }] */
if (foo == null) {
}
if (foo != null) {
}

{"null": "always"}(デフォルト - "always" が最初のオプションの場合)

{ "null": "always" } オプションを使用した場合のこのルールに違反する誤りのあるコードの例:

js
/* eqeqeq: ["error", "always", { "null": "always" }] */

if (foo == null) {
}
if (foo != null) {
}

{ "null": "always" } オプションを使用した場合のこのルールに準拠する正しいコードの例:

js
/* eqeqeq: ["error", "always", { "null": "always" }] */

if (foo === null) {
}
if (foo !== null) {
}

{"null": "never"}"always" が最初のオプションの場合)

{ "null": "never" } オプションを使用した場合のこのルールに違反する誤りのあるコードの例:

js
/* eqeqeq: ["error", "always", { "null": "never" }] */

if (x == 42) {
}
if ("" == text) {
}
if (foo === null) {
}
if (foo !== null) {
}

{ "null": "never" } オプションを使用した場合のこのルールに準拠する正しいコードの例:

js
/* eqeqeq: ["error", "always", { "null": "never" }] */

if (x === 42) {
}
if ("" === text) {
}
if (foo == null) {
}
if (foo != null) {
}

設定

第1オプション

type: "always" | "smart"

"always"

常に三重等価比較(=== / !==)を要求します。 これはデフォルトです。

"smart"

安全な比較(typeof、リテラル、ヌルシェイブ)については == / != を許可します。

第2オプション

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

null

type: "always" | "never" | "ignore"

"always"

null との比較では常に三重等価(=== null / !== null)を要求します。 これはデフォルトです。

"never"

null との比較では常に三重等価を要求せず、== null / != null を常に使用します。

"ignore"

null の比較を無視し、== null / != null または === null / !== null のどちらでも許可します。

使用方法

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

json
{
  "rules": {
    "eqeqeq": "error"
  }
}
bash
oxlint --deny eqeqeq

参照