Sha256: 9e08dc24c22958068ed28d99972fdff0025bcbed448c616b22e4de9fac7df98e

Contents?: true

Size: 1.75 KB

Versions: 24

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks the style of children definitions at classes and
      # modules. Basically there are two different styles:
      #
      # nested - have each child on its own line
      #   class Foo
      #     class Bar
      #     end
      #   end
      #
      # compact - combine definitions as much as possible
      #   class Foo::Bar
      #   end
      #
      # The compact style is only forced, for classes / modules with one child.
      class ClassAndModuleChildren < Cop
        include ConfigurableEnforcedStyle

        NESTED_MSG = 'Use nested module/class definitions instead of ' \
                     'compact style.'

        COMPACT_MSG = 'Use compact module/class definition instead of ' \
                      'nested style.'

        def on_class(node)
          _name, _superclass, body = *node
          check_style(node, body)
        end

        def on_module(node)
          _name, body = *node
          check_style(node, body)
        end

        private

        def check_style(node, body)
          if style == :nested
            check_nested_style(node)
          else
            check_compact_style(node, body)
          end
        end

        def check_nested_style(node)
          return unless compact_node_name?(node)
          add_offense(node, :name, NESTED_MSG)
        end

        def check_compact_style(node, body)
          return unless one_child?(body) && !compact_node_name?(node)
          add_offense(node, :name, COMPACT_MSG)
        end

        def one_child?(body)
          body && [:module, :class].include?(body.type)
        end

        def compact_node_name?(node)
          node.loc.name.source =~ /::/
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/class_and_module_children.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.35.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.35.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.34.2 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.34.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.34.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.33.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.32.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.32.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.31.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.30.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.30.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.29.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.29.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.28.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.27.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.27.0 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.26.1 lib/rubocop/cop/style/class_and_module_children.rb
rubocop-0.26.0 lib/rubocop/cop/style/class_and_module_children.rb