| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2023-03-19 17:31:32 +00:00
										 |  |  | # 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`. | 
					
						
							| 
									
										
										
										
											2024-12-28 15:52:04 -05:00
										 |  |  |         OVERRIDABLE_METHODS = [ | 
					
						
							| 
									
										
										
										
											2023-03-23 17:43:25 +00:00
										 |  |  |           :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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |         sig { override.params(cask_block: RuboCop::Cask::AST::CaskBlock).void } | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2024-12-28 15:52:04 -05:00
										 |  |  |             next unless OVERRIDABLE_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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |         sig { params(on_system: T::Array[RuboCop::Cask::AST::Stanza]).returns(T::Set[Symbol]) } | 
					
						
							| 
									
										
										
										
											2023-03-19 22:40:53 +00:00
										 |  |  |         def on_system_stanzas(on_system) | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |           names = T.let(Set.new, T::Set[Symbol]) | 
					
						
							| 
									
										
										
										
											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| | 
					
						
							| 
									
										
										
										
											2024-12-02 10:06:14 -05:00
										 |  |  |                 # Skip (nested) `livecheck` block as its `url` is different | 
					
						
							|  |  |  |                 # from a download `url`. | 
					
						
							| 
									
										
											  
											
												livecheck: clarify livecheckable language
Formulae, casks, and resources have a `#livecheckable?` method that
indicates whether they contain a `livecheck` block. This is intended
to be read as "has a livecheckable?", not "is livecheckable?" (as
livecheck can find versions for some packages/resources without a
`livecheck` block). Unfortunately, correct understanding of this
method's behavior [outside of documentation] relies on historical
knowledge that few people possess, so this is often confusing to
anyone who hasn't been working on livecheck since 2020.
In the olden days, a "livecheckable" was a Ruby file containing a
`livecheck` block (originally a hash) with a filename that
corresponded to a related formula. The `livecheck` blocks in
livecheckable files were integrated into their respective formulae in
August 2020, so [first-party] livecheckables ceased to exist at that
time. From that point forward, we simply referred to these as
`livecheck` blocks.
With that in mind, this clarifies the situation by replacing
"livecheckable" language. This includes renaming `#livecheckable?` to
`#livecheck_defined?`, replacing usage of "livecheckable" as a noun
with "`livecheck` block", replacing "livecheckable" as a boolean with
"livecheck_defined", and replacing incorrect usage of "livecheckable"
as an adjective with "checkable".
											
										 
											2024-11-27 18:20:56 -05:00
										 |  |  |                 next if send_node.method_name == :livecheck || inside_livecheck_defined?(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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |         sig { params(node: RuboCop::AST::Node).returns(T::Boolean) } | 
					
						
							| 
									
										
											  
											
												livecheck: clarify livecheckable language
Formulae, casks, and resources have a `#livecheckable?` method that
indicates whether they contain a `livecheck` block. This is intended
to be read as "has a livecheckable?", not "is livecheckable?" (as
livecheck can find versions for some packages/resources without a
`livecheck` block). Unfortunately, correct understanding of this
method's behavior [outside of documentation] relies on historical
knowledge that few people possess, so this is often confusing to
anyone who hasn't been working on livecheck since 2020.
In the olden days, a "livecheckable" was a Ruby file containing a
`livecheck` block (originally a hash) with a filename that
corresponded to a related formula. The `livecheck` blocks in
livecheckable files were integrated into their respective formulae in
August 2020, so [first-party] livecheckables ceased to exist at that
time. From that point forward, we simply referred to these as
`livecheck` blocks.
With that in mind, this clarifies the situation by replacing
"livecheckable" language. This includes renaming `#livecheckable?` to
`#livecheck_defined?`, replacing usage of "livecheckable" as a noun
with "`livecheck` block", replacing "livecheckable" as a boolean with
"livecheck_defined", and replacing incorrect usage of "livecheckable"
as an adjective with "checkable".
											
										 
											2024-11-27 18:20:56 -05:00
										 |  |  |         def inside_livecheck_defined?(node) | 
					
						
							|  |  |  |           single_stanza_livecheck_defined?(node) || multi_stanza_livecheck_defined?(node) | 
					
						
							| 
									
										
										
										
											2023-03-22 23:12:06 +00:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |         sig { params(node: RuboCop::AST::Node).returns(T::Boolean) } | 
					
						
							| 
									
										
											  
											
												livecheck: clarify livecheckable language
Formulae, casks, and resources have a `#livecheckable?` method that
indicates whether they contain a `livecheck` block. This is intended
to be read as "has a livecheckable?", not "is livecheckable?" (as
livecheck can find versions for some packages/resources without a
`livecheck` block). Unfortunately, correct understanding of this
method's behavior [outside of documentation] relies on historical
knowledge that few people possess, so this is often confusing to
anyone who hasn't been working on livecheck since 2020.
In the olden days, a "livecheckable" was a Ruby file containing a
`livecheck` block (originally a hash) with a filename that
corresponded to a related formula. The `livecheck` blocks in
livecheckable files were integrated into their respective formulae in
August 2020, so [first-party] livecheckables ceased to exist at that
time. From that point forward, we simply referred to these as
`livecheck` blocks.
With that in mind, this clarifies the situation by replacing
"livecheckable" language. This includes renaming `#livecheckable?` to
`#livecheck_defined?`, replacing usage of "livecheckable" as a noun
with "`livecheck` block", replacing "livecheckable" as a boolean with
"livecheck_defined", and replacing incorrect usage of "livecheckable"
as an adjective with "checkable".
											
										 
											2024-11-27 18:20:56 -05:00
										 |  |  |         def single_stanza_livecheck_defined?(node) | 
					
						
							| 
									
										
										
										
											2023-03-22 23:12:06 +00:00
										 |  |  |           node.parent.block_type? && node.parent.method_name == :livecheck | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-05 23:45:23 +00:00
										 |  |  |         sig { params(node: RuboCop::AST::Node).returns(T::Boolean) } | 
					
						
							| 
									
										
											  
											
												livecheck: clarify livecheckable language
Formulae, casks, and resources have a `#livecheckable?` method that
indicates whether they contain a `livecheck` block. This is intended
to be read as "has a livecheckable?", not "is livecheckable?" (as
livecheck can find versions for some packages/resources without a
`livecheck` block). Unfortunately, correct understanding of this
method's behavior [outside of documentation] relies on historical
knowledge that few people possess, so this is often confusing to
anyone who hasn't been working on livecheck since 2020.
In the olden days, a "livecheckable" was a Ruby file containing a
`livecheck` block (originally a hash) with a filename that
corresponded to a related formula. The `livecheck` blocks in
livecheckable files were integrated into their respective formulae in
August 2020, so [first-party] livecheckables ceased to exist at that
time. From that point forward, we simply referred to these as
`livecheck` blocks.
With that in mind, this clarifies the situation by replacing
"livecheckable" language. This includes renaming `#livecheckable?` to
`#livecheck_defined?`, replacing usage of "livecheckable" as a noun
with "`livecheck` block", replacing "livecheckable" as a boolean with
"livecheck_defined", and replacing incorrect usage of "livecheckable"
as an adjective with "checkable".
											
										 
											2024-11-27 18:20:56 -05:00
										 |  |  |         def multi_stanza_livecheck_defined?(node) | 
					
						
							| 
									
										
										
										
											2023-03-22 23:12:06 +00:00
										 |  |  |           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 |