From f3e3a32037a65fb8819481dc07cdad5ef54656e3 Mon Sep 17 00:00:00 2001 From: Daniel Orner Date: Thu, 16 Jul 2020 12:38:55 -0400 Subject: [PATCH] #8347: RSpec and Rubocop fixes --- CHANGELOG.md | 1 + lib/rubocop/cop/style/hash_syntax.rb | 3 ++- lib/rubocop/rspec/cop_helper.rb | 11 ++++++-- lib/rubocop/rspec/expect_offense.rb | 6 +++-- lib/rubocop/rspec/shared_contexts.rb | 11 ++++---- spec/rubocop/cop/style/hash_syntax_spec.rb | 30 +++++++++++----------- 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7451acd341f..ee233070cc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4700,3 +4700,4 @@ [@CamilleDrapier]: https://github.com/CamilleDrapier [@shekhar-patil]: https://github.com/shekhar-patil [@knejad]: https://github.com/knejad +[@dorner]: https://github.com/dorner diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index e52d98b69b7..413d012bbe4 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -199,7 +199,8 @@ def argument_without_space?(node) def autocorrect_hash_rockets(corrector, pair_node) op = pair_node.loc.operator - corrector.replace(pair_node.key, ":#{pair_node.key.source}#{pair_node.inverse_delimiter(true)}") + new_key = ":#{pair_node.key.source}#{pair_node.inverse_delimiter(true)}" + corrector.replace(pair_node.key, new_key) corrector.remove(range_with_surrounding_space(range: op)) end diff --git a/lib/rubocop/rspec/cop_helper.rb b/lib/rubocop/rspec/cop_helper.rb index be92b632fc1..c793774db9d 100644 --- a/lib/rubocop/rspec/cop_helper.rb +++ b/lib/rubocop/rspec/cop_helper.rb @@ -50,10 +50,17 @@ def _investigate(cops, processed_source) cop_array = Array(cops) team = RuboCop::Cop::Team.new(cop_array, nil, raise_error: true) report = team.investigate(processed_source) - @last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source) - report.correctors[1..-1].each { |corr| @last_corrector.merge!(corr) if corr } + @last_corrector = _corrector(report, processed_source) report.offenses end + + def _corrector(report, processed_source) + corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source) + if report.correctors&.compact&.any? + report.correctors.compact[1..-1].each { |corr| corrector.merge!(corr) } + end + corrector + end end module RuboCop diff --git a/lib/rubocop/rspec/expect_offense.rb b/lib/rubocop/rspec/expect_offense.rb index 33b213f9d4c..986b7273cc9 100644 --- a/lib/rubocop/rspec/expect_offense.rb +++ b/lib/rubocop/rspec/expect_offense.rb @@ -130,7 +130,8 @@ def expect_offense(source, file = nil, severity: nil, **replacements) raise 'Error parsing example code' unless @processed_source.valid_syntax? - offenses = _investigate(run_first_cops + [cop], @processed_source) + first_cops = respond_to?(:run_first_cops) ? run_first_cops : [] + offenses = _investigate(first_cops + [cop], @processed_source) actual_annotations = expected_annotations.with_offense_annotations(offenses) @@ -158,7 +159,8 @@ def expect_correction(correction, loop: true) # Prepare for next loop @processed_source = parse_source(corrected_source, @processed_source.path) - _investigate(run_first_cops + [cop], @processed_source) + first_cops = respond_to?(:run_first_cops) ? run_first_cops : [] + _investigate(first_cops + [cop], @processed_source) end expect(new_source).to eq(correction) diff --git a/lib/rubocop/rspec/shared_contexts.rb b/lib/rubocop/rspec/shared_contexts.rb index 1f61bbc5551..ee18c8a8a80 100644 --- a/lib/rubocop/rspec/shared_contexts.rb +++ b/lib/rubocop/rspec/shared_contexts.rb @@ -96,11 +96,11 @@ def source_range(range, buffer: source_buffer) cop_class.cop_name => cur_cop_config }.merge!(other_cops) run_first.each do |run_first_cop| run_first_cop_config = RuboCop::ConfigLoader - .default_configuration.for_cop(run_first_cop) - .merge({ - 'Enabled' => true, # in case it is 'pending' - 'AutoCorrect' => true # in case defaults set it to false - }).merge(other_cops[run_first_cop.badge.to_s] || {}) + .default_configuration.for_cop(run_first_cop) + .merge({ + 'Enabled' => true, # in case it is 'pending' + 'AutoCorrect' => true # in case defaults set it to false + }).merge(other_cops[run_first_cop.badge.to_s] || {}) hash.merge!(run_first_cop.badge.to_s => run_first_cop_config) end @@ -120,7 +120,6 @@ def source_range(range, buffer: source_buffer) end end end - end RSpec.shared_context 'mock console output' do diff --git a/spec/rubocop/cop/style/hash_syntax_spec.rb b/spec/rubocop/cop/style/hash_syntax_spec.rb index 7b47ecef7db..3cb08afe130 100644 --- a/spec/rubocop/cop/style/hash_syntax_spec.rb +++ b/spec/rubocop/cop/style/hash_syntax_spec.rb @@ -343,23 +343,24 @@ context 'with Layout/HashAlignment' do let(:run_first) { [RuboCop::Cop::Layout::HashAlignment] } - it 'should not conflict' do + + it 'does not conflict' do expect_offense(<<~RUBY) -some_method(a: 'abc', b: 'abc', - ^^ Use hash rockets syntax. - ^^ Use hash rockets syntax. - c: 'abc', d: 'abc' - ^^ Use hash rockets syntax. - ^^ Use hash rockets syntax. - ^^^^^^^^ Align the keys of a hash literal if they span more than one line. - ) - RUBY + some_method(a: 'abc', b: 'abc', + ^^ Use hash rockets syntax. + ^^ Use hash rockets syntax. + c: 'abc', d: 'abc' + ^^ Use hash rockets syntax. + ^^ Use hash rockets syntax. + ^^^^^^^^ Align the keys of a hash literal if they span more than one line. + ) + RUBY expect_correction(<<~RUBY) -some_method(:a => 'abc', :b => 'abc', - :c => 'abc', :d => 'abc' - ) - RUBY + some_method(:a => 'abc', :b => 'abc', + :c => 'abc', :d => 'abc' + ) + RUBY end end end @@ -681,5 +682,4 @@ expect(new_source).to eq('{ :a => 1, "b" => 2 }') end end - end