Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Rubocop | 規約をチェックするツールRubocopのインストール

概要

規約をチェックするツールRubocopのインストール

詳細

規約をチェックするツールRubocopのインストールします

インストール

gem i rubocop

規約

デフォルトの規約から設定を変更したい場合は
.rubocop.ymlを作成する。

試行

サンプルとしてrspec_piccoloのソースをチェックしてみます。

git clone https://github.com/tbpgr/rspec_piccolo.git
cd rspec_piccolo

# 1行80文字制限をオフにする
cat <<EOS>.rubocop.yml
# Limit lines to 80 characters.
LineLength:
  Enabled: false
EOS

rubocop lib/rspec_piccolo_core
$ rubocop lib/rspec_piccolo.rb
Inspecting 1 file
W

Offences:

lib/rspec_piccolo.rb:1:1: C: Missing utf-8 encoding comment.
require "rspec_piccolo/version"
^
lib/rspec_piccolo.rb:1:9: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
require "rspec_piccolo/version"
        ^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:2:9: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
require "erb"
        ^^^^^
lib/rspec_piccolo.rb:7:3: C: Missing space after #.
  #= RSpecPiccolo Core
  ^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:8:3: C: Class definition is too long. [106/100]
  class Core
  ^^^^^
lib/rspec_piccolo.rb:9:5: C: Missing space after #.
    #== RSPec class template
    ^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:10:19: C: Surrounding space missing for operator '='.
    CLASS_TEMPLATE=<<-EOS
                  ^
lib/rspec_piccolo.rb:21:24: C: Surrounding space missing for operator '='.
    REPORTABLE_PREPARE =<<-EOS
                       ^
lib/rspec_piccolo.rb:38:5: C: Missing space after #.
    #== RSPec method template
    ^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:39:20: C: Surrounding space missing for operator '='.
    METHOD_TEMPLATE=<<-EOS
                   ^
lib/rspec_piccolo.rb:84:1: C: Trailing whitespace detected.
lib/rspec_piccolo.rb:85:21: C: Surrounding space missing for operator '='.
    REPORTABLE_CASE =<<-EOS
                    ^
lib/rspec_piccolo.rb:92:27: C: Surrounding space missing for operator '='.
    REPORTABLE_CASE_AFTER =<<-EOS
                          ^
lib/rspec_piccolo.rb:97:5: C: Missing space after #.
    #== initialize
    ^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:99:19: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      @contents = ""
                  ^^
lib/rspec_piccolo.rb:102:5: C: Missing space after #.
    #== generate rspec test case
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:103:5: C: Missing space after #.
    #=== params
    ^^^^^^^^^^^
lib/rspec_piccolo.rb:113:49: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      File.open("./spec/#{class_path}_spec.rb", "w") {|f|f.puts @contents}
                                                ^^^
lib/rspec_piccolo.rb:113:54: C: Space between { and | missing.
      File.open("./spec/#{class_path}_spec.rb", "w") {|f|f.puts @contents}
                                                     ^
lib/rspec_piccolo.rb:113:74: C: Space missing inside }.
      File.open("./spec/#{class_path}_spec.rb", "w") {|f|f.puts @contents}
                                                                         ^
lib/rspec_piccolo.rb:116:5: C: Keep a blank line before and after private.
    private
    ^^^^^^^
lib/rspec_piccolo.rb:118:7: C: Provide an exception class and message as arguments to raise.
      raise RSpecPiccoloError.new("class_name is not allowed nil value") if class_name.nil?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:118:7: C: Use `fail` instead of `raise` to signal exceptions.
      raise RSpecPiccoloError.new("class_name is not allowed nil value") if class_name.nil?
      ^^^^^
lib/rspec_piccolo.rb:118:35: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      raise RSpecPiccoloError.new("class_name is not allowed nil value") if class_name.nil?
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:119:7: C: Provide an exception class and message as arguments to raise.
      raise RSpecPiccoloError.new("class_name is not allowed empty value") if class_name.empty?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:119:7: C: Use `fail` instead of `raise` to signal exceptions.
      raise RSpecPiccoloError.new("class_name is not allowed empty value") if class_name.empty?
      ^^^^^
