| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2018-09-02 23:30:07 +02:00
										 |  |  | require "extend/string" | 
					
						
							| 
									
										
										
										
											2017-05-24 13:04:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |       # This cop audits versioned formulae for `conflicts_with`. | 
					
						
							| 
									
										
										
										
											2017-05-24 13:04:55 +05:30
										 |  |  |       class Conflicts < FormulaCop | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |         extend AutoCorrector | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |         MSG = "Versioned formulae should not use `conflicts_with`. " \ | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  |               "Use `keg_only :versioned_formula` instead." | 
					
						
							| 
									
										
										
										
											2017-05-24 13:04:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 13:20:32 -04:00
										 |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           find_method_calls_by_name(body_node, :conflicts_with).each do |conflicts_with_call| | 
					
						
							|  |  |  |             next unless parameters(conflicts_with_call).last.respond_to? :values | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             reason = parameters(conflicts_with_call).last.values.first | 
					
						
							|  |  |  |             offending_node(reason) | 
					
						
							|  |  |  |             name = Regexp.new(@formula_name, Regexp::IGNORECASE) | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |             reason_text = string_content(reason).sub(name, "") | 
					
						
							|  |  |  |             first_word = reason_text.split.first | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if reason_text.match?(/\A[A-Z]/) | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |               problem "'#{first_word}' from the `conflicts_with` reason " \ | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |                       "should be '#{first_word.downcase}'." do |corrector| | 
					
						
							|  |  |  |                 reason_text[0] = reason_text[0].downcase | 
					
						
							|  |  |  |                 corrector.replace(reason.source_range, "\"#{reason_text}\"") | 
					
						
							|  |  |  |               end | 
					
						
							| 
									
										
										
										
											2020-07-02 13:20:32 -04:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |             next unless reason_text.end_with?(".") | 
					
						
							| 
									
										
										
										
											2020-07-02 13:20:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |             problem "`conflicts_with` reason should not end with a period." do |corrector| | 
					
						
							|  |  |  |               corrector.replace(reason.source_range, "\"#{reason_text.chop}\"") | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2020-07-02 13:20:32 -04:00
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 13:04:55 +05:30
										 |  |  |           return unless versioned_formula? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 16:15:52 +11:00
										 |  |  |           if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) && method_called_ever?(body_node, | 
					
						
							|  |  |  |                                                                                                    :conflicts_with) | 
					
						
							|  |  |  |             problem MSG do |corrector| | 
					
						
							|  |  |  |               corrector.replace(@offensive_node.source_range, "keg_only :versioned_formula") | 
					
						
							| 
									
										
										
										
											2020-07-02 13:20:32 -04:00
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-05-24 13:04:55 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |