Class: RuboCop::Cop::FormulaAudit::ComponentsOrder
- Inherits:
-
RuboCop::Cop::FormulaCop
- Object
- Base
- RuboCop::Cop::FormulaCop
- RuboCop::Cop::FormulaAudit::ComponentsOrder
- Extended by:
- AutoCorrector
- Defined in:
- rubocops/components_order.rb
Overview
This cop checks for correct order of components in formulae.
component_precedence_list
has component hierarchy in a nested list where each sub array contains components’ details which are at same precedence level
Instance Attribute Summary
Attributes inherited from RuboCop::Cop::FormulaCop
Instance Method Summary collapse
-
#audit_formula(_node, _class_node, _parent_class_node, body_node) ⇒ Object
-
#check_block_component_order(component_precedence_list, block) ⇒ Object
-
#check_on_system_block_content(component_precedence_list, on_system_block) ⇒ Object
-
#check_order(component_precedence_list, body_node) ⇒ Object
-
#component_problem(component1, component2) ⇒ Object
Method to report and correct component precedence violations.
-
#get_state(node1) ⇒ Object
Returns precedence index and component’s index to properly reorder and group during autocorrect.
-
#reorder_components(corrector, node1, node2) ⇒ Object
Reorder two nodes in the source, using the corrector instance in autocorrect method.
Methods inherited from RuboCop::Cop::FormulaCop
#audit_comments, #audit_urls, #caveats_strings, #depends_on?, #depends_on_name_type?, #formula_tap, #get_checksum_node, #on_class, #style_exceptions_dir, #tap_style_exception?, #versioned_formula?
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
#audit_formula(_node, _class_node, _parent_class_node, body_node) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'rubocops/components_order.rb', line 17 def audit_formula(_node, _class_node, _parent_class_node, body_node) return if body_node.nil? @present_components, @offensive_nodes = check_order(FORMULA_COMPONENT_PRECEDENCE_LIST, body_node) component_problem @offensive_nodes[0], @offensive_nodes[1] if @offensive_nodes component_precedence_list = [ [{ name: :depends_on, type: :method_call }], [{ name: :resource, type: :block_call }], [{ name: :patch, type: :method_call }, { name: :patch, type: :block_call }], ] head_blocks = find_blocks(body_node, :head) head_blocks.each do |head_block| check_block_component_order(FORMULA_COMPONENT_PRECEDENCE_LIST, head_block) end on_system_methods.each do |on_method| on_method_blocks = find_blocks(body_node, on_method) next if on_method_blocks.empty? if on_method_blocks.length > 1 @offensive_node = on_method_blocks.second problem "there can only be one `#{on_method}` block in a formula." end check_on_system_block_content(component_precedence_list, on_method_blocks.first) end resource_blocks = find_blocks(body_node, :resource) resource_blocks.each do |resource_block| check_block_component_order(FORMULA_COMPONENT_PRECEDENCE_LIST, resource_block) on_system_blocks = {} on_system_methods.each do |on_method| on_system_blocks[on_method] = find_blocks(resource_block.body, on_method) end if on_system_blocks.empty? # Found nothing. Try without .body as depending on the code, # on_{system} might be in .body or not ... on_system_methods.each do |on_method| on_system_blocks[on_method] = find_blocks(resource_block, on_method) end end next if on_system_blocks.empty? @offensive_node = resource_block on_system_bodies = T.let([], T::Array[[RuboCop::AST::BlockNode, RuboCop::AST::Node]]) on_system_blocks.each_value do |blocks| blocks.each do |on_system_block| on_system_body = on_system_block.body branches = on_system_body.if_type? ? on_system_body.branches : [on_system_body] on_system_bodies += branches.map { |branch| [on_system_block, branch] } end end = T.let(nil, T.nilable(String)) allowed_methods = [ [:url, :sha256], [:url, :mirror, :sha256], [:url, :version, :sha256], [:url, :mirror, :version, :sha256], ] minimum_methods = allowed_methods.first.map { |m| "`#{m}`" }.to_sentence maximum_methods = allowed_methods.last.map { |m| "`#{m}`" }.to_sentence on_system_bodies.each do |on_system_block, on_system_body| method_name = on_system_block.method_name child_nodes = on_system_body.begin_type? ? on_system_body.child_nodes : [on_system_body] if child_nodes.all? { |n| n.send_type? || n.block_type? || n.lvasgn_type? } method_names = child_nodes.map do |node| next if node.lvasgn_type? next if node.method_name == :patch next if on_system_methods.include? node.method_name node.method_name end.compact next if method_names.empty? || allowed_methods.include?(method_names) end offending_node(on_system_block) = "`#{method_name}` blocks within `resource` blocks must contain at least " \ "#{minimum_methods} and at most #{maximum_methods} (in order)." break end if problem next end on_system_blocks.each do |on_method, blocks| if blocks.length > 1 problem "there can only be one `#{on_method}` block in a resource block." next end end end end |
#check_block_component_order(component_precedence_list, block) ⇒ Object
121 122 123 124 |
# File 'rubocops/components_order.rb', line 121 def check_block_component_order(component_precedence_list, block) @present_components, offensive_node = check_order(component_precedence_list, block.body) component_problem(*offensive_node) if offensive_node end |
#check_on_system_block_content(component_precedence_list, on_system_block) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'rubocops/components_order.rb', line 126 def check_on_system_block_content(component_precedence_list, on_system_block) if on_system_block.body.block_type? && !on_system_methods.include?(on_system_block.body.method_name) offending_node(on_system_block) problem "Nest `#{on_system_block.method_name}` blocks inside `#{on_system_block.body.method_name}` " \ "blocks when there is only one inner block." do |corrector| original_source = on_system_block.source.split("\n") new_source = [original_source.second, original_source.first, *original_source.drop(2)] corrector.replace(on_system_block.source_range, new_source.join("\n")) end end on_system_allowed_methods = %w[ livecheck keg_only disable! deprecate! depends_on conflicts_with fails_with resource patch ] on_system_allowed_methods += on_system_methods.map(&:to_s) _, offensive_node = check_order(component_precedence_list, on_system_block.body) component_problem(*offensive_node) if offensive_node child_nodes = on_system_block.body.begin_type? ? on_system_block.body.child_nodes : [on_system_block.body] child_nodes.each do |child| valid_node = depends_on_node?(child) # Check for RuboCop::AST::SendNode and RuboCop::AST::BlockNode instances # only, as we are checking the method_name for `patch`, `resource`, etc. method_type = child.send_type? || child.block_type? next unless method_type valid_node ||= on_system_allowed_methods.include? child.method_name.to_s @offensive_node = child next if valid_node problem "`#{on_system_block.method_name}` cannot include `#{child.method_name}`. " \ "Only #{on_system_allowed_methods.map { |m| "`#{m}`" }.to_sentence} are allowed." end end |
#check_order(component_precedence_list, body_node) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'rubocops/components_order.rb', line 199 def check_order(component_precedence_list, body_node) present_components = component_precedence_list.map do |components| components.flat_map do |component| case component[:type] when :method_call find_method_calls_by_name(body_node, component[:name]).to_a when :block_call find_blocks(body_node, component[:name]).to_a when :method_definition find_method_def(body_node, component[:name]) end end.compact end # Check if each present_components is above rest of the present_components offensive_nodes = T.let(nil, T.nilable(T::Array[RuboCop::AST::Node])) present_components.take(present_components.size - 1).each_with_index do |preceding_component, p_idx| next if preceding_component.empty? present_components.drop(p_idx + 1).each do |succeeding_component| next if succeeding_component.empty? offensive_nodes = check_precedence(preceding_component, succeeding_component) return [present_components, offensive_nodes] if offensive_nodes end end nil end |
#component_problem(component1, component2) ⇒ Object
Method to report and correct component precedence violations.
229 230 231 232 233 234 235 236 237 |
# File 'rubocops/components_order.rb', line 229 def component_problem(component1, component2) return if tap_style_exception? :components_order_exceptions problem "`#{format_component(component1)}` (line #{line_number(component1)}) " \ "should be put before `#{format_component(component2)}` " \ "(line #{line_number(component2)})" do |corrector| reorder_components(corrector, component1, component2) end end |
#get_state(node1) ⇒ Object
Returns precedence index and component’s index to properly reorder and group during autocorrect.
193 194 195 196 197 |
# File 'rubocops/components_order.rb', line 193 def get_state(node1) @present_components.each_with_index do |comp, idx| return [idx, comp.index(node1), comp] if comp.member?(node1) end end |
#reorder_components(corrector, node1, node2) ⇒ Object
Reorder two nodes in the source, using the corrector instance in autocorrect method. Components of same type are grouped together when rewriting the source. Linebreaks are introduced if components are of two different methods/blocks/multilines.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'rubocops/components_order.rb', line 171 def reorder_components(corrector, node1, node2) # order_idx : node1's index in component_precedence_list # curr_p_idx: node1's index in preceding_comp_arr # preceding_comp_arr: array containing components of same type order_idx, curr_p_idx, preceding_comp_arr = get_state(node1) # curr_p_idx.positive? means node1 needs to be grouped with its own kind if curr_p_idx.positive? node2 = preceding_comp_arr[curr_p_idx - 1] indentation = " " * (start_column(node2) - line_start_column(node2)) line_breaks = node2.multiline? ? "\n\n" : "\n" corrector.insert_after(node2.source_range, line_breaks + indentation + node1.source) else indentation = " " * (start_column(node2) - line_start_column(node2)) # No line breaks up to version_scheme, order_idx == 8 line_breaks = (order_idx > 8) ? "\n\n" : "\n" corrector.insert_before(node2.source_range, node1.source + line_breaks + indentation) end corrector.remove(range_with_surrounding_space(range: node1.source_range, side: :left)) end |