| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-26 19:41:14 +01:00
										 |  |  | require "rubocops/extend/formula" | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							| 
									
										
										
										
											2018-04-25 07:58:01 +10:00
										 |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2020-08-26 02:24:22 +02:00
										 |  |  |       # This cop checks if redundant components are present and for other component errors. | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  |       # | 
					
						
							|  |  |  |       # - `url|checksum|mirror` should be inside `stable` block | 
					
						
							|  |  |  |       # - `head` and `head do` should not be simultaneously present | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |       # - `bottle :unneeded`/`:disable` and `bottle do` should not be simultaneously present | 
					
						
							| 
									
										
										
										
											2020-09-03 10:34:22 +01:00
										 |  |  |       # - `stable do` should not be present without a `head` spec | 
					
						
							| 
									
										
										
										
											2020-08-26 02:24:22 +02:00
										 |  |  |       # | 
					
						
							|  |  |  |       # @api private | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  |       class ComponentsRedundancy < FormulaCop | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  |         HEAD_MSG = "`head` and `head do` should not be simultaneously present" | 
					
						
							|  |  |  |         BOTTLE_MSG = "`bottle :modifier` and `bottle do` should not be simultaneously present" | 
					
						
							| 
									
										
										
										
											2020-09-03 10:34:22 +01:00
										 |  |  |         STABLE_MSG = "`stable do` should not be present without a `head` spec" | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							| 
									
										
										
										
											2020-08-24 07:39:07 -04:00
										 |  |  |           urls = find_method_calls_by_name(body_node, :url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           urls.each do |url| | 
					
						
							|  |  |  |             url.arguments.each do |arg| | 
					
						
							|  |  |  |               next if arg.class != RuboCop::AST::HashNode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               url_args = arg.keys.each.map(&:value) | 
					
						
							|  |  |  |               if method_called?(body_node, :sha256) && url_args.include?(:tag) && url_args.include?(:revision) | 
					
						
							|  |  |  |                 problem "Do not use both sha256 and tag/revision." | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |           stable_block = find_block(body_node, :stable) | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  |           if stable_block | 
					
						
							|  |  |  |             [:url, :sha256, :mirror].each do |method_name| | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |               problem "`#{method_name}` should be put inside `stable` block" if method_called?(body_node, method_name) | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |           problem HEAD_MSG if method_called?(body_node, :head) && | 
					
						
							|  |  |  |                               find_block(body_node, :head) | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |           problem BOTTLE_MSG if method_called?(body_node, :bottle) && | 
					
						
							|  |  |  |                                 find_block(body_node, :bottle) | 
					
						
							| 
									
										
										
										
											2018-07-03 11:46:39 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |           return if method_called?(body_node, :head) || | 
					
						
							| 
									
										
										
										
											2020-09-03 10:34:22 +01:00
										 |  |  |                     find_block(body_node, :head) | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-03 11:46:39 +10:00
										 |  |  |           problem STABLE_MSG if stable_block | 
					
						
							| 
									
										
										
										
											2017-04-08 15:10:44 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |