Sha256: ac565ae09f48135b6a70229d50c8b075c43f013e5a02d16359cb424ba5a9dcbd

Contents?: true

Size: 1.76 KB

Versions: 11

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

# rubocop:disable Style/AsciiComments

module RuboCop
  module Cop
    module Naming
      # This cop checks for non-ascii characters in identifier names.
      #
      # @example
      #   # bad
      #   def καλημερα # Greek alphabet (non-ascii)
      #   end
      #
      #   # bad
      #   def こんにちはと言う # Japanese character (non-ascii)
      #   end
      #
      #   # bad
      #   def hello_🍣 # Emoji (non-ascii)
      #   end
      #
      #   # good
      #   def say_hello
      #   end
      #
      #   # bad
      #   신장 = 10 # Hangul character (non-ascii)
      #
      #   # good
      #   height = 10
      #
      #   # bad
      #   params[:عرض_gteq] # Arabic character (non-ascii)
      #
      #   # good
      #   params[:width_gteq]
      #
      class AsciiIdentifiers < Cop
        include RangeHelp

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

        def investigate(processed_source)
          processed_source.each_token do |token|
            next unless token.type == :tIDENTIFIER && !token.text.ascii_only?
            add_offense(token, location: first_offense_range(token))
          end
        end

        private

        def first_offense_range(identifier)
          expression    = identifier.pos
          first_offense = first_non_ascii_chars(identifier.text)

          start_position = expression.begin_pos +
                           identifier.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
      end
    end
  end
end
# rubocop:enable Style/AsciiComments

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
rubocop-0.58.2 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.58.1 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.58.0 lib/rubocop/cop/naming/ascii_identifiers.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.57.2 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.57.1 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.57.0 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.56.0 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.55.0 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.54.0 lib/rubocop/cop/naming/ascii_identifiers.rb
rubocop-0.53.0 lib/rubocop/cop/naming/ascii_identifiers.rb