diff --git a/Library/Homebrew/rubocops/lines.rb b/Library/Homebrew/rubocops/lines.rb index a026325b0f..a29092b425 100644 --- a/Library/Homebrew/rubocops/lines.rb +++ b/Library/Homebrew/rubocops/lines.rb @@ -272,6 +272,20 @@ module RuboCop end end + class Licenses < 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.hash_type? + return unless license.each_descendant(:hash).count.positive? + return if license.source.include?("\n") + + problem "Split nested license declarations onto multiple lines" + end + end + class Miscellaneous < FormulaCop def audit_formula(_node, _class_node, _parent_class_node, body_node) # FileUtils is included in Formula diff --git a/Library/Homebrew/test/rubocops/lines_spec.rb b/Library/Homebrew/test/rubocops/lines_spec.rb index b739da80b9..17b2537641 100644 --- a/Library/Homebrew/test/rubocops/lines_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_spec.rb @@ -647,6 +647,66 @@ describe RuboCop::Cop::FormulaAudit::LicenseArrays do end end +describe RuboCop::Cop::FormulaAudit::Licenses 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 "allow multiline nested license hashes" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + desc "foo" + url 'https://brew.sh/foo-1.0.tgz' + license any_of: [ + "MIT", + all_of: ["0BSD", "Zlib"], + ] + end + RUBY + end + + it "require multiple lines for nested license hashes" do + expect_offense(<<~RUBY) + class Foo < Formula + desc "foo" + url 'https://brew.sh/foo-1.0.tgz' + license any_of: ["MIT", all_of: ["0BSD", "Zlib"]] + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Split nested license declarations onto multiple lines + end + RUBY + end + end +end + describe RuboCop::Cop::FormulaAudit::Miscellaneous do subject(:cop) { described_class.new }