Fix skipping of livecheck block contents in on_* blocks

- This passes the previously failing test for `on_*` blocks with
  `livecheck` blocks with multiple stanzas inside them (eg `url` and
  `strategy`) that weren't being correctly skipped because we weren't
  detecting high enough up the ancestry.
This commit is contained in:
Issy Long 2023-03-22 23:12:06 +00:00
parent 991e5f2f9c
commit e66226aefc
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 40 additions and 4 deletions

View File

@ -41,9 +41,7 @@ module RuboCop
node.child_nodes.each do |child|
child.each_node(:send) do |send_node|
# Skip (nested) livecheck blocks as its `url` is different to a download `url`.
next if send_node.method_name == :livecheck
next if send_node.parent.block_type? && send_node.parent.method_name == :livecheck
next if send_node.method_name == :livecheck || inside_livecheck_block?(send_node)
# Skip string interpolations (`:send` inside `:begin` inside `:dstr`).
next if send_node.parent.begin_type? && send_node.parent.parent.dstr_type?
next if ON_SYSTEM_METHODS.include?(send_node.method_name)
@ -54,6 +52,19 @@ module RuboCop
end
names
end
def inside_livecheck_block?(node)
single_stanza_livecheck_block?(node) || multi_stanza_livecheck_block?(node)
end
def single_stanza_livecheck_block?(node)
node.parent.block_type? && node.parent.method_name == :livecheck
end
def multi_stanza_livecheck_block?(node)
grandparent_node = node.parent.parent
node.parent.begin_type? && grandparent_node.block_type? && grandparent_node.method_name == :livecheck
end
end
end
end

View File

@ -46,6 +46,7 @@ describe RuboCop::Cop::Cask::NoOverrides do
version '1.2.3'
on_mojave :or_later do
url "https://brew.sh/foo-\#{version}-\#{arch}.pkg"
sha256 "aaa"
end
end
CASK
@ -54,7 +55,30 @@ describe RuboCop::Cop::Cask::NoOverrides do
include_examples "does not report any offenses"
end
context "when there are livecheck blocks within `on_*` blocks, ignore their contents" do
context "when there are single-line livecheck blocks within `on_*` blocks, ignore their contents" do
let(:source) do
<<~CASK
cask 'foo' do
on_intel do
livecheck do
url 'https://brew.sh/foo' # Livecheck should be allowed since it's a different "kind" of URL.
end
version '1.2.3'
end
on_arm do
version '2.3.4'
end
url 'https://brew.sh/foo.pkg'
sha256 "bbb"
end
CASK
end
include_examples "does not report any offenses"
end
context "when there are multi-line livecheck blocks within `on_*` blocks, ignore their contents" do
let(:source) do
<<~CASK
cask 'foo' do
@ -70,6 +94,7 @@ describe RuboCop::Cop::Cask::NoOverrides do
end
url 'https://brew.sh/foo.pkg'
sha256 "bbb"
end
CASK
end