Sha256: 38197f73b8b3f6e84e800d1db80347841541909ecd37fe776e38803c647c0c8c

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'pronto'
require 'rubocop'
require 'tempfile'

module Pronto
  class Rubocop < Runner
    def initialize
      @cli = ::Rubocop::CLI.new
    end

    def run(diffs)
      return [] unless diffs

      diffs.select { |patch| patch.additions > 0 }
           .map { |patch| inspect(patch) }
    end

    def inspect(patch)
      new_file = patch.delta.new_file

      if File.extname(new_file[:path]) == '.rb'
        repo = patch.diff.instance_eval { @owner.instance_eval { @owner } }
        blob = repo.lookup(new_file[:oid])
        file = create_tempfile(blob)
        offences = @cli.inspect_file(file.path)
        messages_from(offences, patch)
      end
    end

    def added_lines(patch)
      patch.map do |hunk|
        hunk.lines.select(&:addition?)
      end.flatten.compact
    end

    def messages_from(offences, patch)
      offences.map do |offence|
        line = added_lines(patch).select do |added_line|
          added_line.new_lineno == offence.line
        end.first

        path = patch.delta.new_file[:path]
        message_from(path, offence, line) if line
      end.compact
    end

    def message_from(path, offence, line)
      Pronto::Message.new(path,
                          line,
                          level(offence.severity),
                          offence.message)
    end

    def level(severity)
      case severity
      when :refactor, :convention
        :info
      when :warning
        :warning
      when :error
        :error
      when :fatal
        :fatal
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pronto-rubocop-0.0.1 lib/pronto/rubocop.rb