| 
									
										
										
										
											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:49:21 +01:00
										 |  |  | require "forwardable" | 
					
						
							|  |  |  | require "uri" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module Cask | 
					
						
							|  |  |  |       # This cop checks that a cask's homepage ends with a slash | 
					
						
							|  |  |  |       # if it does not have a path component. | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |       class HomepageUrlTrailingSlash < Base | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |         include OnHomepageStanza | 
					
						
							| 
									
										
										
										
											2022-04-27 17:15:03 +01:00
										 |  |  |         include HelperFunctions | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |         extend AutoCorrector | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-03 08:50:45 +02:00
										 |  |  |         MSG_NO_SLASH = "'%<url>s' must have a slash after the domain." | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         def on_homepage_stanza(stanza) | 
					
						
							|  |  |  |           url_node = stanza.stanza_node.first_argument | 
					
						
							| 
									
										
										
										
											2019-10-16 19:06:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |           url = if url_node.dstr_type? | 
					
						
							|  |  |  |             # Remove quotes from interpolated string. | 
					
						
							|  |  |  |             url_node.source[1..-2] | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             url_node.str_content | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-16 18:37:20 +02:00
										 |  |  |           return unless url&.match?(%r{^.+://[^/]+$}) | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-27 17:15:03 +01:00
										 |  |  |           domain = URI(string_content(url_node, strip_dynamic: true)).host | 
					
						
							|  |  |  |           return if domain.blank? | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           # This also takes URLs like 'https://example.org?path' | 
					
						
							|  |  |  |           # and 'https://example.org#path' into account. | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |           corrected_source = url_node.source.sub("://#{domain}", "://#{domain}/") | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 18:25:05 +11:00
										 |  |  |           add_offense(url_node.loc.expression, message: format(MSG_NO_SLASH, url: url)) do |corrector| | 
					
						
							|  |  |  |             corrector.replace(url_node.source_range, corrected_source) | 
					
						
							| 
									
										
										
										
											2018-10-26 19:49:21 +01:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |