From 0c6820493c13822a25db4655973801c8736854ad Mon Sep 17 00:00:00 2001 From: Maxim Krizhanovsky Date: Tue, 16 Jul 2024 15:33:56 +0100 Subject: [PATCH] Fix RuboCop 1.65 compatability (#16) --- .../correctors/ordered_methods_corrector.rb | 9 +-- lib/rubocop/cop/layout/ordered_methods.rb | 61 +++++-------------- rubocop-ordered_methods.gemspec | 2 +- spec/support/file_helper.rb | 8 +-- 4 files changed, 23 insertions(+), 57 deletions(-) diff --git a/lib/rubocop/cop/correctors/ordered_methods_corrector.rb b/lib/rubocop/cop/correctors/ordered_methods_corrector.rb index 3943ea3..580af10 100644 --- a/lib/rubocop/cop/correctors/ordered_methods_corrector.rb +++ b/lib/rubocop/cop/correctors/ordered_methods_corrector.rb @@ -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 @@ -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 diff --git a/lib/rubocop/cop/layout/ordered_methods.rb b/lib/rubocop/cop/layout/ordered_methods.rb index 2a23211..293bda4 100644 --- a/lib/rubocop/cop/layout/ordered_methods.rb +++ b/lib/rubocop/cop/layout/ordered_methods.rb @@ -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 = { @@ -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 @@ -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 @@ -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? @@ -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 diff --git a/rubocop-ordered_methods.gemspec b/rubocop-ordered_methods.gemspec index c06dd93..6224d9c 100644 --- a/rubocop-ordered_methods.gemspec +++ b/rubocop-ordered_methods.gemspec @@ -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 diff --git a/spec/support/file_helper.rb b/spec/support/file_helper.rb index 86e330b..0934077 100644 --- a/spec/support/file_helper.rb +++ b/spec/support/file_helper.rb @@ -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) @@ -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