class Codeqa::Checkers::Rubocop

Public Class Methods

available?() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 10
def self.available?
  rubocop?
end
check?(sourcefile) click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 6
def self.check?(sourcefile)
  sourcefile.ruby?
end

Private Class Methods

rubocop?() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 57
def self.rubocop?
  @loaded ||= begin
                require 'rubocop'
                true
              end
end

Public Instance Methods

check() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 22
def check
  return unless self.class.rubocop?
  with_existing_file do |filename|
    args = config_args << filename
    success, raw_json = capture do
      if defined?(RuboCop) # its RuboCop since 0.24
        ::RuboCop::CLI.new.run(default_args + args) == 0
      else
        ::Rubocop::CLI.new.run(default_args + args) == 0
      end
    end
    handle_rubocop_results(raw_json) unless success
  end
end
hint() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 18
def hint
  'Rubocop does not like your syntax, please fix your code.'
end
name() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 14
def name
  'rubocop'
end

Private Instance Methods

capture() { || ... } click to toggle source

Since using the json format we only care about stdout stderr will be silenced (internal rubocop errors/warning will be printed there)

# File lib/codeqa/checkers/rubocop_full.rb, line 67
def capture
  $stdout, stdout = StringIO.new, $stdout
  $stderr, stderr = StringIO.new, $stderr
  result = yield
  # [result, $stdout.string + $stderr.string]
  [result, $stdout.string]
ensure
  $stdout = stdout
  $stderr = stderr
end
config_args() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 39
def config_args
  %w(--auto-correct)
end
default_args() click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 43
def default_args
  %w(--format json)
end
handle_rubocop_results(raw) click to toggle source
# File lib/codeqa/checkers/rubocop_full.rb, line 47
def handle_rubocop_results(raw)
  MultiJson.load(raw)['files'].
           reject{ |f| f['offenses'].empty? }.
           each do |file|
             file['offenses'].each do |offense|
               position = [offense['location']['line'], offense['location']['column']]
               errors.add(position, "#{offense['cop_name']}: #{offense['message']}")
             end
           end
end