Skip to content

Commit

Permalink
Add test for hash indentation after line break
Browse files Browse the repository at this point in the history
```ruby
def example
  { key1:
    { key2:
      { key3:
	{ key4:
	  { key5:
	    :value
	  }
	}
      }
    }
  }
end
```

What seems to be happening is that a line break is inserted between `{`
and `keyN:`. `Layout/TrailingWhitespace` handles getting rid of the
space remaining after `{`, but everything goes wrong in
`Layout/FirstHashElementIndentation`.

```ruby
def example
  {
key1:
    {
key2:
      {
key3:
	{
key4:
	  {
key5:
	    :value
	  }
	}
      }
    }
  }
end
```

It finds `key1:` in column 0, computes a column delta of 4, which it
applies to the key and value lines, even though the value lines don't
have the same indentation.

```ruby
def example
  {
    key1:
        {
    key2:
          {
    key3:
    	{
    key4:
    	  {
    key5:
    	    :value
    	  }
    	}
          }
        }
  }
end
```

This repeats for `key2:`, which is not indented as much as it should be
relative to its opening `{`, so the entire thing is indented by the
column delta.

```ruby
def example
  {
    key1:
        {
          key2:
                {
          key3:
          	{
          key4:
          	  {
          key5:
          	    :value
          	  }
          	}
                }
        }
  }
end
```

This continues to repeat for `key3:`, `key4:`, and `key5:`,
respectively:

```ruby
def example
  {
    key1:
        {
          key2:
                {
                  key3:
                  	{
                  key4:
                  	  {
                  key5:
                  	    :value
                  	  }
                  	}
                }
        }
  }
end
```

```ruby
def example
  {
    key1:
        {
          key2:
                {
                  key3:
                  	{
                          key4:
                          	  {
                          key5:
                          	    :value
                          	  }
                  	}
                }
        }
  }
end
```

```ruby
def example
  {
    key1:
        {
          key2:
                {
                  key3:
                  	{
                          key4:
                          	  {
                                    key5:
                                    	    :value
                          	  }
                  	}
                }
        }
  }
end
```

Every level is incorrectly corrected once for each of it's parents, and
finally for itself, compounding the error.
  • Loading branch information
sambostock committed Jan 24, 2023
1 parent 99c8b17 commit f322746
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -2763,4 +2763,50 @@ module Foo#{trailing_whitespace}
end
RUBY
end

it 'indents hashes correctly when adding a line break' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
def example
{ key1:
{ key2:
{ key3:
{ key4:
{ key5:
:value
}
}
}
}
}
end
RUBY

status = cli.run(%W[--autocorrect-all --only #{%w[
Layout/FirstHashElementIndentation
Layout/FirstHashElementLineBreak
Layout/TrailingWhitespace
].join(',')}])
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
def example
{
key1:
{
key2:
{
key3:
{
key4:
{
key5:
:value
}
}
}
}
}
end
RUBY
end
end

0 comments on commit f322746

Please sign in to comment.