Skip to content

Commit

Permalink
Fix RuboCop 1.65 compatability (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darhazer authored Jul 16, 2024
1 parent 0441088 commit 0c68204
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 57 deletions.
9 changes: 3 additions & 6 deletions lib/rubocop/cop/correctors/ordered_methods_corrector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ def initialize(comment_locations, siblings, cop_config)
@cop_config = cop_config
end

def correct(node, previous_node)
def correct(node, previous_node, corrector)
AliasMethodOrderVerifier.verify!(node, previous_node)
current_range = join_surroundings(node)
previous_range = join_surroundings(previous_node)
lambda do |corrector|
corrector.replace(current_range, previous_range.source)
corrector.replace(previous_range, current_range.source)
end
corrector.swap(current_range, previous_range)
end

private
Expand All @@ -51,7 +48,7 @@ def found_qualifier?(node, next_sibling)
# @param source_range Parser::Source::Range
# @return Parser::Source::Range
def join_comments(node, source_range)
@comment_locations[node.loc].each do |comment|
@comment_locations[node].each do |comment|
source_range = source_range.join(comment.loc.expression)
end
source_range
Expand Down
61 changes: 15 additions & 46 deletions lib/rubocop/cop/layout/ordered_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ module Layout
#
# def c; end
# def d; end
class OrderedMethods < Cop
# TODO: Extending Cop is deprecated. Should extend Cop::Base.
include IgnoredMethods
class OrderedMethods < Base
extend AutoCorrector
include AllowedMethods
include RangeHelp

COMPARISONS = {
Expand All @@ -48,22 +48,15 @@ def self.method_name(node)
node.first_argument.method_name
end

def autocorrect(node)
_siblings, corrector = cache(node)
corrector.correct(node, @previous_node)
end

def on_begin(node)
start_node = node.children.find(&:class_type?)&.children&.last || node
siblings, _corrector = cache(start_node)

consecutive_methods(siblings) do |previous, current|
unless ordered?(previous, current)
@previous_node = previous
add_offense(
current,
message: "Methods should be sorted in #{cop_config['EnforcedStyle']} order."
)
consecutive_methods(start_node.children) do |previous, current|
next if ordered?(previous, current)

add_offense(current, message: message) do |corrector|
OrderedMethodsCorrector.new(
processed_source.ast_with_comments, start_node.children, cop_config
).correct(current, previous, corrector)
end
end
end
Expand All @@ -76,34 +69,6 @@ def access_modified?(node, is_class_method_block)
(node.send_type? && node.bare_access_modifier?)
end

# rubocop:disable Metrics/MethodLength
# Cache to avoid traversing the AST multiple times
def cache(node)
@cache ||= Hash.new do |h, key|
h[key.hash] = begin
siblings = node.children

# Init the corrector with the cache to avoid traversing the AST in
# the corrector.
#
# We always init the @corrector, even if @options[:auto_correct] is
# nil, because `add_offense` always attempts correction. This
# correction attempt is how RuboCop knows if the offense can be
# labeled "[Correctable]".
comment_locations = ::Parser::Source::Comment.associate_locations(
processed_source.ast,
processed_source.comments
)
corrector = OrderedMethodsCorrector.new(comment_locations, siblings, cop_config)

[siblings, corrector]
end
end

@cache[node.hash]
end
# rubocop:enable Metrics/MethodLength

# We disable `Style/ExplicitBlockArgument` for performance. See
# https://github.com/shanecav84/rubocop-ordered_methods/pull/5#pullrequestreview-562957146
# rubocop:disable Style/ExplicitBlockArgument
Expand Down Expand Up @@ -147,6 +112,10 @@ def group_methods_by_access_modifier(nodes)
end
end

def message
"Methods should be sorted in #{cop_config['EnforcedStyle']} order."
end

def ordered?(left_method, right_method)
comparison = COMPARISONS[cop_config['EnforcedStyle']]
raise Error, ERR_INVALID_COMPARISON if comparison.nil?
Expand All @@ -162,7 +131,7 @@ def qualifier_macro?(node)
end

def relevant_node?(node)
(node.defs_type? || node.def_type?) && !ignored_method?(node.method_name)
(node.defs_type? || node.def_type?) && !allowed_method?(node.method_name)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion rubocop-ordered_methods.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'rubocop', '>= 1.0'
spec.add_dependency 'rubocop', '>= 1.0'

spec.metadata['rubygems_mfa_required'] = 'true'
end
8 changes: 4 additions & 4 deletions spec/support/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

# Copied from `rubocop/spec/support/file_helper.rb`
module FileHelper
def create_empty_file(file_path)
create_file(file_path, '')
end

def create_file(file_path, content)
file_path = File.expand_path(file_path)

Expand All @@ -21,8 +25,4 @@ def create_file(file_path, content)

file_path
end

def create_empty_file(file_path)
create_file(file_path, '')
end
end

0 comments on commit 0c68204

Please sign in to comment.