LoginSignup
2
1

More than 1 year has passed since last update.

RuboCopの導入方法メモ

Last updated at Posted at 2022-05-20

RuboCop導入方法の備忘録です。

環境

macOS Monterey 12.3.1
ruby 3.1.2
rails 6.1.6

インストール方法

Gemfileに必要なgemを追記する。

Gemfile
group :development do
  gem 'rubocop', '~> 1.29', require: false
  gem 'rubocop-performance', require: false # 拡張機能
  gem 'rubocop-rails', require: false # 拡張機能
  gem 'rubocop-rspec', require: false # 拡張機能
end

bundle installする。

ターミナル
bundle install

初期設定など

ターミナル
bundle exec rubocop --auto-gen-config

を実行すると.rubocop.yml, .rubocop_todo.ymlが生成される。

.rubocop.ymlに必要な文を追記する。

.rubocop.yml
inherit_from: .rubocop_todo.yml

require:
  - rubocop-performance # 拡張機能
  - rubocop-rails # 拡張機能
  - rubocop-rspec # 拡張機能

AllCops:
  NewCops: enable
  Exclude:
    - 'bin/*'
    - 'db/**/*'
    - 'tmp/**/*'
    - 'vendor/**/*'
  TargetRubyVersion: 3.1

# RuboCopの設定を追記する

.rubocop_todo.ymlは、作成時点で違反しているファイルを除外するための設定が入っている。

.rubocop_todo.yml
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2022-05-19 13:14:16 UTC using RuboCop version 1.29.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# This cop supports safe auto-correction (--auto-correct).
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: with_first_argument, with_fixed_indentation
Layout/ArgumentAlignment:
  Exclude:
    - 'config/application.rb'
・・・

最終的には.rubocop_todo.ymlの記述が無くなるように、ファイルを修正したり、.rubocop.ymlにRuboCopの設定を追加したりする。

コマンド

ターミナル
bundle exec rubocop # チェックのみ
ターミナル
bundle exec rubocop -a # 指摘箇所を自動で修正

参考にさせていただいた記事

Rubocop Github
Rubocop公式

2
1
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
1