style: convert from license array to license hash
This commit is contained in:
		
							parent
							
								
									01f7446316
								
							
						
					
					
						commit
						ef447a38c6
					
				@ -254,6 +254,24 @@ module RuboCop
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      class LicenseArrays < FormulaCop
 | 
			
		||||
        def audit_formula(_node, _class_node, _parent_class_node, body_node)
 | 
			
		||||
          license_node = find_node_method_by_name(body_node, :license)
 | 
			
		||||
          return unless license_node
 | 
			
		||||
 | 
			
		||||
          license = parameters(license_node).first
 | 
			
		||||
          return unless license.array_type?
 | 
			
		||||
 | 
			
		||||
          problem "Use `license any_of: #{license.source}` instead of `license #{license.source}`"
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def autocorrect(node)
 | 
			
		||||
          lambda do |corrector|
 | 
			
		||||
            corrector.replace(node.source_range, "license any_of: #{parameters(node).first.source}")
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      class Miscellaneous < FormulaCop
 | 
			
		||||
        def audit_formula(_node, _class_node, _parent_class_node, body_node)
 | 
			
		||||
          # FileUtils is included in Formula
 | 
			
		||||
 | 
			
		||||
@ -579,6 +579,74 @@ describe RuboCop::Cop::FormulaAudit::ShellVariables do
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
describe RuboCop::Cop::FormulaAudit::LicenseArrays do
 | 
			
		||||
  subject(:cop) { described_class.new }
 | 
			
		||||
 | 
			
		||||
  context "When auditing licenses" do
 | 
			
		||||
    it "allow license strings" do
 | 
			
		||||
      expect_no_offenses(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license "MIT"
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "allow license symbols" do
 | 
			
		||||
      expect_no_offenses(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license :public_domain
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "allow license hashes" do
 | 
			
		||||
      expect_no_offenses(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license any_of: ["MIT", "0BSD"]
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "require using :any_of instead of a license array" do
 | 
			
		||||
      expect_offense(<<~RUBY)
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license ["MIT", "0BSD"]
 | 
			
		||||
          ^^^^^^^^^^^^^^^^^^^^^^^ Use `license any_of: ["MIT", "0BSD"]` instead of `license ["MIT", "0BSD"]`
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "corrects license arrays to hash with :any_of" do
 | 
			
		||||
      source = <<~RUBY
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license ["MIT", "0BSD"]
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
 | 
			
		||||
      corrected_source = <<~RUBY
 | 
			
		||||
        class Foo < Formula
 | 
			
		||||
          desc "foo"
 | 
			
		||||
          url 'https://brew.sh/foo-1.0.tgz'
 | 
			
		||||
          license any_of: ["MIT", "0BSD"]
 | 
			
		||||
        end
 | 
			
		||||
      RUBY
 | 
			
		||||
 | 
			
		||||
      new_source = autocorrect_source(source)
 | 
			
		||||
      expect(new_source).to eq(corrected_source)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
describe RuboCop::Cop::FormulaAudit::Miscellaneous do
 | 
			
		||||
  subject(:cop) { described_class.new }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user