Skip to content

Commit

Permalink
[Fix #7590] Fix an error for Layout/SpaceBeforeBlockBraces
Browse files Browse the repository at this point in the history
Fixes #7590.

This PR fixes an error for `Layout/SpaceBeforeBlockBraces` when using with
`EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop.
  • Loading branch information
koic authored and bbatsov committed Dec 25, 2019
1 parent ed083fb commit c66efb9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

* [#7193](https://github.com/rubocop-hq/rubocop/issues/7193): Prevent `Style/PercentLiteralDelimiters` from changing `%i` literals that contain escaped delimiters. ([@buehmann][])
* [#7590](https://github.com/rubocop-hq/rubocop/issues/7590): Fix an error for `Layout/SpaceBeforeBlockBraces` when using with `EnforcedStyle: line_count_based` of `Style/BlockDelimiters` cop. ([@koic][])

## 0.78.0 (2019-12-18)

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/layout/space_before_block_braces.rb
Expand Up @@ -47,7 +47,7 @@ def on_block(node)
# 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?
return if conflict_with_block_delimiters?(node)

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

def conflict_with_block_delimiters?
def conflict_with_block_delimiters?(node)
block_delimiters_style == 'line_count_based' &&
style == :no_space && node.multiline?
end
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/layout/space_before_block_braces_spec.rb
Expand Up @@ -62,6 +62,25 @@
it 'accepts left brace without outer space' do
expect_no_offenses('each{ puts }')
end

context 'with `EnforcedStyle` of `Style/BlockDelimiters`' do
let(:config) do
merged_config = RuboCop::ConfigLoader.default_configuration[
'Layout/SpaceBeforeBlockBraces'
].merge(cop_config)

RuboCop::Config.new(
'Layout/SpaceBeforeBlockBraces' => merged_config,
'Style/BlockDelimiters' => { 'EnforcedStyle' => 'line_count_based' }
)
end

it 'accepts left brace without outer space' do
expect_no_offenses(<<~RUBY)
let(:foo){{foo: 1, bar: 2}}
RUBY
end
end
end

context 'with space before empty braces not allowed' do
Expand Down

0 comments on commit c66efb9

Please sign in to comment.