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 #10130] Update Lint/ElseLayout to be able to handle an else with only a single line #10131

Merged
merged 1 commit into from Sep 28, 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/change_update_lintelselayout_to_be_able_to.md
@@ -0,0 +1 @@
* [#10130](https://github.com/rubocop/rubocop/issues/10130): Update `Lint/ElseLayout` to be able to handle an `else` with only a single line. ([@dvandersluis][])
14 changes: 9 additions & 5 deletions lib/rubocop/cop/lint/else_layout.rb
Expand Up @@ -49,6 +49,9 @@ class ElseLayout < Base
def on_if(node)
return if node.ternary?

# If the if is on a single line, it'll be handled by `Style/OneLineConditional`
return if node.single_line?

check(node)
end

Expand All @@ -66,10 +69,7 @@ def check(node)

def check_else(node)
else_branch = node.else_branch

return unless else_branch.begin_type?

first_else = else_branch.children.first
first_else = else_branch.begin_type? ? else_branch.children.first : else_branch

return unless first_else
return unless first_else.source_range.line == node.loc.else.line
Expand All @@ -81,9 +81,13 @@ def autocorrect(corrector, node, first_else)
corrector.insert_after(node.loc.else, "\n")

blank_range = range_between(node.loc.else.end_pos, first_else.loc.expression.begin_pos)
indentation = indent(node.else_branch.children[1])
indentation = indent(node, offset: indentation_width)
corrector.replace(blank_range, indentation)
end

def indentation_width
@config.for_cop('Layout/IndentationWidth')['Width'] || 2
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/util.rb
Expand Up @@ -129,8 +129,8 @@ def same_line?(node1, node2)
node1.respond_to?(:loc) && node2.respond_to?(:loc) && node1.loc.line == node2.loc.line
end

def indent(node)
' ' * node.loc.column
def indent(node, offset: 0)
' ' * (node.loc.column + offset)
end

def to_supported_styles(enforced_style)
Expand Down
36 changes: 30 additions & 6 deletions spec/rubocop/cop/lint/else_layout_spec.rb
Expand Up @@ -23,22 +23,31 @@
RUBY
end

it 'accepts proper else' do
expect_no_offenses(<<~RUBY)
it 'registers an offense and corrects for the entire else body being on the same line' do
expect_offense(<<~RUBY)
if something
test
else
something
else something_else
^^^^^^^^^^^^^^ Odd `else` layout detected. Did you mean to use `elsif`?
end
RUBY

expect_correction(<<~RUBY)
if something
test
else
something_else
end
RUBY
end

it 'accepts single-expr else regardless of layout' do
it 'accepts proper else' do
expect_no_offenses(<<~RUBY)
if something
test
else bala
else
something
test
end
RUBY
end
Expand Down Expand Up @@ -116,4 +125,19 @@
end
RUBY
end

it 'does not register an offense for an elsif with no body' do
expect_no_offenses(<<~RUBY)
if something
foo
elsif something_else
end
RUBY
end

it 'does not register an offense if the entire if is on a single line' do
expect_no_offenses(<<~RUBY)
if a then b else c end
RUBY
end
end