Sha256: bfa97db811796f5939457cdc5525bbf922f0f85f44f5c2f8da1c3855fc9f095b

Contents?: true

Size: 1.71 KB

Versions: 14

Compression:

Stored size: 1.71 KB

Contents

# encoding: utf-8
# 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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/syntax.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/syntax.rb
rubocop-0.43.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.42.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.41.2 lib/rubocop/cop/lint/syntax.rb
rubocop-0.41.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.41.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.40.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.39.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.38.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.37.2 lib/rubocop/cop/lint/syntax.rb
rubocop-0.37.1 lib/rubocop/cop/lint/syntax.rb
rubocop-0.37.0 lib/rubocop/cop/lint/syntax.rb
rubocop-0.36.0 lib/rubocop/cop/lint/syntax.rb