very wip but reimagined
- Thanks to Markus on Slack for saying "the cop should only apply to the content of the blocks, or more specifically only to stanzas that are direct children of cask or on_* blocks", which made me realize that I was overcomplicating things.
This commit is contained in:
		
							parent
							
								
									a493787125
								
							
						
					
					
						commit
						8035d46dfe
					
				@ -17,46 +17,38 @@ module RuboCop
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        def on_cask(cask_block)
 | 
					        def on_cask(cask_block)
 | 
				
			||||||
          @cask_block = cask_block
 | 
					          @cask_block = cask_block
 | 
				
			||||||
          stanzas = [toplevel_stanzas]
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          puts "before on blocks: #{stanzas.first.map(&:stanza_name)}"
 | 
					          # Find all the stanzas that are direct children of the cask block or one of its `on_*` blocks.
 | 
				
			||||||
          if (on_blocks = on_system_methods(stanzas.first)).any?
 | 
					          puts "toplevel_stanzas: #{toplevel_stanzas.map(&:stanza_name).inspect}"
 | 
				
			||||||
            on_blocks.map(&:method_node).select(&:block_type?).each do |on_block|
 | 
					          outer_and_inner_stanzas = toplevel_stanzas + toplevel_stanzas.map do |stanza|
 | 
				
			||||||
              stanzas.push(inner_stanzas(on_block, processed_source.comments))
 | 
					            return stanza unless stanza.method_node&.block_type?
 | 
				
			||||||
            end
 | 
					
 | 
				
			||||||
 | 
					            inner_stanzas(stanza.method_node, stanza.comments)
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
          puts "after on blocks: #{stanzas.last.map(&:method_node).select(&:send_type?).map(&:method_name) }" if on_blocks
 | 
					          puts "outer_and_inner_stanzas: #{outer_and_inner_stanzas.flatten.map(&:stanza_name).inspect}"
 | 
				
			||||||
          add_offenses(stanzas)
 | 
					          add_offenses(outer_and_inner_stanzas.flatten)
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private
 | 
					        private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        attr_reader :cask_block
 | 
					        attr_reader :cask_block
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def_delegators :cask_block, :cask_node, :toplevel_stanzas,
 | 
					        def_delegators :cask_block, :cask_node, :toplevel_stanzas
 | 
				
			||||||
                       :sorted_toplevel_stanzas
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def add_offenses(outer_and_inner_stanzas)
 | 
					        def add_offenses(outer_and_inner_stanzas)
 | 
				
			||||||
          outer_and_inner_stanzas.map do |stanza_types|
 | 
					          outer_and_inner_stanzas.each_cons(2) do |stanza1, stanza2|
 | 
				
			||||||
            offending_stanzas(stanza_types, sorted_toplevel_stanzas).flatten.compact.each do |stanza|
 | 
					            next if stanza_order_index(stanza1.stanza_name) < stanza_order_index(stanza2.stanza_name)
 | 
				
			||||||
              name = stanza.respond_to?(:method_name) ? stanza.method_name : stanza.stanza_name
 | 
					
 | 
				
			||||||
              message = format(MESSAGE, stanza: name)
 | 
					            puts "#{stanza2.stanza_name} should come before #{stanza1.stanza_name}"
 | 
				
			||||||
              add_offense(stanza.source_range_with_comments, message: message) do |corrector|
 | 
					            add_offense(stanza1.method_node, message: format(MESSAGE, stanza: stanza1.stanza_name)) do |corrector|
 | 
				
			||||||
                correct_stanza_index = outer_and_inner_stanzas.flatten.index(stanza)
 | 
					              # TODO: Move the stanza to the correct location.
 | 
				
			||||||
                correct_stanza = sorted_toplevel_stanzas[correct_stanza_index]
 | 
					 | 
				
			||||||
                corrector.replace(stanza&.source_range_with_comments, correct_stanza&.source_with_comments)
 | 
					 | 
				
			||||||
              end
 | 
					 | 
				
			||||||
            end
 | 
					            end
 | 
				
			||||||
          end
 | 
					          end
 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        def offending_stanzas(stanzas, sorted_stanzas)
 | 
					        def stanza_order_index(stanza_name)
 | 
				
			||||||
          stanza_pairs = stanzas.zip(sorted_stanzas)
 | 
					          RuboCop::Cask::Constants::STANZA_ORDER.index(stanza_name)
 | 
				
			||||||
          stanza_pairs.each_with_object([]) do |stanza_pair, offending_stanzas|
 | 
					 | 
				
			||||||
            stanza, sorted_stanza = *stanza_pair
 | 
					 | 
				
			||||||
            offending_stanzas << stanza if stanza != sorted_stanza
 | 
					 | 
				
			||||||
          end
 | 
					 | 
				
			||||||
        end
 | 
					        end
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user