Skip to content

Commit

Permalink
[Fix rubocop#7534] Fix an incorrect autocorrect for `Layout/SpaceBefo…
Browse files Browse the repository at this point in the history
…reBlockBraces`

Fixes rubocop#7534.

Fix an incorrect autocorrect for `Style/BlockDelimiters` cop and
`Layout/SpaceBeforeBlockBraces` cop with `EnforcedStyle: no_space`
when using multiline braces.
  • Loading branch information
koic committed Nov 29, 2019
1 parent b3c056f commit e61357e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7530](https://github.com/rubocop-hq/rubocop/issues/7530): Typo in `Style/TrivialAccessors`'s `AllowedMethods`. ([@movermeyer][])
* [#7532](https://github.com/rubocop-hq/rubocop/issues/7532): Fix an error for `Style/TrailingCommaInArguments` when using an anonymous function with multiple line arguments with `EnforcedStyleForMultiline: consistent_comma`. ([@koic][])
* [#7534](https://github.com/rubocop-hq/rubocop/issues/7534): Fix an incorrect autocorrect for `Style/BlockDelimiters` cop and `Layout/SpaceBeforeBlockBraces` cop with `EnforcedStyle: no_space` when using multiline braces. ([@koic][])

## 0.77.0 (2019-11-27)

Expand Down
17 changes: 17 additions & 0 deletions lib/rubocop/cop/layout/space_before_block_braces.rb
Expand Up @@ -41,6 +41,14 @@ def self.autocorrect_incompatible_with
def on_block(node)
return if node.keywords?

# Do not register an offense for multi-line braces when specifying
# `EnforcedStyle: no_space`. It will conflict with auto-correction
# by `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop.
# That means preventing auto-correction to incorrect auto-corrected
# code.
# See: https://github.com/rubocop-hq/rubocop/issues/7534
return if conflict_with_block_delimiters?

left_brace = node.loc.begin
space_plus_brace = range_with_surrounding_space(range: left_brace)
used_style =
Expand Down Expand Up @@ -110,6 +118,15 @@ def style_for_empty_braces
end
end

def conflict_with_block_delimiters?
block_delimiters_style == 'line_count_based' &&
style == :no_space && node.multiline?
end

def block_delimiters_style
config.for_cop('Style/BlockDelimiters')['EnforcedStyle']
end

def empty_braces?(loc)
loc.begin.end_pos == loc.end.begin_pos
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Expand Up @@ -1653,6 +1653,32 @@ def self.some_method(foo, bar: 1)
RUBY
end

it 'corrects Style/BlockDelimiters offenses when specifing' \
'Layout/SpaceBeforeBlockBraces with `EnforcedStyle: no_space` together' do
create_file('example.rb', <<~RUBY)
foo {
bar
}
RUBY

create_file('.rubocop.yml', <<~YAML)
Layout/SpaceBeforeBlockBraces:
EnforcedStyle: no_space
YAML

expect(cli.run([
'--auto-correct',
'--only',
'Style/BlockDelimiters,Layout/SpaceBeforeBlockBraces'
])).to eq(0)
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(<<~RUBY)
foo do
bar
end
RUBY
end

it 'corrects BracesAroundHashParameters offenses leaving the ' \
'MultilineHashBraceLayout offense unchanged' do
create_file('example.rb', <<~RUBY)
Expand Down

0 comments on commit e61357e

Please sign in to comment.