lib/codeqa/checkers/rubocop_full.rb in codeqa-0.3.1 vs lib/codeqa/checkers/rubocop_full.rb in codeqa-0.4.0.pre
- old
+ new
@@ -1,5 +1,7 @@
+require 'ostruct'
+
module Codeqa
module Checkers
class Rubocop < Checker
def self.check?(sourcefile)
sourcefile.ruby?
@@ -19,35 +21,59 @@
def check
return unless self.class.rubocop?
with_existing_file do |filename|
args = config_args << filename
- success, captured = capture do
+ 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
- errors.add(nil, captured) unless success
+ handle_rubocop_results(raw_json) unless success
end
end
private
def config_args
%w(--auto-correct --fail-level warning)
end
def default_args
- %w(--display-cop-names --format emacs)
+ %w(--format json)
end
+ def handle_rubocop_results(raw)
+ data = JSON.parse raw, :object_class => OpenStruct
+ data.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
def self.rubocop?
@loaded ||= begin
require 'rubocop'
true
end
+ end
+
+ # Since using the json format we only care about stdout
+ # stderr will be silent
+ 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
end
end
end