Sha256: d6c30cc306d39b0b2bc6f8d47df958d9e26d08845d937812cb064a51d6d35d4f

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    # This module provides functionality for checking if names match the
    # configured EnforcedStyle.
    module ConfigurableNaming
      include ConfigurableEnforcedStyle

      SNAKE_CASE = /^@?[\da-z_]+[!?=]?$/
      CAMEL_CASE = /^@?[a-z][\da-zA-Z]+[!?=]?$/

      def check(node, range)
        return unless range

        name = range.source.to_sym
        return if operator?(name)

        if matches_config?(name)
          correct_style_detected
        else
          add_offense(node, range, message(style)) do
            opposite_style_detected
          end
        end
      end

      def matches_config?(name)
        name =~ (style == :snake_case ? SNAKE_CASE : CAMEL_CASE)
      end

      # Returns a range containing the method name after the given regexp and
      # a dot.
      def after_dot(node, method_name_length, regexp)
        expr = node.loc.expression
        match = /\A#{regexp}\s*\.\s*/.match(expr.source)
        return unless match
        offset = match[0].length
        begin_pos = expr.begin_pos + offset
        Parser::Source::Range.new(expr.source_buffer, begin_pos,
                                  begin_pos + method_name_length)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/mixin/configurable_naming.rb
rubocop-0.21.0 lib/rubocop/cop/mixin/configurable_naming.rb
rubocop-0.20.1 lib/rubocop/cop/mixin/configurable_naming.rb
rubocop-0.20.0 lib/rubocop/cop/mixin/configurable_naming.rb
rubocop-0.19.1 lib/rubocop/cop/mixin/configurable_naming.rb
rubocop-0.19.0 lib/rubocop/cop/mixin/configurable_naming.rb