Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dedenting Style/ClassAndModuleChildren #9886

Merged
merged 1 commit into from Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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