Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Style/BracesAroundHashParameters
Browse files Browse the repository at this point in the history
Fixes rubocop/rubocop-jp#45. (This link is Japanese issue)

This PR fixes an incorrect autocorrect for `Style/BracesAroundHashParameters`
when the opening brace is before the first hash element.

```console
% rubocop -V
0.60.0 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-darwin17)

% cat example.rb
# frozen_string_literal: true

foo = Foo.new(
  { foo: 'foo',
    bar: 'bar',
    baz: 'this is the last element'}
)
```

The following difference is the execution result of `rubocop -a` command.

## Before

The last element is lost.

```diff
diff --git a/example.rb b/example.rb
index ecf9711..c9a02b8 100644
--- a/example.rb
+++ b/example.rb
@@ -1,7 +1,6 @@
 # frozen_string_literal: true

 foo = Foo.new(
-  { foo: 'foo',
-    bar: 'bar',
-    baz: 'this is the last element'}
+  foo: 'foo',
+  bar: 'bar'
 )
```

## After

Remove only the braces.

```diff
diff --git a/example.rb b/example.rb
index ecf9711..57790da 100644
--- a/example.rb
+++ b/example.rb
@@ -1,7 +1,7 @@
 # frozen_string_literal: true

 foo = Foo.new(
-  { foo: 'foo',
-    bar: 'bar',
-    baz: 'this is the last element'}
+  foo: 'foo',
+  bar: 'bar',
+  baz: 'this is the last element'
 )
```
  • Loading branch information
koic committed Nov 5, 2018
1 parent c5a8470 commit df46f61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
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',
bar: 'bar',
baz: 'this is the last element'
)
RUBY
end
end
end

Expand Down

0 comments on commit df46f61

Please sign in to comment.