| 
									
										
										
										
											2024-11-21 14:20:36 +00:00
										 |  |  | # typed: strict | 
					
						
							|  |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     # Checks if rubocop disable comments have a clarifying comment preceding them. | 
					
						
							|  |  |  |     class DisableComment < Base | 
					
						
							|  |  |  |       MSG = "Add a clarifying comment to the RuboCop disable comment" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sig { void } | 
					
						
							|  |  |  |       def on_new_investigation | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         processed_source.comments.each do |comment| | 
					
						
							|  |  |  |           next unless disable_comment?(comment) | 
					
						
							|  |  |  |           next if comment?(processed_source[comment.loc.line - 2]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           add_offense(comment) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 20:20:00 +01:00
										 |  |  |       sig { params(comment: Parser::Source::Comment).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2024-11-21 14:20:36 +00:00
										 |  |  |       def disable_comment?(comment) | 
					
						
							|  |  |  |         comment.text.start_with? "# rubocop:disable" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 20:20:00 +01:00
										 |  |  |       sig { params(line: String).returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2024-11-21 14:20:36 +00:00
										 |  |  |       def comment?(line) | 
					
						
							| 
									
										
										
										
											2025-01-31 20:37:00 +01:00
										 |  |  |         line.strip.start_with?("#") && line.strip.delete_prefix("#") != "" | 
					
						
							| 
									
										
										
										
											2024-11-21 14:20:36 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |