Sha256: d29cd682bf78e4860f5cb9bd7ef3ba317188fcfa9fa6f9aee22dea1f266f2ed3

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks ensures source files have no utf-8 encoding comments.
      class Encoding < Cop
        include RangeHelp

        MSG_UNNECESSARY = 'Unnecessary utf-8 encoding comment.'.freeze
        ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/
        SHEBANG = '#!'.freeze

        def investigate(processed_source)
          return if processed_source.buffer.source.empty?

          line_number = encoding_line_number(processed_source)
          return unless (@message = offense(processed_source, line_number))

          range = processed_source.buffer.line_range(line_number + 1)
          add_offense(range, location: range, message: @message)
        end

        def autocorrect(range)
          lambda do |corrector|
            corrector.remove(range_with_surrounding_space(range: range,
                                                          side: :right))
          end
        end

        private

        def offense(processed_source, line_number)
          line = processed_source[line_number]

          MSG_UNNECESSARY if encoding_omitable?(line)
        end

        def encoding_omitable?(line)
          line =~ ENCODING_PATTERN
        end

        def encoding_line_number(processed_source)
          line_number = 0
          line_number += 1 if processed_source[line_number].start_with?(SHEBANG)
          line_number
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/style/encoding.rb
rubocop-0.57.2 lib/rubocop/cop/style/encoding.rb
rubocop-0.57.1 lib/rubocop/cop/style/encoding.rb
rubocop-0.57.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.56.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.55.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.54.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.53.0 lib/rubocop/cop/style/encoding.rb