| 
									
										
										
										
											2023-02-14 19:19:37 -08:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | module RuboCop | 
					
						
							|  |  |  |   module AST | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |     # Extensions for RuboCop's AST Node class. | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |     class Node | 
					
						
							|  |  |  |       include RuboCop::Cask::Constants | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def_node_matcher :method_node, "{$(send ...) (block $(send ...) ...)}" | 
					
						
							|  |  |  |       def_node_matcher :block_body,  "(block _ _ $_)" | 
					
						
							| 
									
										
										
										
											2023-04-22 04:00:04 +02:00
										 |  |  |       def_node_matcher :cask_block?, "(block (send nil? :cask ...) args ...)" | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |       def_node_matcher :on_system_block?, | 
					
						
							|  |  |  |                        "(block (send nil? {#{ON_SYSTEM_METHODS.map(&:inspect).join(" ")}} ...) args ...)" | 
					
						
							| 
									
										
										
										
											2022-08-05 15:35:25 -04:00
										 |  |  |       def_node_matcher :arch_variable?, "(lvasgn _ (send nil? :on_arch_conditional ...))" | 
					
						
							| 
									
										
										
										
											2023-03-21 14:43:39 +01:00
										 |  |  |       def_node_matcher :begin_block?, "(begin ...)" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 08:23:19 +02:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							|  |  |  |       def cask_on_system_block? | 
					
						
							|  |  |  |         (on_system_block? && each_ancestor.any?(&:cask_block?)) || false | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |       def stanza? | 
					
						
							| 
									
										
										
										
											2022-08-05 15:35:25 -04:00
										 |  |  |         return true if arch_variable? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-14 19:19:37 -08:00
										 |  |  |         case self | 
					
						
							|  |  |  |         when RuboCop::AST::BlockNode, RuboCop::AST::SendNode | 
					
						
							|  |  |  |           ON_SYSTEM_METHODS.include?(method_name) || STANZA_ORDER.include?(method_name) | 
					
						
							|  |  |  |         else false | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def heredoc? | 
					
						
							|  |  |  |         loc.is_a?(Parser::Source::Map::Heredoc) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-05 15:35:25 -04:00
										 |  |  |       def location_expression | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |         base_expression = loc.expression | 
					
						
							|  |  |  |         descendants.select(&:heredoc?).reduce(base_expression) do |expr, node| | 
					
						
							|  |  |  |           expr.join(node.loc.heredoc_end) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |