VSCodeでRuboCopによる警告文一覧(随時追加)
共通(?)
Favor modifier if
usage when having a single-line body. Another good alternative is the usage of control flow &&
/||
.
Style/IfUnlessModifier
で制御configroutes.rb
ファイルのletteropnerweb
関連で表示されるif分の中身が一行の場合は 後置if文を使用するか、&& or ||リテラルを使用し、処理をコントロールして下さい
ということらしい(要は行数を少なくしろってことかな?)- 【RuboCop-Linter】align the elements of a hash literal if they span more than one line.という警告に対処する - ぱふの自由帳
Align the keys of a hash literal if they span more than one line.
Layout/HashAlignment
で制御configroutes.rb
ファイルのdevise
のルーティング関連で表示される2行以上であれば要素を揃えなさい
という内容- 【RuboCop-Linter】align the elements of a hash literal if they span more than one line.という警告に対処する - ぱふの自由帳
Please use Rails.root.join('path/to')
instead.
Rails/FilePath
で制御相対パスで書き直してね
という警告- 私はオフにしています
# bad Dir[Rails.root.join('spec', 'support', '**', '*.rb')] # good Dir[Rails.root.join('spec/support/**/*.rb')]
Align the arguments of a method call if they span more than one line.
Layout/ArgumentAlignment
で制御メソッド呼び出しの引数が複数行にわたる場合は、それらを揃えてね
という内容- 私の場合はgem
seed_fu
を使用するときに表示されました
# bad Unit.seed_once(:id, { id: 1, name: "なし" }, ) # good Unit.seed_once( :id, { id: 1, name: "なし" }, )
DB関連
You can use change_table :xxxs, bulk: true
to combine alter queries
Rails/BulkChangeTable
で制御する- DBのカラム変更のmigration編集時に表示
- 警告はしてくれるけど手動で修正する必要がある
change_table
メソッドは、bulk: true
というオプションを設定できる- 使い方 - Class: RuboCop::Cop::Rails::BulkChangeTable — Documentation for rubocop (0.61.1)
- ただしできないのもある?(
t.remove_references
とか)
- ただしできないのもある?(
- 警告通りに修正するとmigrationが早くなる「change_table」で「bulk: true」を使用したマイグレーションの高速化(Rails) - ryotaku's Tech Blog
- 使い方 - Class: RuboCop::Cop::Rails::BulkChangeTable — Documentation for rubocop (0.61.1)
remove_column(without type) is not reversible.
Rails/ReversibleMigration
で制御- DBのカラム変更のmigration編集時に表示
rollbackできない書き方してるからだめだよ
と言われている
Method definitions must not be nested. Use lambda
instead.
- ???
- DBのカラム変更のmigration編集時に表示
メソッド定義はネストしないで、するならlambdaを使って
と言われている- migrationファイルでは
def change
ブロックを削除してdef up
,def down
ブロックを追加して編集する
Uniqueness validation should be with a unique index.
Rails/UniqueValidationWithoutIndex
で制御- modelファイルのvalidation編集時に表示
- modelの記述とDBの定義が矛盾しているときに表示する
RDBMSのunique indexがついていないカラムに対して、Active Recordのレイヤーでuniquenessバリデーションを書いている場合に警告をします。 - Rails/UniqueValidationWithoutIndex copを追加した - pockestrap
Specify an :inverse_of
option.
Rails/InverseOf
で制御- 挙動がおかしくなる場合があるから
inverse_of
オプションをつけてた方が良さそう - けっこう説明が難しそう
同じレコードを参照する場合に、わざわざSQLを発行せずに、既に知っているオブジェクトを使いまわせるようにするためのオプション... といったところでしょうか
RuboCopの Rails/InverseOf について調べた - sometimes I laugh - 詳しく解説されてます - RuboCopの Rails/InverseOf について調べた - sometimes I laugh
Specify a :dependent
option.
Rails/HasManyOrHasOneDependent
で制御dependentオプションを指定していない関連付けがあるよ、関連付けしてね
という警告- 基本的につけた方がいい
- ただし
dependent: :destroy
は要注意
RSpec
Start context description with 'when', 'with', or 'without'.
RSpec/ContextWording
で制御contextを使うときは'when','with', 'without'のどれかを説明の最初につけてね
という警告
Example has too many lines [x/y].
RSpec/ExampleLength
で制御Example(it)1つに色々つめこみすぎ、やること絞ってね
という警告- 場合によっては詰め込みたい場合もあるからspecの種類ごとに除外しても良いかも
Example has too many expectations [x/y]
RSpec/MultipleExpectations
で制御expect構文を使いすぎるな(it 1つにつき expect 1つだよ)
という警告
参考サイト
- Class: RuboCop::Cop::Rails::BulkChangeTable — Documentation for rubocop (0.61.1)
- 「change_table」で「bulk: true」を使用したマイグレーションの高速化(Rails) - ryotaku's Tech Blog
- Rails/UniqueValidationWithoutIndex copを追加した - pockestrap
- Rubyコラム - Dig Ruby! 第4回「RuboCop」を使用したコーディング規約管理
- 【RuboCop-Linter】align the elements of a hash literal if they span more than one line.という警告に対処する - ぱふの自由帳