Help us understand the problem. What is going on with this article?

switct-case 文でインデントした時に ESLint の警告を受けないように

More than 1 year has passed since last update.

問題

こんな感じに書きたい。ただ、これはデフォルトのままだとindentルールに引っかかります。

switch (1) {
  case 1: {...}
  case 2: {...}
  default: {...}
}

こうしろと言ってくるんです。

switch (1) {
case 1: {...}
case 2: {...}
default: {...}
}

解決

どうしても上で書きたい場合は、indentルールのオプションの{SwitchCase: <ネストの深さ>を指定します。なんとデフォルトが{SwitchCase: 0}だった為に警告されたわけです。
つまり、.tslintrcをこんな感じにすれば上の方法で書けるようになります。

"rules": {
  "indent": ["error", 2, {"SwitchCase": 1}],
}

eslint-config-prettier と eslint-plugin-prettier を使ってる場合

もし、prettier/prettierルールを使っている場合でネストをのまま使っていると、

  • Insert '··' (prettier/prettier)
  • Replace '······' with '········' (prettier/prettier)

こんな感じの意味不明なエラー文に怒られてハマります。

これはswitch-case文のインデントを1にすれば直ります😇

nju33
① JavaScript (TypeScript) ② Bash ③ Rust な感じでやってます。最近はブログの方がメインです。
https://nju33.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした