Skip to content

Commit

Permalink
Fix indentation in Style/ClassAndModuleChildren
Browse files Browse the repository at this point in the history
The Style/ClassAndModuleChildren style: compact had a bug that left the
indentation intact, instead of dedenting it by the correct number of spaces
after it autocorrected.
  • Loading branch information
markburns committed Jun 22, 2021
1 parent eba9ab8 commit a9a4350
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_indentation_in_class_and_module_children.md
@@ -0,0 +1 @@
* [#9886](https://github.com/rubocop/rubocop/pull/9886): Fix indentation in Style/ClassAndModuleChildren. ([@markburns][])
14 changes: 14 additions & 0 deletions lib/rubocop/cop/style/class_and_module_children.rb
Expand Up @@ -84,6 +84,7 @@ def add_trailing_end(corrector, node, padding)
def compact_definition(corrector, node)
compact_node(corrector, node)
remove_end(corrector, node.body)
unindent(corrector, node)
end

def compact_node(corrector, node)
Expand Down Expand Up @@ -114,6 +115,19 @@ def remove_end(corrector, body)
corrector.remove(range)
end

def configured_indentation_width
config.for_badge(Layout::IndentationWidth.badge).fetch('Width', 2)
end

def unindent(corrector, node)
return if node.body.children.last.nil?

column_delta = configured_indentation_width - leading_spaces(node.body.children.last).size
return if column_delta.zero?

AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
end

def leading_spaces(node)
node.source_range.source_line[/\A\s*/]
end
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/style/class_and_module_children_spec.rb
Expand Up @@ -210,12 +210,43 @@ class FooClass::BarClass
module FooModule
^^^^^^^^^ Use compact module/class definition instead of nested style.
module BarModule
def method_example
end
end
end
RUBY

expect_correction(<<~RUBY)
module FooModule::BarModule
def method_example
end
end
RUBY
end

it 'correctly indents heavily nested children' do
expect_offense(<<~RUBY)
module FooModule
^^^^^^^^^ Use compact module/class definition instead of nested style.
module BarModule
module BazModule
module QuxModule
CONST = 1
def method_example
end
end
end
end
end
RUBY

expect_correction(<<~RUBY)
module FooModule::BarModule::BazModule::QuxModule
CONST = 1
def method_example
end
end
RUBY
end
Expand Down

0 comments on commit a9a4350

Please sign in to comment.