Sha256: 6e14ea072f9fe28dc63c60b0b65d33e38e5125aacf91c5d1b70769936f1f2191

Contents?: true

Size: 1.37 KB

Versions: 30

Compression:

Stored size: 1.37 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)) { report_opposing_styles(node, name) }
        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
        unrecognized_style_detected
      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? { |c| c.loc.name.is?(name.to_s) }
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 6 rubygems

Version Path
rubocop-1.68.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.67.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.66.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.66.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.65.1 lib/rubocop/cop/mixin/configurable_formatting.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.65.0 lib/rubocop/cop/mixin/configurable_formatting.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.64.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.63.4 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.63.0 lib/rubocop/cop/mixin/configurable_formatting.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.62.1 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.62.0 lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.61.0 lib/rubocop/cop/mixin/configurable_formatting.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/mixin/configurable_formatting.rb
rubocop-1.60.2 lib/rubocop/cop/mixin/configurable_formatting.rb