Ensure that stanza grouping works for nested stanzas with comments
- Since moving `comments_hash` to `Stanza`, we've been using the wrong kind of "comments": the comments for the _stanza_, not the comments for the entire Cask. - Add a test to ensure this actually works. There was previously an infinite loop here due to the bad `comments`, visible in a `StanzaOrder` cop test, which I speculatively added a failing test for. Turns out that supporting nested stanza _ordering_ (vs. just grouping) is a whole separate piece of work (there are multiple TODOs there already), so I've backed that out and will do that separately.
This commit is contained in:
		
							parent
							
								
									8b9ee05118
								
							
						
					
					
						commit
						6de61e4994
					
				@ -32,7 +32,7 @@ module RuboCop
 | 
			
		||||
 | 
			
		||||
          @stanzas ||= cask_body.each_node
 | 
			
		||||
                                .select(&:stanza?)
 | 
			
		||||
                                .map { |node| Stanza.new(node) }
 | 
			
		||||
                                .map { |node| Stanza.new(node, comments) }
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def toplevel_stanzas
 | 
			
		||||
 | 
			
		||||
@ -12,11 +12,12 @@ module RuboCop
 | 
			
		||||
      class Stanza
 | 
			
		||||
        extend Forwardable
 | 
			
		||||
 | 
			
		||||
        def initialize(method_node)
 | 
			
		||||
        def initialize(method_node, all_comments)
 | 
			
		||||
          @method_node = method_node
 | 
			
		||||
          @all_comments = all_comments
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        attr_reader :method_node
 | 
			
		||||
        attr_reader :method_node, :all_comments
 | 
			
		||||
 | 
			
		||||
        alias stanza_node method_node
 | 
			
		||||
 | 
			
		||||
@ -58,10 +59,7 @@ module RuboCop
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def comments_hash
 | 
			
		||||
          @comments_hash ||= Parser::Source::Comment.associate_locations(
 | 
			
		||||
            stanza_node.parent,
 | 
			
		||||
            comments,
 | 
			
		||||
          )
 | 
			
		||||
          @comments_hash ||= Parser::Source::Comment.associate_locations(stanza_node.parent, all_comments)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def ==(other)
 | 
			
		||||
 | 
			
		||||
@ -32,7 +32,7 @@ module RuboCop
 | 
			
		||||
 | 
			
		||||
            block_contents = on_block.child_nodes.select(&:begin_type?)
 | 
			
		||||
            inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?)
 | 
			
		||||
            inner_stanzas = inner_nodes.map { |node| RuboCop::Cask::AST::Stanza.new(node) }
 | 
			
		||||
            inner_stanzas = inner_nodes.map { |node| RuboCop::Cask::AST::Stanza.new(node, processed_source.comments) }
 | 
			
		||||
 | 
			
		||||
            add_offenses(inner_stanzas)
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
@ -545,6 +545,41 @@ describe RuboCop::Cop::Cask::StanzaGrouping do
 | 
			
		||||
      include_examples "autocorrects source"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    describe "nested `on_*` blocks with comments" do
 | 
			
		||||
      let(:source) do
 | 
			
		||||
        <<~CASK
 | 
			
		||||
          cask 'foo' do
 | 
			
		||||
            on_arm do
 | 
			
		||||
              version "1.0.2"
 | 
			
		||||
 | 
			
		||||
              sha256 :no_check # comment on same line
 | 
			
		||||
            end
 | 
			
		||||
            on_intel do
 | 
			
		||||
              version "0.9.8"
 | 
			
		||||
              sha256 :no_check
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
        CASK
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      let(:correct_source) do
 | 
			
		||||
        <<~CASK
 | 
			
		||||
          cask 'foo' do
 | 
			
		||||
            on_arm do
 | 
			
		||||
              version "1.0.2"
 | 
			
		||||
              sha256 :no_check # comment on same line
 | 
			
		||||
            end
 | 
			
		||||
            on_intel do
 | 
			
		||||
              version "0.9.8"
 | 
			
		||||
              sha256 :no_check
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
        CASK
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      include_examples "autocorrects source"
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # TODO: Maybe this should be fixed too?
 | 
			
		||||
    describe "inner erroneously grouped nested livecheck block contents are ignored" do
 | 
			
		||||
      let(:source) do
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user