Sha256: 7c915d5724421b490901c980c3060a14edbdfea4edcad9fb61be1027cf00dec1

Contents?: true

Size: 1.69 KB

Versions: 6778

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/AsciiComments

module RuboCop
  module Cop
    module Style
      # This cop checks for non-ascii (non-English) characters
      # in comments. You could set an array of allowed non-ascii chars in
      # AllowedChars attribute (empty by default).
      #
      # @example
      #   # bad
      #   # Translates from English to 日本語。
      #
      #   # good
      #   # Translates from English to Japanese
      class AsciiComments < Cop
        include RangeHelp

        MSG = 'Use only ascii symbols in comments.'.freeze

        def investigate(processed_source)
          processed_source.each_comment do |comment|
            next if comment.text.ascii_only?
            next if only_allowed_non_ascii_chars?(comment.text)

            add_offense(comment, location: first_offense_range(comment))
          end
        end

        private

        def first_offense_range(comment)
          expression    = comment.loc.expression
          first_offense = first_non_ascii_chars(comment.text)

          start_position = expression.begin_pos +
                           comment.text.index(first_offense)
          end_position   = start_position + first_offense.length

          range_between(start_position, end_position)
        end

        def first_non_ascii_chars(string)
          string.match(/[^[:ascii:]]+/).to_s
        end

        def only_allowed_non_ascii_chars?(string)
          non_ascii = string.scan(/[^[:ascii:]]/)
          (non_ascii - allowed_non_ascii_chars).empty?
        end

        def allowed_non_ascii_chars
          cop_config['AllowedChars'] || []
        end
      end
    end
  end
end
# rubocop:enable Style/AsciiComments

Version data entries

6,778 entries across 6,772 versions & 24 rubygems

Version Path
rubocop-0.67.1 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.67.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.66.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.65.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.64.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.63.1 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.63.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.62.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.61.1 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.61.0 lib/rubocop/cop/style/ascii_comments.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/ascii_comments.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/ascii_comments.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/ascii_comments.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/rubocop-0.60.0/lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.60.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.59.2 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.59.1 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.59.0 lib/rubocop/cop/style/ascii_comments.rb