Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: forbid url do blocks in homebrew/cask #18404

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/cask_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def on_block(block_node)

return unless block_node.cask_block?

@file_path = processed_source.file_path

cask_block = RuboCop::Cask::AST::CaskBlock.new(block_node, comments)
on_cask(cask_block)
end
Expand All @@ -39,6 +41,13 @@ def inner_stanzas(block_node, comments)
inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?)
inner_nodes.map { |n| RuboCop::Cask::AST::Stanza.new(n, comments) }
end

sig { returns(T.nilable(String)) }
def cask_tap
return unless (match_obj = @file_path.match(%r{/(homebrew-\w+)/}))

match_obj[1]
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion Library/Homebrew/rubocops/cask/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class Url < Base
include UrlHelper

def on_url_stanza(stanza)
return if stanza.stanza_node.block_type?
if stanza.stanza_node.block_type?
if cask_tap == "homebrew-cask"
add_offense(stanza.stanza_node, message: 'Do not use `url "..." do` blocks in Homebrew/homebrew-cask.')
end
return
end

url_stanza = stanza.stanza_node.first_argument
hash_node = stanza.stanza_node.last_argument
Expand Down
39 changes: 39 additions & 0 deletions Library/Homebrew/test/rubocops/cask/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
require "rubocops/rubocop-cask"

RSpec.describe RuboCop::Cop::Cask::Url, :config do
it "allows regular `url` blocks in homebrew-cask" do
expect_no_offenses <<~CASK, "/homebrew-cask/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end

it "does not allow `url do` blocks in homebrew-cask" do
expect_offense <<~CASK, "/homebrew-cask/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg" do |url|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `url "..." do` blocks in Homebrew/homebrew-cask.
url
end
end
CASK
end

it "allows regular `url` blocks in a non-homebrew-cask tap" do
expect_no_offenses <<~CASK, "/homebrew-tap/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end

it "allows `url do` blocks in a non-homebrew-cask tap" do
expect_no_offenses <<~CASK, "/homebrew-tap/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg" do |url|
url
end
end
CASK
end

it "accepts a `verified` value that does not start with a protocol" do
expect_no_offenses <<~CASK
cask "foo" do
Expand Down