Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cop::Base for Bundler/OrderedGems and Gemspec/OrderedDependencies #10123

Merged
merged 1 commit into from Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 3 additions & 12 deletions lib/rubocop/cop/bundler/ordered_gems.rb
Expand Up @@ -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 `%<previous>s` should appear before `%<current>s`.'

def investigate(processed_source)
def on_new_investigation
return if processed_source.blank?

gem_declarations(processed_source.ast)
Expand All @@ -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)
Expand Down
21 changes: 11 additions & 10 deletions lib/rubocop/cop/correctors/ordered_gem_corrector.rb
Expand Up @@ -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,
Expand All @@ -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
Expand Down
15 changes: 3 additions & 12 deletions lib/rubocop/cop/gemspec/ordered_dependencies.rb
Expand Up @@ -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 `%<previous>s` should appear before `%<current>s`.'

def investigate(processed_source)
def on_new_investigation
return if processed_source.blank?

dependency_declarations(processed_source.ast)
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/cop/mixin/ordered_gem_node.rb
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/team_spec.rb
Expand Up @@ -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'
Expand Down