Sha256: 9b2dbc7715abe7e5f2efa74f0cc48def496ddaa620c7445f84adeaf1a3d9749b

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for non-ascii (non-English) characters
      # in comments.
      class AsciiComments < Cop
        MSG = 'Use only ascii symbols in comments.'.freeze

        def investigate(processed_source)
          processed_source.comments.each do |comment|
            unless comment.text.ascii_only?
              add_offense(comment, first_offense_range(comment))
            end
          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

          Parser::Source::Range.new(comment.loc.expression.source_buffer,
                                    start_position, end_position)
        end

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/ascii_comments.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.42.0 lib/rubocop/cop/style/ascii_comments.rb
rubocop-0.41.2 lib/rubocop/cop/style/ascii_comments.rb