Disallow head do blocks with only url and branch

- Since `head` must now specify a url and branch, the `head do` block
  with only these stanzas can be condensed to the single-line
  `head "url", branch: "branch"` format.
This commit is contained in:
Issy Long 2025-08-03 22:52:05 +01:00
parent 37eaed5bb7
commit 05b27aa847
No known key found for this signature in database
2 changed files with 18 additions and 4 deletions

View File

@ -13,7 +13,7 @@ module RuboCop
# - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present # - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present
# - `stable do` should not be present without a `head` spec # - `stable do` should not be present without a `head` spec
# - `stable do` should not be present with only `url|checksum|mirror|version` # - `stable do` should not be present with only `url|checksum|mirror|version`
# - `head do` should not be present with only `url` # - `head do` should not be present with only `url|branch`
class ComponentsRedundancy < FormulaCop class ComponentsRedundancy < FormulaCop
HEAD_MSG = "`head` and `head do` should not be simultaneously present" HEAD_MSG = "`head` and `head do` should not be simultaneously present"
BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present" BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present"
@ -54,8 +54,9 @@ module RuboCop
head_block = find_block(body_node, :head) head_block = find_block(body_node, :head)
if head_block && !head_block.body.nil? if head_block && !head_block.body.nil?
child_nodes = head_block.body.begin_type? ? head_block.body.child_nodes : [head_block.body] child_nodes = head_block.body.begin_type? ? head_block.body.child_nodes : [head_block.body]
if child_nodes.all? { |n| n.send_type? && n.method_name == :url } shorthand_head_methods = [:url, :branch]
problem "`head do` should not be present with only `url`" if child_nodes.all? { |n| n.send_type? && shorthand_head_methods.include?(n.method_name) }
problem "`head do` should not be present with only #{shorthand_head_methods.join("/")}"
end end
end end

View File

@ -82,13 +82,26 @@ RSpec.describe RuboCop::Cop::FormulaAudit::ComponentsRedundancy do
end end
head do head do
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only `url` ^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch
url "https://brew.sh/foo.git" url "https://brew.sh/foo.git"
end end
end end
RUBY RUBY
end end
it "reports an offense if `head do` is present with only `url` and `branch`" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
head do
^^^^^^^ FormulaAudit/ComponentsRedundancy: `head do` should not be present with only url/branch
url "https://brew.sh/foo.git", branch: "develop"
end
end
RUBY
end
it "reports no offenses if `stable do` is present with `url` and `depends_on`" do it "reports no offenses if `stable do` is present with `url` and `depends_on`" do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
class Foo < Formula class Foo < Formula