Sha256: 88a29620475c5b56642b3d734ba0ad4f30fa597424c0cbcbb2bfb5713909f32e

Contents?: true

Size: 1.45 KB

Versions: 50

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.
      # @example
      #   # bad
      #   # encoding: UTF-8
      #   # coding: UTF-8
      #   # -*- coding: UTF-8 -*-
      class Encoding < Base
        include RangeHelp
        extend AutoCorrector

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

        def on_new_investigation
          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, message: @message) 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)
          ENCODING_PATTERN.match?(line)
        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

50 entries across 50 versions & 5 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/encoding.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/style/encoding.rb
rubocop-1.19.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.18.4 lib/rubocop/cop/style/encoding.rb
rubocop-1.18.3 lib/rubocop/cop/style/encoding.rb
rubocop-1.18.2 lib/rubocop/cop/style/encoding.rb
rubocop-1.18.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.18.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.17.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.16.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.16.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.15.0 lib/rubocop/cop/style/encoding.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/style/encoding.rb
rubocop-1.14.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.13.0 lib/rubocop/cop/style/encoding.rb