2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Empty block statement.eslintno-empty の解決法

Posted at

はじめに

catch ブロックを空にしていたのが原因で、ESLint に「Empty block statement」エラーが出た。

問題

以下のようにlocalStorage.removeItemを呼ぶ際、エラーを握りつぶしたい場合があります。

export function clearHistory() {
  try {
    localStorage.removeItem("KEY");
  } catch {}
}

しかしこの書き方だと ESLint が次のエラーを出します。

Empty block statement. (no-empty)

「catch ブロックが空ですよ。せめて何か書いてください」という指摘らしいです。

解決方法

ざっと調べた限りで、解決方法は3つありました。

1. コメントを入れる

export function clearHistory() {
  try {
    localStorage.removeItem("aimai__runHistory");
  } catch {
    // ignore
  }
}

// ignoreのように「なぜ空なのか」を示すコメントを書けば ESLint が許してくれます。

2. ログを出す

export function clearHistory() {
  try {
    localStorage.removeItem("aimai__runHistory");
  } catch (e) {
    console.warn("clearHistory failed:", e);
  }
}

開発時に気づけるようにログを出す方法です。

3. ESLint ルールを無効化する

/* eslint-disable no-empty */
export function clearHistory() {
  try {
    localStorage.removeItem("aimai__runHistory");
  } catch {}
}

これはあまり使わないかと思います。

参考

公式ドキュメントにちゃんと書かれてました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?