module RuboCop::Cop::OrderedGemNode
Common functionality for Bundler/OrderedGems and Gemspec/OrderedDependencies.
Private Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 28 def autocorrect(node) previous = previous_declaration(node) current_range = declaration_with_comment(node) previous_range = declaration_with_comment(previous) lambda do |corrector| swap_range(corrector, current_range, previous_range) end end
case_insensitive_out_of_order?(string_a, string_b)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 10 def case_insensitive_out_of_order?(string_a, string_b) string_a.downcase < string_b.downcase end
consecutive_lines(previous, current)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 14 def consecutive_lines(previous, current) first_line = get_source_range(current).first_line previous.source_range.last_line == first_line - 1 end
declaration_with_comment(node)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 39 def declaration_with_comment(node) buffer = processed_source.buffer begin_pos = get_source_range(node).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
gem_name(declaration_node)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 24 def gem_name(declaration_node) declaration_node.first_argument.str_content end
get_source_range(node)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 54 def get_source_range(node) unless cop_config['TreatCommentsAsGroupSeparators'] first_comment = processed_source.ast_with_comments[node].first return first_comment.loc.expression unless first_comment.nil? end node.source_range end
register_offense(previous, current)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 19 def register_offense(previous, current) message = format(self.class::MSG, gem_name(current), gem_name(previous)) add_offense(current, message: message) end
swap_range(corrector, range1, range2)
click to toggle source
# File lib/rubocop/cop/mixin/ordered_gem_node.rb, line 47 def swap_range(corrector, range1, range2) src1 = range1.source src2 = range2.source corrector.replace(range1, src2) corrector.replace(range2, src1) end