| 
									
										
										
										
											2024-02-19 22:47:31 +00:00
										 |  |  | # typed: true | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/extend/formula_cop" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |       # This cop audits Python formulae that include certain resources | 
					
						
							| 
									
										
										
										
											2024-02-19 22:47:31 +00:00
										 |  |  |       # to ensure that they also have the correct `uses_from_macos` | 
					
						
							|  |  |  |       # dependencies. | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @api private | 
					
						
							|  |  |  |       class ResourceRequiresDependencies < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           return if body_node.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           resource_nodes = find_every_method_call_by_name(body_node, :resource) | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |           return if resource_nodes.empty? | 
					
						
							| 
									
										
										
										
											2024-02-19 22:47:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |           %w[lxml pyyaml].each do |resource_name| | 
					
						
							| 
									
										
										
										
											2024-02-20 23:48:58 +00:00
										 |  |  |             found = resource_nodes.find { |node| node.arguments&.first&.str_content == resource_name } | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |             next unless found | 
					
						
							| 
									
										
										
										
											2024-02-19 22:47:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |             uses_from_macos_nodes = find_every_method_call_by_name(body_node, :uses_from_macos) | 
					
						
							|  |  |  |             uses_from_macos = uses_from_macos_nodes.map { |node| node.arguments.first.str_content } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             depends_on_nodes = find_every_method_call_by_name(body_node, :depends_on) | 
					
						
							|  |  |  |             depends_on = depends_on_nodes.map { |node| node.arguments.first.str_content } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             required_deps = case resource_name | 
					
						
							|  |  |  |             when "lxml" | 
					
						
							|  |  |  |               kind = "uses_from_macos" | 
					
						
							|  |  |  |               ["libxml2", "libxslt"] | 
					
						
							|  |  |  |             when "pyyaml" | 
					
						
							|  |  |  |               kind = "depends_on" | 
					
						
							|  |  |  |               ["libyaml"] | 
					
						
							| 
									
										
										
										
											2024-02-20 23:36:52 +00:00
										 |  |  |             else | 
					
						
							|  |  |  |               [] | 
					
						
							| 
									
										
										
										
											2024-02-20 22:09:47 +00:00
										 |  |  |             end | 
					
						
							|  |  |  |             next if required_deps.all? { |dep| uses_from_macos.include?(dep) || depends_on.include?(dep) } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             offending_node(found) | 
					
						
							|  |  |  |             problem "Add `#{kind}` lines above for #{required_deps.map { |req| "`\"#{req}\"`" }.join(" and ")}." | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-02-19 22:47:31 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |