Class: RuboCop::Cop::Cask::UninstallMethodsOrder Private
- Extended by:
- AutoCorrector
- Includes:
- HelperFunctions
- Defined in:
- rubocops/cask/uninstall_methods_order.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
This cop checks for the correct order of methods within the 'uninstall' and 'zap' stanzas.
Constant Summary collapse
- MSG =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
T.let("`%<method>s` method out of order", String)
Instance Method Summary collapse
- #method_order_index(method_node) ⇒ Integer private
- #on_send(node) ⇒ void private
- #report_and_correct_offense(method, hash_node, expected_order, comments) ⇒ void private
Methods included from HelperFunctions
#block_method_called_in_block?, #block_size, #check_precedence, #class_name, #component_precedes?, #end_column, #expression_negated?, #find_all_blocks, #find_block, #find_blocks, #find_const, #find_every_func_call_by_name, #find_every_method_call_by_name, #find_instance_call, #find_instance_method_call, #find_method_calls_by_name, #find_method_def, #find_method_with_args, #find_node_method_by_name, #find_strings, #format_component, #line_number, #line_start_column, #method_called?, #method_called_ever?, #method_name, #node_equals?, #offending_node, #parameters, #parameters_passed?, #problem, #regex_match_group, #size, #source_buffer, #start_column, #string_content
Instance Method Details
#method_order_index(method_node) ⇒ Integer
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
58 59 60 61 |
# File 'rubocops/cask/uninstall_methods_order.rb', line 58 def method_order_index(method_node) method_name = method_node.children.first RuboCop::Cask::Constants::UNINSTALL_METHODS_ORDER.index(method_name) || -1 end |
#on_send(node) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'rubocops/cask/uninstall_methods_order.rb', line 17 def on_send(node) return unless [:zap, :uninstall].include?(node.method_name) hash_node = node.arguments.first return if hash_node.nil? || (!hash_node.is_a?(AST::Node) && !hash_node.hash_type?) method_nodes = hash_node.pairs.map(&:key) expected_order = method_nodes.sort_by { |method| method_order_index(method) } comments = processed_source.comments method_nodes.each_with_index do |method, index| next if method == expected_order[index] report_and_correct_offense(method, hash_node, expected_order, comments) end end |
#report_and_correct_offense(method, hash_node, expected_order, comments) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'rubocops/cask/uninstall_methods_order.rb', line 40 def report_and_correct_offense(method, hash_node, expected_order, comments) add_offense(method, message: format(MSG, method: method.children.first)) do |corrector| indentation = " " * (start_column(method) - line_start_column(method)) new_code = expected_order.map do |expected_method| method_pair = hash_node.pairs.find { |pair| pair.key == expected_method } source = method_pair.source # Find and attach a comment on the same line as the method_pair, if any inline_comment = comments.find do |comment| comment.location.line == method_pair.loc.line && comment.location.column > method_pair.loc.column end inline_comment ? "#{source} #{inline_comment.text}" : source end.join(",\n#{indentation}") corrector.replace(hash_node.source_range, new_code) end end |