Replace # No zap stanza required comments

- `zap :not_required` declares that a cask has nothing to zap in a way
  that can be audited, unlike the comment that ~2,900 casks use.
- Add `Cask/ZapNotRequired` to autocorrect the comment so taps can
  migrate with `brew style --fix`.
- Leave comments that give a reason, as autocorrecting would lose it,
  and comments after code, as the stanza can't go on that line.
This commit is contained in:
Bevan Kay committed 2026-10-27 23:36:11 +11:00
1 parent 20e162d4e8
commit 38514bf787
3 files changed
+80

No files matched your search

@@ -0,0 +1,36 @@
# typed: strict
# frozen_string_literal: true
module RuboCop
module Cop
module Cask
# This cop replaces the `# No zap stanza required` comment with the
# auditable `zap :not_required` stanza.
#
# Example
# # bad
# # No zap stanza required
#
# # good
# zap :not_required
class ZapNotRequired < Base
include CaskHelp
extend AutoCorrector
MSG = "Use `zap :not_required` instead of a `# No zap stanza required` comment."
sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void }
def on_cask(cask_block)
cask_block.comments.each do |comment|
next unless comment.text.match?(/\A#\s*no zap stanza required\.?\z/i)
next unless comment_line?(processed_source[comment.loc.line - 1])
add_offense(comment) do |corrector|
corrector.replace(comment, "zap :not_required")
end
end
end
end
end
end
end
@@ -34,4 +34,5 @@ require_relative "cask/uninstall_wildcard"
require_relative "cask/url"
require_relative "cask/url_legacy_comma_separators"
require_relative "cask/variables"
require_relative "cask/zap_not_required"
require_relative "cask/deprecate_disable_unsigned_reason"
@@ -0,0 +1,43 @@
# typed: strict
# frozen_string_literal: true
require "rubocops/rubocop-cask"
RSpec.describe RuboCop::Cop::Cask::ZapNotRequired, :config do
it "flags and autocorrects a `# No zap stanza required` comment" do
expect_offense <<~CASK
cask "foo" do
app "Foo.app"
# No zap stanza required
^^^^^^^^^^^^^^^^^^^^^^^^ Use `zap :not_required` instead of a `# No zap stanza required` comment.
end
CASK
expect_correction <<~CASK
cask "foo" do
app "Foo.app"
zap :not_required
end
CASK
end
it "ignores comments that explain more" do
expect_no_offenses <<~CASK
cask "foo" do
app "Foo.app"
# No zap stanza required as the app stores no user data
end
CASK
end
it "ignores comments that follow code" do
expect_no_offenses <<~CASK
cask "foo" do
app "Foo.app" # No zap stanza required
end
CASK
end
end