Sha256: 8f262dcd4f4a96d5e26ba526a47ed74792b607c9c52fd3ec0b1549f5c874bd4c

Contents?: true

Size: 1.39 KB

Versions: 41

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Shared functionality between mixins that enforce naming conventions
    module ConfigurableFormatting
      include ConfigurableEnforcedStyle

      def check_name(node, name, name_range)
        if valid_name?(node, name)
          correct_style_detected
        else
          add_offense(name_range, message: message(style)) do
            report_opposing_styles(node, name)
          end
        end
      end

      def report_opposing_styles(node, name)
        alternative_styles.each do |alternative|
          return unexpected_style_detected(alternative) if valid_name?(node, name, alternative)
        end
      end

      def valid_name?(node, name, given_style = style)
        name.match?(self.class::FORMATS.fetch(given_style)) ||
          class_emitter_method?(node, name)
      end

      # A class emitter method is a singleton method in a class/module, where
      # the method has the same name as a class defined in the class/module.
      def class_emitter_method?(node, name)
        return false unless node.parent && node.defs_type?

        # a class emitter method may be defined inside `def self.included`,
        # `def self.extended`, etc.
        node = node.parent while node.parent.defs_type?

        node.parent.each_child_node(:class).any? do |c|
          c.loc.name.is?(name.to_s)
        end
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 4 rubygems

Version Path
rubocop-1.4.2 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.4.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.4.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.3.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.3.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.2.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.1.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.0.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.93.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.93.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.92.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.91.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.91.0 lib/rubocop/cop/mixin/configurable_formatting.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.90.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.89.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.89.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.88.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-0.87.1 lib/rubocop/cop/mixin/configurable_formatting.rb