Sha256: 95c6de6d8ba49c5db8240daf8caaa7bc5e18c0ae3b3e8e559532e637378550ad

Contents?: true

Size: 1.76 KB

Versions: 6822

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

6,822 entries across 6,816 versions & 25 rubygems

Version Path
ory-client-0.0.1.alpha78 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha77 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha76 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha75 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha74 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha73 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha72 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha71 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha70 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha69 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha68 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha67 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha66 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-oathkeeper-client-0.38.19.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-oathkeeper-client-0.38.18.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-oathkeeper-client-0.38.17.beta1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
avalara_sdk-2.4.7 vendor/bundle/ruby/2.6.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha58 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha57 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb
ory-client-0.0.1.alpha56 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/naming/ascii_identifiers.rb