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 #9729] Fix an error for Style/IfUnlessModifier #9730

Merged
merged 1 commit into from Apr 22, 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_an_error_for_style_if_unless_modifier.md
@@ -0,0 +1 @@
* [#9729](https://github.com/rubocop/rubocop/issues/9729): Fix an error for `Style/IfUnlessModifier` when variable assignment is used in the branch body of if modifier. ([@koic][])
7 changes: 3 additions & 4 deletions lib/rubocop/cop/style/if_unless_modifier.rb
Expand Up @@ -67,15 +67,14 @@ def on_if(node)

def autocorrect(corrector, node)
replacement = if node.modifier_form?
indentation = ' ' * node.source_range.column
last_argument = node.if_branch.last_argument
last_argument = node.if_branch.last_argument if node.if_branch.send_type?

if last_argument.respond_to?(:heredoc?) && last_argument.heredoc?
heredoc = extract_heredoc_from(last_argument)
remove_heredoc(corrector, heredoc)
to_normal_form_with_heredoc(node, indentation, heredoc)
to_normal_form_with_heredoc(node, indent(node), heredoc)
else
to_normal_form(node, indentation)
to_normal_form(node, indent(node))
end
else
to_modifier_form(node)
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/if_unless_modifier_spec.rb
Expand Up @@ -92,6 +92,21 @@ def f
end
end

context 'when variable assignment is used in the branch body of if modifier' do
it 'registers an offense' do
expect_offense(<<~RUBY)
variable = foooooooooooooooooooooooooooooooooooooooooooooooooooooooo if condition
^^ Modifier form of `if` makes the line too long.
RUBY

expect_correction(<<~RUBY)
if condition
variable = foooooooooooooooooooooooooooooooooooooooooooooooooooooooo
end
RUBY
end
end

describe 'IgnoreCopDirectives' do
let(:spaces) { ' ' * 57 }
let(:comment) { '# rubocop:disable Style/For' }
Expand Down