Class: RuboCop::Cop::Cask::NoOverrides Private
- Includes:
- CaskHelp
- Defined in:
- rubocops/cask/no_overrides.rb
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Constant Summary collapse
- OVERRIDABLE_METHODS =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
TODO:Update this list when stanzas in
Cask::DSLstart or stop callingset_unique_stanza.These stanzas can be overridden by
on_*blocks, so take them into account. [ :appcast, :arch, :auto_updates, :container, :desc, :homepage, :os, :sha256, :url, :version ].freeze
Instance Method Summary collapse
- #inside_livecheck_defined?(node) ⇒ Boolean private
- #macos_dependency_pair(send_node) ⇒ RuboCop::AST::PairNode? private
- #multi_stanza_livecheck_defined?(node) ⇒ Boolean private
- #on_cask(cask_block) ⇒ void private
- #on_system_stanzas(on_system) ⇒ Set<Symbol> private
- #single_stanza_livecheck_defined?(node) ⇒ Boolean private
Methods included from CaskHelp
#cask_tap, #inner_stanzas, #on_block, #on_cask_stanza_block, #on_system_methods
Instance Method Details
#inside_livecheck_defined?(node) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
99 100 101 |
# File 'rubocops/cask/no_overrides.rb', line 99 def inside_livecheck_defined?(node) single_stanza_livecheck_defined?(node) || multi_stanza_livecheck_defined?(node) end |
#macos_dependency_pair(send_node) ⇒ RuboCop::AST::PairNode?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
91 92 93 94 95 96 |
# File 'rubocops/cask/no_overrides.rb', line 91 def macos_dependency_pair(send_node) return if send_node.method_name != :depends_on return unless (hash = send_node.first_argument)&.hash_type? hash.pairs.find { |pair| pair.key.sym_type? && pair.key.value == :macos } end |
#multi_stanza_livecheck_defined?(node) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
109 110 111 112 |
# File 'rubocops/cask/no_overrides.rb', line 109 def multi_stanza_livecheck_defined?(node) grandparent_node = node.parent.parent node.parent.begin_type? && grandparent_node.block_type? && grandparent_node.method_name == :livecheck end |
#on_cask(cask_block) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'rubocops/cask/no_overrides.rb', line 18 def on_cask(cask_block) = "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." cask_stanzas = cask_block.toplevel_stanzas return if (on_blocks = on_system_methods(cask_stanzas)).none? stanzas_in_blocks = on_system_stanzas(on_blocks) cask_stanzas.each do |stanza| # Skip if the stanza is not allowed to be overridden. next unless OVERRIDABLE_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) add_offense(stanza.source_range, message: format(, stanza: stanza.stanza_name)) end end |
#on_system_stanzas(on_system) ⇒ Set<Symbol>
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'rubocops/cask/no_overrides.rb', line 39 def on_system_stanzas(on_system) = "Do not use a `depends_on macos:` stanza inside an `on_{system}` block. " \ "Add it once to specify the oldest macOS supported by any version in the cask." names = T.let(Set.new, T::Set[Symbol]) method_nodes = on_system.map(&:method_node) # Check if multiple `on_{system}` blocks have different `depends_on macos:` versions. # If so, this indicates architecture-specific requirements and is allowed. macos_versions = T.let([], T::Array[String]) method_nodes.select(&:block_type?).each do |node| node.child_nodes.each do |child| child.each_node(:send) do |send_node| next unless (macos_pair = macos_dependency_pair(send_node)) macos_versions << macos_pair.value.source end end end # Allow if there are multiple different macOS versions specified allow_macos_depends_in_blocks = macos_versions.size > 1 && macos_versions.uniq.size > 1 method_nodes.select(&:block_type?).each do |node| node.child_nodes.each do |child| child.each_node(:send) do |send_node| # Skip (nested) `livecheck` block as its `url` is different # from a download `url`. next if send_node.method_name == :livecheck || inside_livecheck_defined?(send_node) # Skip string interpolations. if send_node.ancestors.drop_while { |a| !a.begin_type? }.any? { |a| a.dstr_type? || a.regexp_type? } next end next if RuboCop::Cask::Constants::ON_SYSTEM_METHODS.include?(send_node.method_name) macos_pair = macos_dependency_pair(send_node) # `depends_on macos: :any` declares no minimum version to hoist. if macos_pair && !(macos_pair.value.sym_type? && macos_pair.value.value == :any) && OnSystemConditionalsHelper::ON_SYSTEM_OPTIONS.map do |m| :"on_#{m}" end.include?(T.cast(node, RuboCop::AST::BlockNode).method_name) && T.cast(node, RuboCop::AST::BlockNode).method_name != :on_macos && !allow_macos_depends_in_blocks # Allow `depends_on macos:` in multiple `on_{system}` blocks for architecture-specific requirements add_offense(send_node.source_range, message:) end names.add(send_node.method_name) end end end names end |
#single_stanza_livecheck_defined?(node) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
104 105 106 |
# File 'rubocops/cask/no_overrides.rb', line 104 def single_stanza_livecheck_defined?(node) node.parent.block_type? && node.parent.method_name == :livecheck end |