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 #10689] Fix autocorrect for Layout/FirstHashElementIndentation and Layout/FirstArrayElementIndentation #10707

Merged
merged 1 commit into from Jun 20, 2022

Conversation

j-miyake
Copy link
Contributor

@j-miyake j-miyake commented Jun 9, 2022

Fixes #10689

This PR fixes the autocorrect for Layout/FirstHashElementIndentation and Layout/FirstArrayElementIndentation each combined with Layout/HashAlignment.

The new behavior indents the elements of a hash in a hash (or an array in a hash) based on the parent hash key when the parent hash is a method argument and has a following other sibling pair.

Example code to be corrected:

func x: {
  a: 1,
       b: 2
},
     y: { # following sibling pair in the parent hash
  c: 1,
       d: 2
}

func x: [
  :a,
       :b
],
     y: [ # following sibling pair in the parent hash
  :c,
       :d
]

In the original behavior, the code is corrected like

func x: {
  a: 1,
  b: 2
},
     y: { # following sibling pair of the parent hash
       c: 1,
       d: 2
     }

func x: [
  :a,
  :b
],
     y: [ # following sibling pair in the parent hash
       :c,
       :d
     ]

With this fix, the code is corrected like

func x: {
       a: 1,
       b: 2
     },
     y: { # following sibling pair in the parent hash
       c: 1,
       d: 2
     }

func x: [
       :a,
       :b
     ],
     y: [ # following sibling pair in the parent hash
       :c,
       :d
     ]

The autocorrect keeps the original behavior when the parent hash has no other following pairs. (I could change this so that the elements of the hash are always indented based on the parent hash key, but I kept the original behavior this time)

func x: {
  a: 1,
       b: 2
}

# corrected into
func x: {
  a: 1,
  b: 2
}

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@j-miyake j-miyake changed the title [Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation [WIP][Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation Jun 10, 2022
@j-miyake j-miyake force-pushed the fix_first_hash_element_indentation branch 7 times, most recently from fcd36b1 to 19862a4 Compare June 18, 2022 14:57
@j-miyake j-miyake force-pushed the fix_first_hash_element_indentation branch from 19862a4 to 7bb1e59 Compare June 19, 2022 02:54
@j-miyake j-miyake changed the title [WIP][Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation [WIP][Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation and Layout/FirstArrayElementIndentation Jun 19, 2022
@j-miyake j-miyake marked this pull request as ready for review June 19, 2022 03:27
@j-miyake j-miyake changed the title [WIP][Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation and Layout/FirstArrayElementIndentation [Fix #10689] Fix autocorrect for Layout/FirstHashElementIndentation and Layout/FirstArrayElementIndentation Jun 19, 2022
@j-miyake j-miyake force-pushed the fix_first_hash_element_indentation branch from 7bb1e59 to 8f1ff6e Compare June 19, 2022 03:29
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 20, 2022

The changes look good, but your branch has to be rebased on top of the current master branch due to merge conflicts.

@j-miyake j-miyake force-pushed the fix_first_hash_element_indentation branch from 8f1ff6e to ed9a1af Compare June 20, 2022 11:48
…tation` and `Layout/FirstArrayElementIndentation`

Fixes rubocop#10689

This PR fixes the autocorrect for `Layout/FirstHashElementIndentation` and `Layout/FirstArrayElementIndentation` each combined with `Layout/HashAlignment`.

The new behavior indents the elements of a hash in a hash (or an array in a hash) based on the parent hash key _when the parent hash is a method argument and has a following other sibling pair_.

Example code to be corrected:
```ruby
func x: {
  a: 1,
       b: 2
},
     y: { # following sibling pair in the parent hash
  c: 1,
       d: 2
}
```

In the original behavior, the code is corrected like
```ruby
func x: {
  a: 1,
  b: 2
},
     y: { # following sibling pair of the parent hash
       c: 1,
       d: 2
     }
```

With this fix, the code is corrected like
```ruby
func x: {
       a: 1,
       b: 2
     },
     y: { # following sibling pair in the parent hash
       c: 1,
       d: 2
     }
```

The autocorrect keeps the original behavior when the parent hash has no other following pairs. (I could change this so that the elements of the hash are always indented based on the parent hash key, but I kept the original behavior this time)

```ruby
func x: {
  a: 1,
       b: 2
}

func x: {
  a: 1,
  b: 2
}
```
@j-miyake j-miyake force-pushed the fix_first_hash_element_indentation branch from ed9a1af to 5b15cc5 Compare June 20, 2022 12:14
@j-miyake
Copy link
Contributor Author

I updated the PR. Thanks!

@bbatsov bbatsov merged commit 023e0ea into rubocop:master Jun 20, 2022
@j-miyake j-miyake deleted the fix_first_hash_element_indentation branch June 20, 2022 14:01
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.

Autocorrect for Layout/FirstHashElementIndentation breaks indentation consistency
2 participants