style: forbid url do blocks in homebrew/cask

This commit is contained in:
Rylan Polster 2024-09-24 22:45:00 -04:00
parent 4deb9dff97
commit 18147a6a1e
No known key found for this signature in database
3 changed files with 54 additions and 1 deletions

View File

@ -26,6 +26,8 @@ module RuboCop
return unless block_node.cask_block? return unless block_node.cask_block?
@file_path = processed_source.file_path
cask_block = RuboCop::Cask::AST::CaskBlock.new(block_node, comments) cask_block = RuboCop::Cask::AST::CaskBlock.new(block_node, comments)
on_cask(cask_block) on_cask(cask_block)
end end
@ -39,6 +41,13 @@ module RuboCop
inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?) inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?)
inner_nodes.map { |n| RuboCop::Cask::AST::Stanza.new(n, comments) } inner_nodes.map { |n| RuboCop::Cask::AST::Stanza.new(n, comments) }
end 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 end
end end

View File

@ -26,7 +26,12 @@ module RuboCop
include UrlHelper include UrlHelper
def on_url_stanza(stanza) 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 url_stanza = stanza.stanza_node.first_argument
hash_node = stanza.stanza_node.last_argument hash_node = stanza.stanza_node.last_argument

View File

@ -3,6 +3,45 @@
require "rubocops/rubocop-cask" require "rubocops/rubocop-cask"
RSpec.describe RuboCop::Cop::Cask::Url, :config do 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 it "accepts a `verified` value that does not start with a protocol" do
expect_no_offenses <<~CASK expect_no_offenses <<~CASK
cask "foo" do cask "foo" do