Skip to content
Snippets Groups Projects
Commit 97b1b75d authored by Rylan Polster's avatar Rylan Polster
Browse files

style: forbid one line nested license expressions

parent ef447a38
No related branches found
No related tags found
No related merge requests found
...@@ -272,6 +272,20 @@ module RuboCop ...@@ -272,6 +272,20 @@ module RuboCop
end end
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 class Miscellaneous < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node) def audit_formula(_node, _class_node, _parent_class_node, body_node)
# FileUtils is included in Formula # FileUtils is included in Formula
......
...@@ -647,6 +647,66 @@ describe RuboCop::Cop::FormulaAudit::LicenseArrays do ...@@ -647,6 +647,66 @@ describe RuboCop::Cop::FormulaAudit::LicenseArrays do
end end
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 describe RuboCop::Cop::FormulaAudit::Miscellaneous do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment