Sha256: 2e8c3d3096a4de2b4ae95f33f7206d8ee355e073228572bc3043b759b7ce0b4d

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

# encoding: UTF-8

require 'json'
require 'rubocop'
require 'face_control/comment'

module FaceControl
  module Checkers
    class RuboCop
      attr_writer :options

      def relevant_globs
        %w(
          *.rb
          *.rake
          Capfile
          Gemfile
          Rakefile
          Vagrantfile
        )
      end

      def command(filenames)
        "rubocop --format json #{filenames}"
      end

      def parse(command_output)
        JSON.parse(command_output)['files'].map do |file|
          file['offenses'].reject do |offense|
            ignored_severities.include?(offense['severity'])
          end.map do |offense|
            Comment.new(
              file: file['path'],
              line: offense['location']['line'],
              text: text(offense, file)
            )
          end
        end
      end

      private

      def ignored_severities
        @options.fetch(:ignored_severities, [])
      end

      def text(offense, file)
        text = offense['message']

        if (link = style_guide_url(offense))
          text << " — [Guide](#{link})"
        end

        if can_be_autocorrected?(offense)
          text << " (Run `rubocop -a #{file['path']}` to fix.)"
        end

        text
      end

      def style_guide_url(offense)
        cop_name = offense['cop_name']
        cop_config = ::RuboCop::ConfigLoader.default_configuration[cop_name]
        cop_config['StyleGuide']
      end

      def can_be_autocorrected?(offense)
        !offense['corrected'].nil?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
face_control-0.7.0 lib/face_control/checkers/rubocop.rb