lib/rspec_piccolo.rb:119:35: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      raise RSpecPiccoloError.new("class_name is not allowed empty value") if class_name.empty?
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:123:7: C: Provide an exception class and message as arguments to raise.
      raise RSpecPiccoloError.new("class_path is not allowed nil value") if class_path.nil?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:123:7: C: Use `fail` instead of `raise` to signal exceptions.
      raise RSpecPiccoloError.new("class_path is not allowed nil value") if class_path.nil?
      ^^^^^
lib/rspec_piccolo.rb:123:35: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      raise RSpecPiccoloError.new("class_path is not allowed nil value") if class_path.nil?
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:124:7: C: Provide an exception class and message as arguments to raise.
      raise RSpecPiccoloError.new("class_path is not allowed empty value") if class_path.empty?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:124:7: C: Use `fail` instead of `raise` to signal exceptions.
      raise RSpecPiccoloError.new("class_path is not allowed empty value") if class_path.empty?
      ^^^^^
lib/rspec_piccolo.rb:124:35: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      raise RSpecPiccoloError.new("class_path is not allowed empty value") if class_path.empty?
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:127:5: C: Cyclomatic complexity for generate_method_template is too high. [9/6]
    def generate_method_template(class_name, method_names, reportable)
    ^^^
lib/rspec_piccolo.rb:127:5: C: Method has too many lines. [15/10]
    def generate_method_template(class_name, method_names, reportable)
    ^^^
lib/rspec_piccolo.rb:128:14: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      return "" if method_names.nil?
             ^^
lib/rspec_piccolo.rb:130:7: W: Useless assignment to variable - reportable_case
      reportable_case = reportable ? REPORTABLE_CASE.chop : ""
      ^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:130:61: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      reportable_case = reportable ? REPORTABLE_CASE.chop : ""
                                                            ^^
lib/rspec_piccolo.rb:135:40: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        method_name = method_name.gsub("@c", "") if is_class_method
                                       ^^^^
lib/rspec_piccolo.rb:135:46: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        method_name = method_name.gsub("@c", "") if is_class_method
                                             ^^
lib/rspec_piccolo.rb:136:9: W: Useless assignment to variable - given_src
        given_src = is_class_method ? "# nothing" : "#{instance_name} = #{class_name}.new"
        ^^^^^^^^^
lib/rspec_piccolo.rb:136:39: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        given_src = is_class_method ? "# nothing" : "#{instance_name} = #{class_name}.new"
                                      ^^^^^^^^^^^
lib/rspec_piccolo.rb:137:9: W: Useless assignment to variable - when_src
        when_src = is_class_method ? "# actual = #{class_name}.#{method_name}" : "# actual = #{instance_name}.#{method_name}"
        ^^^^^^^^
lib/rspec_piccolo.rb:138:76: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        reportable_case_before = reportable ? REPORTABLE_CASE_BEFORE.dup : ""
                                                                           ^^
lib/rspec_piccolo.rb:139:38: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        reportable_case_before.gsub!("method_name", method_name)
                                     ^^^^^^^^^^^^^
lib/rspec_piccolo.rb:140:9: W: Useless assignment to variable - reportable_case_after
        reportable_case_after = reportable ? REPORTABLE_CASE_AFTER.dup.chop : ""
        ^^^^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:140:79: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
        reportable_case_after = reportable ? REPORTABLE_CASE_AFTER.dup.chop : ""
                                                                              ^^
lib/rspec_piccolo.rb:152:7: W: Useless assignment to variable - reportable_prepare
      reportable_prepare = reportable ? REPORTABLE_PREPARE : ""
      ^^^^^^^^^^^^^^^^^^
lib/rspec_piccolo.rb:152:62: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      reportable_prepare = reportable ? REPORTABLE_PREPARE : ""
                                                             ^^
lib/rspec_piccolo.rb:157:42: C: Space missing after semicolon.
  class RSpecPiccoloError < StandardError;end
                                         ^
1 file inspected, 50 offences detected

たらふくでました(汗
多くがスペーシングと、変数埋め込みの内ダブルクォートの文字列はシングルクォートに直せ、という警告。
ERB使用部とかは、変数が未使用と見なされてしまっていて誤検出。これは少し嫌ですね。

failとraiseの使い分けとかは見たことないが、皆さん意識しているのだろうか?
使い分けどころかfailを使ってるコードにあったことがない。

自動補正

  • aオプションで自動補正可能な箇所は補正してくれます。
$ rubocop lib/rspec_piccolo.rb -a
:
略
:
1 file inspected, 50 offences detected, 36 offences corrected

スペーシング、クォートあたりは自動で補正してくれました。