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 #7957] Fix Style/IfUnlessModifier to remain multiline if code after end keyword makes it too long #8449

Merged
merged 1 commit into from Aug 4, 2020
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.md
Expand Up @@ -37,6 +37,7 @@
* [#8424](https://github.com/rubocop-hq/rubocop/issues/8424): Fix an error for `Lint/IneffectiveAccessModifier` when there is `begin...end` before a method definition. ([@koic][])
* [#8006](https://github.com/rubocop-hq/rubocop/issues/8006): Fix line length calculation for `Style/IfUnlessModifier` to correctly take into account code before the if condition when considering conversation to a single-line form. ([@dsavochkin][])
* [#8283](https://github.com/rubocop-hq/rubocop/issues/8283): Fix line length calculation for `Style/IfUnlessModifier` to correctly take into account a comment on the first line when considering conversation to a single-line form. ([@dsavochkin][])
* [#7957](https://github.com/rubocop-hq/rubocop/issues/7957): Fix line length calculation for `Style/IfUnlessModifier` to correctly take into account code on the last line after the end keyword when considering conversion to a single-line form. ([@dsavochkin][])
* [#8226](https://github.com/rubocop-hq/rubocop/issues/8226): Fix `Style/IfUnlessModifier` to add parentheses when converting if-end condition inside an array or a hash to a single-line form. ([@dsavochkin][])
* [#8443](https://github.com/rubocop-hq/rubocop/pull/8443): Fix an incorrect auto-correct for `Style/StructInheritance` when there is a comment before class declaration. ([@koic][])
* [#8444](https://github.com/rubocop-hq/rubocop/issues/8444): Fix an error for `Layout/FirstMethodArgumentLineBreak` when using kwargs in `super`. ([@koic][])
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/mixin/statement_modifier.rb
Expand Up @@ -40,10 +40,12 @@ def modifier_fits_on_single_line?(node)
end

def length_in_modifier_form(node)
keyword = node.loc.keyword
prefix = keyword.source_line[0...keyword.column]
keyword_element = node.loc.keyword
end_element = node.loc.end
code_before = keyword_element.source_line[0...keyword_element.column]
code_after = end_element.source_line[end_element.last_column..-1]
expression = to_modifier_form(node)
line_length("#{prefix}#{expression}")
line_length("#{code_before}#{expression}#{code_after}")
end

def to_modifier_form(node)
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/style/if_unless_modifier_spec.rb
Expand Up @@ -740,4 +740,33 @@ def f
end
end
end

context 'when if-end condition has some code after the end keyword' do
let(:source) do
<<~RUBY
[
1, if foo
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really hope that no one actually writes such code. 😆

#{'b' * body_length}
end, 300_000_000
]
RUBY
end

context 'when it is short enough to fit on a single line' do
let(:body_length) { 53 }

it 'corrects it to the single-line form' do
corrected = autocorrect_source(source)
expect(corrected).to eq "[\n 1, (#{'b' * body_length} if foo), 300_000_000\n]\n"
end
end

context 'when it is not short enough to fit on a single line' do
let(:body_length) { 54 }

it 'accepts it in the multiline form' do
expect_no_offenses(source)
end
end
end
end