Sha256: a6b923c2619ea849c1bbf181b2a3461fd99599c1ed72893a93c9b01a5568cbf5

Contents?: true

Size: 1.67 KB

Versions: 24

Compression:

Stored size: 1.67 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 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?

          comments.each do |line_number, comment|
            next unless offense?(comment)

            register_offense(line_number, comment)
          end
        end

        private

        def comments
          processed_source.lines.each.with_index.with_object({}) do |(line, line_number), comments|
            next if line.start_with?(SHEBANG)

            comment = MagicComment.parse(line)
            return comments unless comment.valid?

            comments[line_number + 1] = comment
          end
        end

        def offense?(comment)
          comment.encoding_specified? && comment.encoding.casecmp('utf-8').zero?
        end

        def register_offense(line_number, comment)
          range = processed_source.buffer.line_range(line_number)

          add_offense(range) do |corrector|
            text = comment.without(:encoding)

            if text.blank?
              corrector.remove(range_with_surrounding_space(range: range, side: :right))
            else
              corrector.replace(range, text)
            end
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/encoding.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/encoding.rb
rubocop-1.29.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.29.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.28.2 lib/rubocop/cop/style/encoding.rb
rubocop-1.28.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.28.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.27.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.26.1 lib/rubocop/cop/style/encoding.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/encoding.rb
rubocop-1.26.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.25.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.25.0 lib/rubocop/cop/style/encoding.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/encoding.rb
rubocop-1.24.1 lib/rubocop/cop/style/encoding.rb
rubocop-1.24.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.23.0 lib/rubocop/cop/style/encoding.rb
rubocop-1.22.3 lib/rubocop/cop/style/encoding.rb
rubocop-1.22.2 lib/rubocop/cop/style/encoding.rb
rubocop-1.22.1 lib/rubocop/cop/style/encoding.rb