Search code examples
rubysuppress-warningssuppressrubocop

How to make rubocop output nothing when there are no warnings or offenses?


I'd like rubocop but have it create no output if there are no offenses. I've played with the documented command line options, and taken a quick peek a the config option and source code without seeing anything promising.

The closest I've come is rubocop -f o, but even that produces a summary line:

$ rubocop -f o

--
0  Total

Any ideas on how to suppress output on a clean, successful run?


Solution

  • I believe there's no predefined option to do what you ask. The best option I think is to implement a custom formatter with the desired behaviour and use that to run RuboCop. As a super simple example consider the following, inspired by RuboCop's SimpleTextFormatter:

    # my_rubocop_formatter.rb
    class MyRuboCopFormatter < RuboCop::Formatter::BaseFormatter
      def started(target_files)
        @total_offenses = 0
      end
      def file_finished(file, offenses)
        unless offenses.empty?
          output.puts(offenses)
          @total_offenses += offenses.count
        end
      end
      def finished(inspected_files)
        puts @total_offenses unless @total_offenses.zero?
      end
    end
    

    You can run RuboCop using it with the following command:

    rubocop -r '/path/to/my_rubocop_formatter.rb' \ 
            -f MyRuboCopFormatter file_to_check.rb