Only take overrideable stanzas into account

- This skips over stanza names that are not overrideable in `on_*`
  blocks, with the positive side effect that `on_*` blocks themselves
  aren't in the list so we can get rid of another conditional.
- Stanzas overrideable in blocks are defined in `Cask::DSL` by each of
  the methods calling `set_unique_stanza`.
This commit is contained in:
Issy Long 2023-03-23 17:43:25 +00:00
parent d4d413db62
commit e4156909d4
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 28 additions and 2 deletions

View File

@ -9,6 +9,12 @@ module RuboCop
include CaskHelp
ON_SYSTEM_METHODS = RuboCop::Cask::Constants::ON_SYSTEM_METHODS
# These stanzas can be overridden by `on_*` blocks, so take them into account.
# TODO: Update this list if new stanzas are added to `Cask::DSL` that call `set_unique_stanza`.
OVERRIDEABLE_METHODS = [
:appcast, :arch, :auto_updates, :conflicts_with, :container,
:desc, :homepage, :sha256, :url, :version
].freeze
MESSAGE = <<~EOS
Do not use a top-level `%<stanza>s` stanza as the default. Add it to an `on_{system}` block instead.
Use `:or_older` or `:or_newer` to specify a range of macOS versions.
@ -23,8 +29,8 @@ module RuboCop
stanzas_in_blocks = on_system_stanzas(on_blocks)
cask_stanzas.each do |stanza|
# Skip if the stanza is itself an `on_*` block.
next if ON_SYSTEM_METHODS.include?(stanza.stanza_name)
# Skip if the stanza is not allowed to be overridden.
next unless OVERRIDEABLE_METHODS.include?(stanza.stanza_name)
# Skip if the stanza outside of a block is not also in an `on_*` block.
next unless stanzas_in_blocks.include?(stanza.stanza_name)

View File

@ -38,6 +38,26 @@ describe RuboCop::Cop::Cask::NoOverrides do
include_examples "does not report any offenses"
end
context "when there are top-level stanzas also in `on_*` blocks that should not override" do
let(:source) do
<<~CASK
cask 'foo' do
version '1.2.3'
on_arm do
binary "foo-\#{version}-arm64"
end
app "foo-\#{version}.app"
binary "foo-\#{version}"
end
CASK
end
include_examples "does not report any offenses"
end
context "when there are `arch` variables in the `url` in the `on_*` blocks" do
let(:source) do
<<~CASK