2023-03-19 17:31:32 +00:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Cask
|
|
|
|
class NoOverrides < Base
|
|
|
|
extend T::Sig
|
|
|
|
include CaskHelp
|
|
|
|
|
2023-03-19 20:41:43 +00:00
|
|
|
ON_SYSTEM_METHODS = RuboCop::Cask::Constants::ON_SYSTEM_METHODS
|
2023-03-19 17:31:32 +00:00
|
|
|
MESSAGE = <<~EOS
|
2023-03-20 00:32:19 +00:00
|
|
|
Do not use a top-level `%<stanza>s` stanza as the default. Add it to an `on_{system}` block instead.
|
2023-03-19 17:31:32 +00:00
|
|
|
Use `:or_older` or `:or_newer` to specify a range of macOS versions.
|
|
|
|
EOS
|
|
|
|
|
|
|
|
def on_cask(cask_block)
|
2023-03-20 23:49:03 +00:00
|
|
|
cask_stanzas = cask_block.toplevel_stanzas
|
|
|
|
|
2023-03-19 20:41:43 +00:00
|
|
|
# Skip if there are no `on_*` blocks.
|
2023-03-19 22:40:53 +00:00
|
|
|
return unless (on_blocks = cask_stanzas.select { |s| ON_SYSTEM_METHODS.include?(s.stanza_name) }).any?
|
2023-03-19 17:31:32 +00:00
|
|
|
|
2023-03-21 00:12:28 +00:00
|
|
|
stanzas_in_blocks = on_system_stanzas(on_blocks)
|
|
|
|
|
2023-03-19 20:41:43 +00:00
|
|
|
cask_stanzas.each do |stanza|
|
2023-03-19 22:40:53 +00:00
|
|
|
# Skip if the stanza is itself an `on_*` block.
|
2023-03-20 00:30:15 +00:00
|
|
|
next if ON_SYSTEM_METHODS.include?(stanza.stanza_name)
|
2023-03-19 22:40:53 +00:00
|
|
|
# Skip if the stanza outside of a block is not also in an `on_*` block.
|
2023-03-21 00:12:28 +00:00
|
|
|
next unless stanzas_in_blocks.include?(stanza.stanza_name)
|
2023-03-19 17:31:32 +00:00
|
|
|
|
|
|
|
add_offense(stanza.source_range, message: format(MESSAGE, stanza: stanza.stanza_name))
|
|
|
|
end
|
|
|
|
end
|
2023-03-19 22:40:53 +00:00
|
|
|
|
|
|
|
def on_system_stanzas(on_system)
|
2023-03-20 23:53:20 +00:00
|
|
|
names = Set.new
|
2023-03-19 22:40:53 +00:00
|
|
|
method_nodes = on_system.map(&:method_node)
|
|
|
|
method_nodes.each do |node|
|
|
|
|
next unless node.block_type?
|
|
|
|
|
|
|
|
node.child_nodes.each do |child|
|
|
|
|
child.each_node(:send) do |send_node|
|
2023-03-21 23:49:54 +00:00
|
|
|
# Skip (nested) livecheck blocks as its `url` is different to a download `url`.
|
2023-03-22 23:12:06 +00:00
|
|
|
next if send_node.method_name == :livecheck || inside_livecheck_block?(send_node)
|
2023-03-22 19:13:47 +00:00
|
|
|
# Skip string interpolations (`:send` inside `:begin` inside `:dstr`).
|
|
|
|
next if send_node.parent.begin_type? && send_node.parent.parent.dstr_type?
|
2023-03-19 22:40:53 +00:00
|
|
|
next if ON_SYSTEM_METHODS.include?(send_node.method_name)
|
|
|
|
|
2023-03-20 23:53:20 +00:00
|
|
|
names.add(send_node.method_name)
|
2023-03-19 22:40:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
names
|
|
|
|
end
|
2023-03-22 23:12:06 +00:00
|
|
|
|
|
|
|
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
|
2023-03-19 17:31:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|