diff --git a/Library/Homebrew/rubocops/lines.rb b/Library/Homebrew/rubocops/lines.rb index 158a7b8c60..9f7dfe437a 100644 --- a/Library/Homebrew/rubocops/lines.rb +++ b/Library/Homebrew/rubocops/lines.rb @@ -305,14 +305,18 @@ module RuboCop 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 + return if license_node.source.include?("\n") - license = parameters(license_node).first - return unless license.hash_type? - return unless license.each_descendant(:hash).count.positive? - return if license.source.include?("\n") + parameters(license_node).first.each_descendant(:hash).each do |license_hash| + next if license_exception? license_hash - problem "Split nested license declarations onto multiple lines" + problem "Split nested license declarations onto multiple lines" + end end + + def_node_matcher :license_exception?, <<~EOS + (hash (pair (sym :with) str)) + EOS end # This cop checks for other miscellaneous style violations. diff --git a/Library/Homebrew/test/rubocops/lines_spec.rb b/Library/Homebrew/test/rubocops/lines_spec.rb index 17b2537641..fa61dd9a64 100644 --- a/Library/Homebrew/test/rubocops/lines_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_spec.rb @@ -681,6 +681,16 @@ describe RuboCop::Cop::FormulaAudit::Licenses do RUBY end + it "allow license exceptions" do + expect_no_offenses(<<~RUBY) + class Foo < Formula + desc "foo" + url 'https://brew.sh/foo-1.0.tgz' + license "MIT" => { with: "LLVM-exception" } + end + RUBY + end + it "allow multiline nested license hashes" do expect_no_offenses(<<~RUBY) class Foo < Formula @@ -694,6 +704,20 @@ describe RuboCop::Cop::FormulaAudit::Licenses do RUBY end + it "allow multiline nested license hashes with exceptions" 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"], + "GPL-2.0-only" => { with: "LLVM-exception" }, + ] + end + RUBY + end + it "require multiple lines for nested license hashes" do expect_offense(<<~RUBY) class Foo < Formula