From 8b1a46cea5124cc35324cb481c49fc1924ac015c Mon Sep 17 00:00:00 2001 From: Jonas Arvidsson Date: Sat, 27 Aug 2016 20:45:19 +0200 Subject: [PATCH] [Fix #3374] Don't check BlockDelimiters in SpaceInsideBlockBraces Same in SpaceBeforeBlockBraces. The reason we needed this special handling seems to be gone now. Auto-correct has become smarter. --- CHANGELOG.md | 1 + .../cop/style/space_before_block_braces.rb | 9 ---- .../cop/style/space_inside_block_braces.rb | 9 ---- spec/rubocop/cli/cli_autocorrect_spec.rb | 45 +++++++++++++++++++ .../style/space_before_block_braces_spec.rb | 19 ++++---- .../style/space_inside_block_braces_spec.rb | 17 +++---- 6 files changed, 62 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ccebb7ab842..3c700f7457f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/space_before_block_braces.rb b/lib/rubocop/cop/style/space_before_block_braces.rb index 6b9518848c5b..c427d0779fd7 100644 --- a/lib/rubocop/cop/style/space_before_block_braces.rb +++ b/lib/rubocop/cop/style/space_before_block_braces.rb @@ -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 = diff --git a/lib/rubocop/cop/style/space_inside_block_braces.rb b/lib/rubocop/cop/style/space_inside_block_braces.rb index 7d0875ecf169..4fd31bf8899a 100644 --- a/lib/rubocop/cop/style/space_inside_block_braces.rb +++ b/lib/rubocop/cop/style/space_inside_block_braces.rb @@ -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 diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index eb30bd0b8465..9314a79e7dda 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -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', '', diff --git a/spec/rubocop/cop/style/space_before_block_braces_spec.rb b/spec/rubocop/cop/style/space_before_block_braces_spec.rb index a072f462c031..968c3a1a4b15 100644 --- a/spec/rubocop/cop/style/space_before_block_braces_spec.rb +++ b/spec/rubocop/cop/style/space_before_block_braces_spec.rb @@ -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 @@ -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 {}') diff --git a/spec/rubocop/cop/style/space_inside_block_braces_spec.rb b/spec/rubocop/cop/style/space_inside_block_braces_spec.rb index 6ea0bb0048f0..0072a3f814f8 100644 --- a/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +++ b/spec/rubocop/cop/style/space_inside_block_braces_spec.rb @@ -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', @@ -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