style: improve keg_only style checks
This commit is contained in:
		
							parent
							
								
									505c1e7240
								
							
						
					
					
						commit
						e252de5295
					
				@ -22,18 +22,28 @@ module RuboCop
 | 
			
		||||
            Firefox
 | 
			
		||||
          ].freeze
 | 
			
		||||
 | 
			
		||||
          reason = string_content(parameters(keg_only_node).first)
 | 
			
		||||
          reason = parameters(keg_only_node).first
 | 
			
		||||
          offending_node(reason)
 | 
			
		||||
          name = Regexp.new(@formula_name, Regexp::IGNORECASE)
 | 
			
		||||
          reason = reason.sub(name, "")
 | 
			
		||||
          reason = string_content(reason).sub(name, "")
 | 
			
		||||
          first_word = reason.split.first
 | 
			
		||||
 | 
			
		||||
          if reason =~ /\A[A-Z]/ && !reason.start_with?(*allowlist)
 | 
			
		||||
            problem "'#{first_word}' from the keg_only reason should be '#{first_word.downcase}'."
 | 
			
		||||
            problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'."
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          return unless reason.end_with?(".")
 | 
			
		||||
 | 
			
		||||
          problem "keg_only reason should not end with a period."
 | 
			
		||||
          problem "`keg_only` reason should not end with a period."
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def autocorrect(node)
 | 
			
		||||
          lambda do |corrector|
 | 
			
		||||
            reason = string_content(node)
 | 
			
		||||
            reason[0] = reason[0].downcase
 | 
			
		||||
            reason = reason.delete_suffix(".")
 | 
			
		||||
            corrector.replace(node.source_range, "\"#{reason}\"")
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
 | 
			
		||||
        keg_only "Because why not"
 | 
			
		||||
        ^^^^^^^^^^^^^^^^^^^^^^^^^^ 'Because' from the keg_only reason should be 'because'.
 | 
			
		||||
                 ^^^^^^^^^^^^^^^^^ 'Because' from the `keg_only` reason should be 'because'.
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
  end
 | 
			
		||||
@ -25,11 +25,53 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
 | 
			
		||||
        keg_only "ending with a period."
 | 
			
		||||
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ keg_only reason should not end with a period.
 | 
			
		||||
                 ^^^^^^^^^^^^^^^^^^^^^^^ `keg_only` reason should not end with a period.
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  specify "keg_only_autocorrects_downcasing" do
 | 
			
		||||
    source = <<~RUBY
 | 
			
		||||
      class Foo < Formula
 | 
			
		||||
        url "https://brew.sh/foo-1.0.tgz"
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
        keg_only "Because why not"
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
 | 
			
		||||
    corrected_source = <<~RUBY
 | 
			
		||||
      class Foo < Formula
 | 
			
		||||
        url "https://brew.sh/foo-1.0.tgz"
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
        keg_only "because why not"
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
 | 
			
		||||
    new_source = autocorrect_source(source)
 | 
			
		||||
    expect(new_source).to eq(corrected_source)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  specify "keg_only_autocorrects_redundant_period" do
 | 
			
		||||
    source = <<~RUBY
 | 
			
		||||
      class Foo < Formula
 | 
			
		||||
        url "https://brew.sh/foo-1.0.tgz"
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
        keg_only "ending with a period."
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
 | 
			
		||||
    corrected_source = <<~RUBY
 | 
			
		||||
      class Foo < Formula
 | 
			
		||||
        url "https://brew.sh/foo-1.0.tgz"
 | 
			
		||||
        homepage "https://brew.sh"
 | 
			
		||||
        keg_only "ending with a period"
 | 
			
		||||
      end
 | 
			
		||||
    RUBY
 | 
			
		||||
 | 
			
		||||
    new_source = autocorrect_source(source)
 | 
			
		||||
    expect(new_source).to eq(corrected_source)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  specify "keg_only_handles_block_correctly" do
 | 
			
		||||
    expect_no_offenses(<<~RUBY)
 | 
			
		||||
      class Foo < Formula
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user