Sha256: 479d5eee7ba53e12ddc090c57d47bf7f6b718c155d5567428f3af868ae8cc2f6

Contents?: true

Size: 1.56 KB

Versions: 54

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks ensures source files have no utf-8 encoding comments.
      # @example
      #   # bad
      #   # encoding: UTF-8
      #   # coding: UTF-8
      #   # -*- coding: UTF-8 -*-
      class Encoding < Cop
        include RangeHelp

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

        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

54 entries across 35 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/encoding.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/encoding.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/encoding.rb
rubocop-0.85.1 lib/rubocop/cop/style/encoding.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/encoding.rb
rubocop-0.85.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.84.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.83.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.82.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.81.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.80.1 lib/rubocop/cop/style/encoding.rb
rubocop-0.80.0 lib/rubocop/cop/style/encoding.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/encoding.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/encoding.rb
rubocop-0.79.0 lib/rubocop/cop/style/encoding.rb
rubocop-0.78.0 lib/rubocop/cop/style/encoding.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/encoding.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/encoding.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/encoding.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/encoding.rb