LoginSignup
8
15

More than 5 years have passed since last update.

AtomでRubocopを使う

Posted at

Rubocopとは

Rubocopは、rubyのコードがRubyコーディング規約に沿っているかどうかをチェックするためのユーティリティー(Linter)。

使い方

Rubocopをインストールする

$ gem install rubocop

コマンドラインからの使い方

$ rubocopコマンドで、*.rbファイルの構造のチェックができるようになる。

$ rubocop .\projects_controller.rb
Inspecting 1 file

Offenses:

projects_controller.rb:2:37: C: Use %i or %I for an array of symbols.
  before_action :find_master, only: [:create, :new, :update]
                                    ^^^^^^^^^^^^^^^^^^^^^^^^
projects_controller.rb:14:7: C: Annotation keywords like TODO should be all upper case, followed by a colon, and a space
, then a note describing the problem.
    # TODO:disabled タスク #654
      ^^^^^
projects_controller.rb:21:58: C: Space missing after comma.
      DatabaseManage.create!(backup_file_path: 'CAN/test',backup_date: Date.today, project: @project)
                                                         ^
projects_controller.rb:21:77: C: Do not use Date.today without zone. Use Time.zone.today instead.
      DatabaseManage.create!(backup_file_path: 'CAN/test',backup_date: Date.today, project: @project)
                                                                            ^^^^^
projects_controller.rb:21:81: C: Line is too long. [101/80]
      DatabaseManage.create!(backup_file_path: 'CAN/test',backup_date: Date.today, project: @project)
                                                                                ^^^^^^^^^^^^^^^^^^^^^
projects_controller.rb:49:3: C: Keep a blank line before and after private.
  private
  ^^^^^^^
projects_controller.rb:55:5: C: Annotation keywords like TODO should be all upper case, followed by a colon, and a space
, then a note describing the problem.
  # TODO:disabled タスク #654
    ^^^^^
projects_controller.rb:56:32: C: Space inside parentheses detected.
  def create_sign(name, project )
                               ^
projects_controller.rb:57:50: C: Space missing after colon.
    Sign.create!(name: name, active: '1', vartype:'2', unit:'3',
                                                 ^
projects_controller.rb:57:60: C: Space missing after colon.
    Sign.create!(name: name, active: '1', vartype:'2', unit:'3',
                                                           ^
projects_controller.rb:58:5: C: Align the elements of a hash literal if they span more than one line.
    exchange_rate:'4.0', priority:'5', input_module:'6', output_moduel:'7',
    ^^^^^^^^^^^^^^^^^^^
projects_controller.rb:58:18: C: Space missing after colon.
    exchange_rate:'4.0', priority:'5', input_module:'6', output_moduel:'7',
                 ^
projects_controller.rb:58:34: C: Space missing after colon.
    exchange_rate:'4.0', priority:'5', input_module:'6', output_moduel:'7',
                                 ^
projects_controller.rb:58:52: C: Space missing after colon.
    exchange_rate:'4.0', priority:'5', input_module:'6', output_moduel:'7',
                                                   ^
projects_controller.rb:58:71: C: Space missing after colon.
    exchange_rate:'4.0', priority:'5', input_module:'6', output_moduel:'7',
                                                                      ^
projects_controller.rb:59:5: C: Align the elements of a hash literal if they span more than one line.
    input_period:'8', output_period:'9', access_level:'10', project:project,
    ^^^^^^^^^^^^^^^^
projects_controller.rb:59:17: C: Space missing after colon.
    input_period:'8', output_period:'9', access_level:'10', project:project,
                ^
projects_controller.rb:59:36: C: Space missing after colon.
    input_period:'8', output_period:'9', access_level:'10', project:project,
                                   ^
projects_controller.rb:59:54: C: Space missing after colon.
    input_period:'8', output_period:'9', access_level:'10', project:project,
                                                     ^
projects_controller.rb:59:68: C: Space missing after colon.
    input_period:'8', output_period:'9', access_level:'10', project:project,
                                                                   ^
projects_controller.rb:60:5: C: Align the elements of a hash literal if they span more than one line.
    description: "project:#{project.name},name:#{name}")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
projects_controller.rb:64:81: C: Line is too long. [102/80]
    params.require(:project).permit(:name, :communication_protocol_id, :qines_version_id, :byte_order)
                                                                                ^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 22 offenses detected

Atomにrubocop用のプラグインをインストール

  • Atomの[ファイル]→[環境設定]→[Install]タブに「rubocop」と入力し[Packages]ボタンをクリック
  • [linter-rubocop]が表示されるのでインストールする
  • ビジュアルにrubocopの内容を確認できるようになる(青波線や赤波線の部分) rubocop.png

Rubocopの設定

  • 基本的にRubyコーディング規約に従っているのでカスタマイズの必要はない。
  • 自分のホームフォルダに設定ファイル(~/.rubocop.yml)を配置することでカスタマイズ可能。
  • 何も設定しないと結構つらいので、私は以下のように設定しています。
.rubocop.yml
AllCops:
  TargetRubyVersion: 2.4

Rails:
  Enabled: true

AsciiComments:
  Enabled: false

Documentation:
  Enabled: false

Style/FrozenStringLiteralComment:
  Enabled: false

Style/EndOfLine:
  Enabled: false
8
15
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
8
15