Sha256: 434fc833b95d09b13806cd67376b62e70b9946a0fac7f703899410b62a1e729d

Contents?: true

Size: 1.58 KB

Versions: 22

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

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 (copyright notice "©" by default).
      #
      # @example
      #   # bad
      #   # Translates from English to 日本語。
      #
      #   # good
      #   # Translates from English to Japanese
      class AsciiComments < Base
        include RangeHelp

        MSG = 'Use only ascii symbols in comments.'

        def on_new_investigation
          processed_source.comments.each do |comment|
            next if comment.text.ascii_only?
            next if only_allowed_non_ascii_chars?(comment.text)

            add_offense(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

Version data entries

22 entries across 22 versions & 4 rubygems

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