| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | require "forwardable" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module Cask | 
					
						
							| 
									
										
										
										
											2023-04-15 22:55:12 +01:00
										 |  |  |       # This cop checks that a cask's stanzas are ordered correctly, including nested within `on_*` blocks. | 
					
						
							| 
									
										
										
										
											2021-06-23 13:23:18 -07:00
										 |  |  |       # @see https://docs.brew.sh/Cask-Cookbook#stanza-order | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |       class StanzaOrder < Base | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |         include IgnoredNode | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |         extend AutoCorrector | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |         include CaskHelp | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 08:50:45 +02:00
										 |  |  |         MESSAGE = "`%<stanza>s` stanza out of order" | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |         def on_cask_stanza_block(stanza_block) | 
					
						
							|  |  |  |           stanzas = stanza_block.stanzas | 
					
						
							|  |  |  |           ordered_stanzas = sort_stanzas(stanzas) | 
					
						
							| 
									
										
										
										
											2023-04-13 19:10:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |           return if stanzas == ordered_stanzas | 
					
						
							| 
									
										
										
										
											2023-04-21 00:30:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |           stanzas.zip(ordered_stanzas).each do |stanza_before, stanza_after| | 
					
						
							|  |  |  |             next if stanza_before == stanza_after | 
					
						
							| 
									
										
										
										
											2023-04-20 15:41:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |             add_offense( | 
					
						
							|  |  |  |               stanza_before.method_node, | 
					
						
							|  |  |  |               message: format(MESSAGE, stanza: stanza_before.stanza_name), | 
					
						
							|  |  |  |             ) do |corrector| | 
					
						
							|  |  |  |               next if part_of_ignored_node?(stanza_before.method_node) | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |               corrector.replace( | 
					
						
							|  |  |  |                 stanza_before.source_range_with_comments, | 
					
						
							|  |  |  |                 stanza_after.source_with_comments, | 
					
						
							|  |  |  |               ) | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |               # Ignore node so that nested content is not auto-corrected and clobbered. | 
					
						
							|  |  |  |               ignore_node(stanza_before.method_node) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-05-12 08:32:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 10:04:28 +02:00
										 |  |  |         def on_new_investigation | 
					
						
							|  |  |  |           super | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ignored_nodes.clear | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |         private | 
					
						
							| 
									
										
										
										
											2023-04-20 15:41:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |         def sort_stanzas(stanzas) | 
					
						
							|  |  |  |           stanzas.sort do |stanza1, stanza2| | 
					
						
							|  |  |  |             i1 = stanza1.stanza_index | 
					
						
							|  |  |  |             i2 = stanza2.stanza_index | 
					
						
							| 
									
										
										
										
											2023-04-21 00:30:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |             if i1 == i2 | 
					
						
							|  |  |  |               i1 = stanzas.index(stanza1) | 
					
						
							|  |  |  |               i2 = stanzas.index(stanza2) | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             i1 - i2 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |         def stanza_order_index(stanza) | 
					
						
							|  |  |  |           stanza_name = stanza.respond_to?(:method_name) ? stanza.method_name : stanza.stanza_name | 
					
						
							| 
									
										
										
										
											2023-04-21 00:30:45 +01:00
										 |  |  |           RuboCop::Cask::Constants::STANZA_ORDER.index(stanza_name) | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |