| 
									
										
										
										
											2023-03-19 17:31:32 +00:00
										 |  |  | # typed: true | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module Cask | 
					
						
							|  |  |  |       class NoOverrides < Base | 
					
						
							|  |  |  |         include CaskHelp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-23 17:43:25 +00:00
										 |  |  |         # 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 | 
					
						
							| 
									
										
										
										
											2023-05-08 11:02:44 +02:00
										 |  |  |         MESSAGE = "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." | 
					
						
							| 
									
										
										
										
											2023-03-19 17:31:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def on_cask(cask_block) | 
					
						
							| 
									
										
										
										
											2023-03-20 23:49:03 +00:00
										 |  |  |           cask_stanzas = cask_block.toplevel_stanzas | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:34:13 +02:00
										 |  |  |           return if (on_blocks = on_system_methods(cask_stanzas)).none? | 
					
						
							| 
									
										
										
										
											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-23 17:43:25 +00:00
										 |  |  |             # Skip if the stanza is not allowed to be overridden. | 
					
						
							|  |  |  |             next unless OVERRIDEABLE_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) | 
					
						
							| 
									
										
										
										
											2023-04-15 22:55:12 +01:00
										 |  |  |           method_nodes.select(&:block_type?).each do |node| | 
					
						
							| 
									
										
										
										
											2023-03-19 22:40:53 +00:00
										 |  |  |             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-23 13:16:10 +00:00
										 |  |  |                 # Skip string interpolations. | 
					
						
							| 
									
										
										
										
											2023-03-23 13:54:55 +00:00
										 |  |  |                 if send_node.ancestors.drop_while { |a| !a.begin_type? }.any? { |a| a.dstr_type? || a.regexp_type? } | 
					
						
							|  |  |  |                   next | 
					
						
							|  |  |  |                 end | 
					
						
							| 
									
										
										
										
											2023-04-15 22:55:12 +01:00
										 |  |  |                 next if RuboCop::Cask::Constants::ON_SYSTEM_METHODS.include?(send_node.method_name) | 
					
						
							| 
									
										
										
										
											2023-03-19 22:40:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 |