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

Autocorrect for Layout/FirstHashElementIndentation breaks indentation consistency #10689

Closed
j-miyake opened this issue Jun 3, 2022 · 1 comment · Fixed by #10707
Closed

Comments

@j-miyake
Copy link
Contributor

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

The autocorrect for Layout/FirstHashElementIndentation breaks indentation consistency when inspecting this code.

(Code borrowed from grape's README)

desc 'Returns your public timeline.' do
  headers XAuthToken: {
            description: 'Validates your identity',
            required: true
          },
          XOptionalHeader: {
            description: 'Not really needed',
            required: false
          }
end

Expected behavior

No offenses, or correct the code with keeping indentation consistency for the second hash element.

Actual behavior

C: [Correctable] Layout/FirstHashElementIndentation: Indent the right brace the same as the start of the line where the left brace is.
              },
              ^

The code is autocorrected like,

 desc 'Returns your public timeline.' do
   headers XAuthToken: {
-            description: 'Validates your identity',
-            required: true
-          },
+    description: 'Validates your identity',
+    required: true
+  },
           XOptionalHeader: {
             description: 'Not really needed',
             required: false
           }

Steps to reproduce the problem

Run rubocop against the above code with the default configuration.

RuboCop version

$ exe/rubocop -V
1.30.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 2.7.5 x86_64-darwin20)
  - rubocop-performance 1.14.0
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.11.1
@j-miyake
Copy link
Contributor Author

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

The error message says "Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.". Allowing to align the inner hash content to the key of the outer hash instead of the start of the line may be one of the possible solutions.

C: [Correctable] Layout/FirstHashElementIndentation: Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is.
            description: 'Validates your identity',
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The second hash key XOptionalHeader probably should keep its original position as it has to align to the first key XAuthToken following Layout/HashAlignment cop.

j-miyake added a commit to j-miyake/rubocop that referenced this issue Jun 9, 2022
j-miyake added a commit to j-miyake/rubocop that referenced this issue Jun 15, 2022
j-miyake added a commit to j-miyake/rubocop that referenced this issue Jun 18, 2022
…tation` and `Layout/FirstArrayElementIndentation`

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 hash (or an array in hash) based on the parent hash key when the parent hash is a method argument.

Example code:
```ruby
func x: {
  a: 1,
       b: 2
},
     y: {
  c: 1,
       d: 2
}
```

In the original behavior, the code is corrected like
```ruby
func x: {
  a: 1,
  b: 2
},
     y: {
       c: 1,
       d: 2
     }

```

In the new behavior, the code is corrected like
```ruby
func x: {
       a: 1,
       b: 2
     },
     y: {
       c: 1,
       d: 2
     }
```
j-miyake added a commit to j-miyake/rubocop that referenced this issue Jun 18, 2022
…tation` and `Layout/FirstArrayElementIndentation`

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 hash (or an array in hash) based on the parent hash key when the parent hash is a method argument.

Example code:
```ruby
func x: {
  a: 1,
       b: 2
},
     y: {
  c: 1,
       d: 2
}
```

In the original behavior, the code is corrected like
```ruby
func x: {
  a: 1,
  b: 2
},
     y: {
       c: 1,
       d: 2
     }

```

In the new behavior, the code is corrected like
```ruby
func x: {
       a: 1,
       b: 2
     },
     y: {
       c: 1,
       d: 2
     }
```
j-miyake added a commit to j-miyake/rubocop that referenced this issue Jun 19, 2022
…tation` and `Layout/FirstArrayElementIndentation`

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 added a commit to j-miyake/rubocop that referenced this issue Jun 19, 2022
…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 added a commit to j-miyake/rubocop that referenced this issue Jun 20, 2022
…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 added a commit to j-miyake/rubocop that referenced this issue Jun 20, 2022
…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
}
```
bbatsov pushed a commit that referenced this issue Jun 20, 2022
… and `Layout/FirstArrayElementIndentation`

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:
```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 added a commit to j-miyake/rubocop that referenced this issue Jun 28, 2022
…ation

Fixes rubocop#10751

`Layout/FirstHashElementIndentation` should allow a hash where the content of the first pair is indented relative to the base column specified with configured cop style, and the second pair begins on the same line with the right brace of the first pair; because those two keys won't be aligned with each other by `Layout/HashAlignment`, not like the case of rubocop#10689.

With no parenthesis
```ruby
post :create, params: {
  foo: { bar: 'Baz' }
}, as: :json
```

With the parenthesis and `special_inside_parentheses` style
```ruby
post(:create, params: {
       foo: { bar: 'Baz' }
     }, as: :json)
```

With the parenthesis and `consistent` style
```ruby
post(:create, params: {
  foo: { bar: 'Baz' }
}, as: :json)
```
bbatsov pushed a commit that referenced this issue Jun 29, 2022
Fixes #10751

`Layout/FirstHashElementIndentation` should allow a hash where the content of the first pair is indented relative to the base column specified with configured cop style, and the second pair begins on the same line with the right brace of the first pair; because those two keys won't be aligned with each other by `Layout/HashAlignment`, not like the case of #10689.

With no parenthesis
```ruby
post :create, params: {
  foo: { bar: 'Baz' }
}, as: :json
```

With the parenthesis and `special_inside_parentheses` style
```ruby
post(:create, params: {
       foo: { bar: 'Baz' }
     }, as: :json)
```

With the parenthesis and `consistent` style
```ruby
post(:create, params: {
  foo: { bar: 'Baz' }
}, as: :json)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant