class RuboCop::Cop::Style::ClassAndModuleChildren

This cop checks the style of children definitions at classes and modules. Basically there are two different styles:

@example EnforcedStyle: nested (default)

# good
# have each child on its own line
class Foo
  class Bar
  end
end

@example EnforcedStyle: compact

# good
# combine definitions as much as possible
class Foo::Bar
end

The compact style is only forced for classes/modules with one child.

Constants

COMPACT_MSG
NESTED_MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 43
def autocorrect(node)
  lambda do |corrector|
    return if node.class_type? && node.parent_class && style != :nested

    nest_or_compact(corrector, node)
  end
end
on_class(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 33
def on_class(node)
  return if node.parent_class && style != :nested

  check_style(node, node.body)
end
on_module(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 39
def on_module(node)
  check_style(node, node.body)
end

Private Instance Methods

add_trailing_end(corrector, node, padding) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 83
def add_trailing_end(corrector, node, padding)
  replacement = "#{padding}end\n#{leading_spaces(node)}end"
  corrector.replace(node.loc.end, replacement)
end
check_compact_style(node, body) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 135
def check_compact_style(node, body)
  return unless one_child?(body) && !compact_node_name?(node)

  add_offense(node, location: :name, message: COMPACT_MSG)
end
check_nested_style(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 129
def check_nested_style(node)
  return unless compact_node_name?(node)

  add_offense(node, location: :name, message: NESTED_MSG)
end
check_style(node, body) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 121
def check_style(node, body)
  if style == :nested
    check_nested_style(node)
  else
    check_compact_style(node, body)
  end
end
compact_definition(corrector, node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 88
def compact_definition(corrector, node)
  compact_node(corrector, node)
  remove_end(corrector, node.body)
end
compact_identifier_name(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 100
def compact_identifier_name(node)
  "#{node.identifier.const_name}::" \
    "#{node.body.children.first.const_name}"
end
compact_node(corrector, node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 93
def compact_node(corrector, node)
  replacement = "#{node.body.type} #{compact_identifier_name(node)}"
  range = range_between(node.loc.keyword.begin_pos,
                        node.body.loc.name.end_pos)
  corrector.replace(range, replacement)
end
compact_node_name?(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 145
def compact_node_name?(node)
  node.loc.name.source =~ /::/
end
indent_width() click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 117
def indent_width
  @config.for_cop('IndentationWidth')['Width'] || 2
end
leading_spaces(node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 113
def leading_spaces(node)
  node.source_range.source_line[/\A\s*/]
end
nest_definition(corrector, node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 61
def nest_definition(corrector, node)
  padding = ((' ' * indent_width) + leading_spaces(node)).to_s
  padding_for_trailing_end = padding.sub(' ' * node.loc.end.column, '')

  replace_keyword_with_module(corrector, node)
  split_on_double_colon(corrector, node, padding)
  add_trailing_end(corrector, node, padding_for_trailing_end)
end
nest_or_compact(corrector, node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 53
def nest_or_compact(corrector, node)
  if style == :nested
    nest_definition(corrector, node)
  else
    compact_definition(corrector, node)
  end
end
one_child?(body) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 141
def one_child?(body)
  body && %i[module class].include?(body.type)
end
remove_end(corrector, body) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 105
def remove_end(corrector, body)
  range = range_between(
    body.loc.end.begin_pos - leading_spaces(body).size,
    body.loc.end.end_pos + 1
  )
  corrector.remove(range)
end
replace_keyword_with_module(corrector, node) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 70
def replace_keyword_with_module(corrector, node)
  corrector.replace(node.loc.keyword, 'module')
end
split_on_double_colon(corrector, node, padding) click to toggle source
# File lib/rubocop/cop/style/class_and_module_children.rb, line 74
def split_on_double_colon(corrector, node, padding)
  children_definition = node.children.first
  range = range_between(children_definition.loc.double_colon.begin_pos,
                        children_definition.loc.double_colon.end_pos)
  replacement = "\n#{padding}#{node.loc.keyword.source} "

  corrector.replace(range, replacement)
end