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

Conversation

koic
Copy link
Member

@koic koic commented Nov 5, 2018

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.

% 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 --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 --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'
 )

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default. It executes all tests and RuboCop for itself, and generates the documentation.

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'
 )
```
@koic koic force-pushed the fix_incorrect_autocorrect_for_braces_around_hash_parameters branch from 83d64fa to df46f61 Compare November 5, 2018 09:09

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.

@bbatsov bbatsov merged commit ccbd71a into rubocop:master Nov 5, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 5, 2018

👍

@koic koic deleted the fix_incorrect_autocorrect_for_braces_around_hash_parameters branch November 5, 2018 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

auto-correctすると要素が消える
2 participants