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 an incorrect autocorrect for Style/BracesAroundHashParameters #6443

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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#5970](https://github.com/rubocop-hq/rubocop/issues/5970): Make running `--auto-gen-config` in a subdirectory work. ([@jonas054][])
* [#6412](https://github.com/rubocop-hq/rubocop/issues/6412): Fix an `unknown keywords` error when using `Psych.safe_load` with Ruby 2.6.0-preview2. ([@koic][])
* [#6436](https://github.com/rubocop-hq/rubocop/pull/6436): Fix exit status code to be 130 when rubocop is interrupted. ([@deivid-rodriguez][])
* [#6443](https://github.com/rubocop-hq/rubocop/pull/6443): Fix an incorrect autocorrect for `Style/BracesAroundHashParameters` when the opening brace is bofore the first hash element at same line. ([@koic][])

## 0.60.0 (2018-10-26)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/braces_around_hash_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def left_whole_line_range(loc_begin)
end

def right_whole_line_range(loc_end)
if range_by_whole_lines(loc_end).source.strip =~ /}\s*,?\z/
if range_by_whole_lines(loc_end).source.strip =~ /\A}\s*,?\z/
range_by_whole_lines(loc_end, include_final_newline: true)
else
loc_end
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,25 @@
)
RUBY
end

it 'corrects when the opening brace is before the first hash element' \
'at same line' do
corrected = autocorrect_source(<<-RUBY.strip_indent)
foo = Foo.new(
{ foo: 'foo',
bar: 'bar',
baz: 'this is the last element'}
)
RUBY

expect(corrected).to eq(<<-RUBY.strip_indent)
foo = Foo.new(
foo: 'foo',
Copy link
Member Author

Choose a reason for hiding this comment

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

To correct this indentation gap, the opening brace must be replaced with space instead of being removed.

First of all, this PR solves the problem that last element is lost by auto-correct. I'm considering solving this indentation gap problem with another PR.

Until then this indetation gap will be aligned with Layout/AlignHash cop.

bar: 'bar',
baz: 'this is the last element'
)
RUBY
end
end
end

Expand Down