Skip to content

Commit

Permalink
[Fix rubocop#3374] Don't check BlockDelimiters in SpaceInsideBlockBra…
Browse files Browse the repository at this point in the history
…ces (rubocop#3446)

Same in SpaceBeforeBlockBraces. The reason we needed this special
handling seems to be gone now. Auto-correct has become smarter.
  • Loading branch information
jonas054 authored and Neodelf committed Oct 15, 2016
1 parent b1defb1 commit 675439e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [#3386](https://github.com/bbatsov/rubocop/issues/3386): Make `VariableForce` understand an empty RegExp literal as LHS to `=~`. ([@drenmi][])
* [#3421](https://github.com/bbatsov/rubocop/pull/3421): Fix clobbering `inherit_from` additions when not using Namespaces in the configs. ([@nicklamuro][])
* [#3425](https://github.com/bbatsov/rubocop/pull/3425): Fix bug for invalid bytes in UTF-8 in `Lint/PercentStringArray` cop. ([@pocke][])
* [#3374](https://github.com/bbatsov/rubocop/issues/3374): Make `SpaceInsideBlockBraces` and `SpaceBeforeBlockBraces` not depend on `BlockDelimiters` configuration. ([@jonas054][])

### Changes

Expand Down
9 changes: 0 additions & 9 deletions lib/rubocop/cop/style/space_before_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ class SpaceBeforeBlockBraces < Cop
def on_block(node)
return if node.loc.begin.is?('do') # No braces.

# If braces are on separate lines, and the BlockDelimiters cop is
# enabled, those braces will be changed to do..end by the user or by
# auto-correct, so reporting space issues is not useful, and it
# creates auto-correct conflicts.
if config.for_cop('Style/BlockDelimiters')['Enabled'] &&
block_length(node) > 0
return
end

left_brace = node.loc.begin
space_plus_brace = range_with_surrounding_space(left_brace)
used_style =
Expand Down
9 changes: 0 additions & 9 deletions lib/rubocop/cop/style/space_inside_block_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ class SpaceInsideBlockBraces < Cop
def on_block(node)
return if node.loc.begin.is?('do') # No braces.

# If braces are on separate lines, and the BlockDelimiters cop is
# enabled, those braces will be changed to do..end by the user or by
# auto-correct, so reporting space issues is not useful, and it
# creates auto-correct conflicts.
if config.for_cop('Style/BlockDelimiters')['Enabled'] &&
block_length(node) > 0
return
end

left_brace = node.loc.begin
right_brace = node.loc.end

Expand Down
45 changes: 45 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,51 @@ def abs(path)
expect(IO.read('example.rb')).to eq(corrected.join("\n"))
end

[:line_count_based, :semantic, :braces_for_chaining].each do |style|
context "when BlockDelimiters has #{style} style" do
it 'corrects SpaceBeforeBlockBraces, SpaceInsideBlockBraces offenses' do
source = ['r = foo.map{|a|',
' a.bar.to_s',
'}',
'foo.map{|a|',
' a.bar.to_s',
'}.baz']
create_file('example.rb', source)
create_file('.rubocop.yml', ['Style/BlockDelimiters:',
" EnforcedStyle: #{style}"])
expect(cli.run(['--auto-correct'])).to eq(1)
corrected = case style
when :semantic
['r = foo.map { |a|',
' a.bar.to_s',
'}',
'foo.map { |a|',
' a.bar.to_s',
'}.baz',
'']
when :braces_for_chaining
['r = foo.map do |a|',
' a.bar.to_s',
'end',
'foo.map { |a|',
' a.bar.to_s',
'}.baz',
'']
when :line_count_based
['r = foo.map do |a|',
' a.bar.to_s',
'end',
'foo.map do |a|',
' a.bar.to_s',
'end.baz',
'']
end
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(corrected.join("\n"))
end
end
end

it 'corrects InitialIndentation offenses' do
source = [' # comment 1',
'',
Expand Down
19 changes: 11 additions & 8 deletions spec/rubocop/cop/style/space_before_block_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@

require 'spec_helper'

describe RuboCop::Cop::Style::SpaceBeforeBlockBraces do
describe RuboCop::Cop::Style::SpaceBeforeBlockBraces, :config do
subject(:cop) { described_class.new(config) }
let(:config) do
merged = RuboCop::ConfigLoader
.default_configuration['Style/SpaceBeforeBlockBraces']
.merge(cop_config)
RuboCop::Config.new('Style/BlockDelimiters' => { 'Enabled' => false },
'Style/SpaceBeforeBlockBraces' => merged)
end
let(:cop_config) { { 'EnforcedStyle' => 'space' } }

context 'when EnforcedStyle is space' do
Expand All @@ -36,6 +29,16 @@
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for multiline block where left brace has no ' \
'outer space' do
inspect_source(cop, ['foo.map{ |a|',
' a.bar.to_s',
'}'])
expect(cop.messages).to eq(['Space missing to the left of {.'])
expect(cop.highlights).to eq(['{'])
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'no_space')
end

it 'auto-corrects missing space' do
new_source = autocorrect_source(cop, 'each{}')
expect(new_source).to eq('each {}')
Expand Down
17 changes: 5 additions & 12 deletions spec/rubocop/cop/style/space_inside_block_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@

require 'spec_helper'

describe RuboCop::Cop::Style::SpaceInsideBlockBraces do
describe RuboCop::Cop::Style::SpaceInsideBlockBraces, :config do
SUPPORTED_STYLES = %w(space no_space).freeze

subject(:cop) { described_class.new(config) }
let(:config) do
merged = RuboCop::ConfigLoader
.default_configuration['Style/SpaceInsideBlockBraces']
.merge(cop_config)
RuboCop::Config.new('Style/BlockDelimiters' => { 'Enabled' => false },
'Style/SpaceInsideBlockBraces' => merged)
end
let(:cop_config) do
{
'EnforcedStyle' => 'space',
Expand Down Expand Up @@ -210,14 +203,14 @@
expect(new_source).to eq('each { |x| puts }')
end

it 'does not do auto-correction for multi-line blocks' do
# {} will be changed to do..end by the BlockDelimiters cop, and then
# this cop is not relevant anymore.
it 'does auto-correction for multi-line blocks' do
old_source = ['each {|x|',
' puts',
'}']
new_source = autocorrect_source(cop, old_source)
expect(new_source).to eq(old_source.join("\n"))
expect(new_source).to eq(['each { |x|',
' puts',
'}'].join("\n"))
end
end

Expand Down

0 comments on commit 675439e

Please sign in to comment.