From d52c8dc33435831162972ba6472c0ba1cd28c209 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Fri, 24 Sep 2021 12:34:24 -0400 Subject: [PATCH] Use `Cop::Base` for `Bundler/OrderedGems` and `Gemspec/OrderedDependencies`. --- lib/rubocop/cop/bundler/ordered_gems.rb | 15 +++---------- .../cop/correctors/ordered_gem_corrector.rb | 21 ++++++++++--------- .../cop/gemspec/ordered_dependencies.rb | 15 +++---------- lib/rubocop/cop/mixin/ordered_gem_node.rb | 10 ++++++++- spec/rubocop/cop/team_spec.rb | 3 +-- 5 files changed, 27 insertions(+), 37 deletions(-) diff --git a/lib/rubocop/cop/bundler/ordered_gems.rb b/lib/rubocop/cop/bundler/ordered_gems.rb index 57d1f2f7ee2..a8ab4f0a780 100644 --- a/lib/rubocop/cop/bundler/ordered_gems.rb +++ b/lib/rubocop/cop/bundler/ordered_gems.rb @@ -24,15 +24,15 @@ module Bundler # gem 'rubocop' # # For tests # gem 'rspec' - class OrderedGems < Cop # rubocop:disable InternalAffairs/InheritDeprecatedCopClass - include ConfigurableEnforcedStyle + class OrderedGems < Base + extend AutoCorrector include OrderedGemNode MSG = 'Gems should be sorted in an alphabetical order within their '\ 'section of the Gemfile. '\ 'Gem `%s` should appear before `%s`.' - def investigate(processed_source) + def on_new_investigation return if processed_source.blank? gem_declarations(processed_source.ast) @@ -44,15 +44,6 @@ def investigate(processed_source) end end - def autocorrect(node) - OrderedGemCorrector.correct( - processed_source, - node, - previous_declaration(node), - treat_comments_as_separators - ) - end - private def previous_declaration(node) diff --git a/lib/rubocop/cop/correctors/ordered_gem_corrector.rb b/lib/rubocop/cop/correctors/ordered_gem_corrector.rb index 834f790dfda..af1effc98ab 100644 --- a/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +++ b/lib/rubocop/cop/correctors/ordered_gem_corrector.rb @@ -4,9 +4,10 @@ module RuboCop module Cop # This auto-corrects gem dependency order class OrderedGemCorrector - extend OrderedGemNode - class << self + include OrderedGemNode + include RangeHelp + attr_reader :processed_source, :comments_as_separators def correct(processed_source, node, @@ -17,24 +18,24 @@ def correct(processed_source, node, current_range = declaration_with_comment(node) previous_range = declaration_with_comment(previous_declaration) - ->(corrector) do swap_range(corrector, current_range, previous_range) end + ->(corrector) { swap_range(corrector, current_range, previous_range) } end private def declaration_with_comment(node) buffer = processed_source.buffer - begin_pos = get_source_range(node, comments_as_separators).begin_pos + begin_pos = range_by_whole_lines(get_source_range(node, comments_as_separators)).begin_pos end_line = buffer.line_for_position(node.loc.expression.end_pos) - end_pos = buffer.line_range(end_line).end_pos - Parser::Source::Range.new(buffer, begin_pos, end_pos) + end_pos = range_by_whole_lines(buffer.line_range(end_line), + include_final_newline: true).end_pos + + range_between(begin_pos, end_pos) end def swap_range(corrector, range1, range2) - src1 = range1.source - src2 = range2.source - corrector.replace(range1, src2) - corrector.replace(range2, src1) + corrector.insert_before(range2, range1.source) + corrector.remove(range1) end end end diff --git a/lib/rubocop/cop/gemspec/ordered_dependencies.rb b/lib/rubocop/cop/gemspec/ordered_dependencies.rb index 0d1dce1af4c..f2105827bd3 100644 --- a/lib/rubocop/cop/gemspec/ordered_dependencies.rb +++ b/lib/rubocop/cop/gemspec/ordered_dependencies.rb @@ -50,15 +50,15 @@ module Gemspec # spec.add_dependency 'rubocop' # # For tests # spec.add_dependency 'rspec' - class OrderedDependencies < Cop # rubocop:disable InternalAffairs/InheritDeprecatedCopClass - include ConfigurableEnforcedStyle + class OrderedDependencies < Base + extend AutoCorrector include OrderedGemNode MSG = 'Dependencies should be sorted in an alphabetical order within ' \ 'their section of the gemspec. '\ 'Dependency `%s` should appear before `%s`.' - def investigate(processed_source) + def on_new_investigation return if processed_source.blank? dependency_declarations(processed_source.ast) @@ -71,15 +71,6 @@ def investigate(processed_source) end end - def autocorrect(node) - OrderedGemCorrector.correct( - processed_source, - node, - previous_declaration(node), - treat_comments_as_separators - ) - end - private def previous_declaration(node) diff --git a/lib/rubocop/cop/mixin/ordered_gem_node.rb b/lib/rubocop/cop/mixin/ordered_gem_node.rb index 5cb10aba6c9..b93602b8fae 100644 --- a/lib/rubocop/cop/mixin/ordered_gem_node.rb +++ b/lib/rubocop/cop/mixin/ordered_gem_node.rb @@ -35,7 +35,15 @@ def register_offense(previous, current) previous: gem_name(current), current: gem_name(previous) ) - add_offense(current, message: message) + + add_offense(current, message: message) do |corrector| + OrderedGemCorrector.correct( + processed_source, + current, + previous_declaration(current), + treat_comments_as_separators + ).call(corrector) + end end def gem_name(declaration_node) diff --git a/spec/rubocop/cop/team_spec.rb b/spec/rubocop/cop/team_spec.rb index 2be4406a87c..7cc75d9a86f 100644 --- a/spec/rubocop/cop/team_spec.rb +++ b/spec/rubocop/cop/team_spec.rb @@ -188,8 +188,7 @@ def a include_context 'mock console output' before do - allow_any_instance_of(RuboCop::Cop::Bundler::OrderedGems) - .to receive(:autocorrect).and_return(buggy_correction) + allow(RuboCop::Cop::OrderedGemCorrector).to receive(:correct).and_return(buggy_correction) create_file(file_path, <<~RUBY) gem 'rubocop'