Skip to content

Commit

Permalink
rubocop#8347: RSpec and Rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Orner committed Jul 16, 2020
1 parent 9ff222d commit f3e3a32
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/hash_syntax.rb
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/rspec/cop_helper.rb
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions lib/rubocop/rspec/expect_offense.rb
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions lib/rubocop/rspec/shared_contexts.rb
Expand Up @@ -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

Expand All @@ -120,7 +120,6 @@ def source_range(range, buffer: source_buffer)
end
end
end

end

RSpec.shared_context 'mock console output' do
Expand Down
30 changes: 15 additions & 15 deletions spec/rubocop/cop/style/hash_syntax_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -681,5 +682,4 @@
expect(new_source).to eq('{ :a => 1, "b" => 2 }')
end
end

end

0 comments on commit f3e3a32

Please sign in to comment.