Sha256: a29be53c9551f66fc1c45322b188e6db072c7ba9273696709b783014a04c87a3

Contents?: true

Size: 1.85 KB

Versions: 17

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This is actually not a cop and inspects nothing. It just provides
      # methods to repack Parser's diagnostics/errors into RuboCop's offenses.
      class Syntax < Cop
        PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
                                       :end_pos)

        ERROR_SOURCE_RANGE = PseudoSourceRange.new(1, 0, '', 0, 1).freeze

        def self.offenses_from_processed_source(processed_source,
                                                config, options)
          cop = new(config, options)

          if processed_source.parser_error
            cop.add_offense_from_error(processed_source.parser_error)
          end

          processed_source.diagnostics.each do |diagnostic|
            cop.add_offense_from_diagnostic(diagnostic,
                                            processed_source.ruby_version)
          end

          cop.offenses
        end

        def add_offense_from_diagnostic(diagnostic, ruby_version)
          message =
            "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
            'configure using `TargetRubyVersion` parameter, under `AllCops`)'
          add_offense(nil,
                      location: diagnostic.location,
                      message: message,
                      severity: diagnostic.level)
        end

        def add_offense_from_error(error)
          message = beautify_message(error.message)
          add_offense(nil,
                      location: ERROR_SOURCE_RANGE,
                      message: message,
                      severity: :fatal)
        end

        private

        def beautify_message(message)
          message = message.capitalize
          message << '.' unless message.end_with?('.')
          message
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/lint/syntax.rb
rubocop-0.59.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.59.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.58.2 lib/rubocop/cop/lint/syntax.rb
rubocop-0.58.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.58.0 lib/rubocop/cop/lint/syntax.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/lint/syntax.rb
rubocop-0.57.2 lib/rubocop/cop/lint/syntax.rb
rubocop-0.57.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.57.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.56.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.55.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.54.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.53.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.52.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.52.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.51.0 lib/rubocop/cop/lint/syntax.rb