Sha256: 78e7cd736564dace42738c7525f623daaf7a09bb5a19c75cb5d43e32fd89988b

Contents?: true

Size: 1.69 KB

Versions: 17

Compression:

Stored size: 1.69 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.
      module Syntax
        PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
                                       :end_pos)

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

        def self.offenses_from_processed_source(processed_source)
          offenses = []

          if processed_source.parser_error
            offenses << offense_from_error(processed_source.parser_error)
          end

          processed_source.diagnostics.each do |diagnostic|
            offenses << offense_from_diagnostic(diagnostic,
                                                processed_source.ruby_version)
          end

          offenses
        end

        def self.offense_from_diagnostic(diagnostic, ruby_version)
          Offense.new(
            diagnostic.level,
            diagnostic.location,
            "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
            'configure using `TargetRubyVersion` parameter, under `AllCops`)',
            COP_NAME
          )
        end

        def self.offense_from_error(error)
          message = beautify_message(error.message)
          Offense.new(:fatal, ERROR_SOURCE_RANGE, message, COP_NAME)
        end

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

        private_class_method :beautify_message
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/syntax.rb
rubocop-0.49.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.49.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.48.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.48.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.47.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.47.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.46.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.45.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.44.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.44.0 lib/rubocop/cop/lint/syntax.rb