Skip to content

Commit

Permalink
[Fix rubocop#3183] Ensure Style/SpaceInsideBlockBraces works for mu…
Browse files Browse the repository at this point in the history
…lti-line blocks
  • Loading branch information
owst authored and Owen Stephens committed Jun 15, 2016
1 parent 2e6fb07 commit 0ef2306
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [#3217](https://github.com/bbatsov/rubocop/pull/3217): Fix output of ellipses for multi-line offense ranges in HTML formatter. ([@jonas054][])
* [#3207](https://github.com/bbatsov/rubocop/issues/3207): Accept modifier forms of `while`/`until` in `Style/InfiniteLoop`. ([@jonas054][])
* [#3202](https://github.com/bbatsov/rubocop/issues/3202): Fix `Style/EmptyElse` registering wrong offenses and thus making RuboCop crash. ([@deivid-rodriguez][])
* [#3183](https://github.com/bbatsov/rubocop/issues/3183): Ensure `Style/SpaceInsideBlockBraces` reports offenses for multi-line blocks. ([@owst][])
* [#3017](https://github.com/bbatsov/rubocop/issues/3017): Fix `Style/StringLiterals` to register offenses on non-ascii strings. ([@deivid-rodriguez][])
* [#3056](https://github.com/bbatsov/rubocop/issues/3056): Fix `Style/StringLiterals` to register offenses on non-ascii strings. ([@deivid-rodriguez][])

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/style/space_inside_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def check_inside(node, left_brace, right_brace)

if left_brace.end_pos == right_brace.begin_pos
adjacent_braces(sb, left_brace, right_brace)
elsif left_brace.line == right_brace.line
else
range = Parser::Source::Range.new(sb, left_brace.end_pos,
right_brace.begin_pos)
inner = range.source
Expand Down Expand Up @@ -70,7 +70,7 @@ def braces_with_contents_inside(node, inner, sb)
space_inside_left_brace(left_brace, args_delimiter, sb)
end

if inner =~ /\S$/
if inner =~ /\S$/ && block_length(node) == 0
no_space(sb, right_brace.begin_pos, right_brace.end_pos,
'Space missing inside }.')
else
Expand Down
64 changes: 50 additions & 14 deletions spec/rubocop/cop/style/space_inside_block_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@
expect(cop.messages).to be_empty
end

it 'accepts empty braces with line break inside' do
inspect_source(cop, [' each {',
it 'accepts empty braces with comment and line break inside' do
inspect_source(cop, [' each { # Comment',
' }'])
expect(cop.messages).to be_empty
end

it 'accepts empty braces with comment and line break inside' do
inspect_source(cop, [' each { # Comment',
it 'registers an offense for empty braces with line break inside' do
inspect_source(cop, [' each {',
' }'])
expect(cop.messages).to be_empty
expect(cop.messages).to eq(['Space inside empty braces detected.'])
expect(cop.highlights).to eq(["\n "])
end

it 'registers an offense for empty braces with space inside' do
Expand Down Expand Up @@ -140,17 +141,52 @@
end

context 'with passed in parameters' do
it 'accepts left brace with inner space' do
inspect_source(cop, 'each { |x| puts }')
expect(cop.messages).to be_empty
expect(cop.highlights).to be_empty
context 'for single-line blocks' do
it 'accepts left brace with inner space' do
inspect_source(cop, 'each { |x| puts }')
expect(cop.messages).to be_empty
expect(cop.highlights).to be_empty
end

it 'registers an offense for left brace without inner space' do
inspect_source(cop, 'each {|x| puts }')
expect(cop.messages).to eq(['Space between { and | missing.'])
expect(cop.highlights).to eq(['{|'])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end
end

it 'registers an offense for left brace without inner space' do
inspect_source(cop, 'each {|x| puts }')
expect(cop.messages).to eq(['Space between { and | missing.'])
expect(cop.highlights).to eq(['{|'])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
context 'for multi-line blocks' do
it 'accepts left brace with inner space' do
inspect_source(cop, ['each { |x|',
'puts',
'}'])
expect(cop.messages).to be_empty
expect(cop.highlights).to be_empty
end

it 'registers an offense for left brace without inner space' do
inspect_source(cop, ['each {|x|',
'puts',
'}'])
expect(cop.messages).to eq(['Space between { and | missing.'])
expect(cop.highlights).to eq(['{|'])
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(cop, <<-SOURCE)
each {|x|
puts
}
SOURCE

expect(new_source).to eq(<<-NEW_SOURCE)
each { |x|
puts
}
NEW_SOURCE
end
end

it 'accepts new lambda syntax' do
Expand Down

0 comments on commit 0ef2306

Please sign in to comment.