| 
									
										
										
										
											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-03-02 20:26:29 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 01:10:36 +05:30
										 |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							| 
									
										
										
										
											2018-04-25 07:48:21 +10:00
										 |  |  |     module FormulaAudit | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |       # This cop audits `desc` in Formulae. | 
					
						
							| 
									
										
										
										
											2017-03-02 20:26:29 +05:30
										 |  |  |       # | 
					
						
							|  |  |  |       # - Checks for existence of `desc` | 
					
						
							|  |  |  |       # - Checks if size of `desc` > 80 | 
					
						
							| 
									
										
										
										
											2018-08-06 18:43:26 +01:00
										 |  |  |       # - Checks for leading/trailing whitespace in `desc` | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |       # - Checks if `desc` begins with an article | 
					
						
							|  |  |  |       # - Checks for correct usage of `command-line` in `desc` | 
					
						
							|  |  |  |       # - Checks description starts with a capital letter | 
					
						
							|  |  |  |       # - Checks if `desc` contains the formula name | 
					
						
							| 
									
										
										
										
											2017-11-01 19:10:48 +00:00
										 |  |  |       # - Checks if `desc` ends with a full stop (apart from in the case of "etc.") | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |       class Desc < FormulaCop | 
					
						
							|  |  |  |         VALID_LOWERCASE_WORDS = %w[
 | 
					
						
							|  |  |  |           macOS | 
					
						
							|  |  |  |         ].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 00:36:18 -04:00
										 |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           desc_call = find_node_method_by_name(body_node, :desc) | 
					
						
							| 
									
										
										
										
											2020-04-13 14:35:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           # Check if a formula's desc is present | 
					
						
							|  |  |  |           if desc_call.nil? | 
					
						
							|  |  |  |             problem "Formula should have a desc (Description)." | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |           desc = parameters(desc_call).first | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 14:35:03 +01:00
										 |  |  |           # Check the formula's desc length. Should be >0 and <80 characters. | 
					
						
							|  |  |  |           pure_desc_length = string_content(desc).length | 
					
						
							|  |  |  |           if pure_desc_length.zero? | 
					
						
							|  |  |  |             problem "The desc (description) should not be an empty string." | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-06 18:43:26 +01:00
										 |  |  |           # Check for leading whitespace. | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |           problem "Description shouldn't have a leading space" if regex_match_group(desc, /^\s+/) | 
					
						
							| 
									
										
										
										
											2018-08-06 18:43:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |           # Check for trailing whitespace. | 
					
						
							| 
									
										
										
										
											2019-02-19 13:11:32 +00:00
										 |  |  |           problem "Description shouldn't have a trailing space" if regex_match_group(desc, /\s+$/) | 
					
						
							| 
									
										
										
										
											2018-08-06 18:43:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           # Check if command-line is wrongly used in formula's desc | 
					
						
							|  |  |  |           if match = regex_match_group(desc, /(command ?line)/i) | 
					
						
							| 
									
										
										
										
											2017-06-02 22:25:07 +01:00
										 |  |  |             c = match.to_s.chars.first | 
					
						
							|  |  |  |             problem "Description should use \"#{c}ommand-line\" instead of \"#{match}\"" | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           # Check if a/an are used in a formula's desc | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           if match = regex_match_group(desc, /^(an?)\s/i) | 
					
						
							| 
									
										
										
										
											2019-04-08 12:47:15 -04:00
										 |  |  |             problem "Description shouldn't start with an indefinite article, i.e. \"#{match.to_s.strip}\"" | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           # Check if invalid uppercase words are at the start of a | 
					
						
							|  |  |  |           # formula's desc | 
					
						
							|  |  |  |           if !VALID_LOWERCASE_WORDS.include?(string_content(desc).split.first) && | 
					
						
							|  |  |  |              regex_match_group(desc, /^[a-z]/) | 
					
						
							| 
									
										
										
										
											2017-06-02 22:25:07 +01:00
										 |  |  |             problem "Description should start with a capital letter" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 08:14:58 -07:00
										 |  |  |           # Check if formula's desc starts with formula's name | 
					
						
							| 
									
										
										
										
											2017-06-17 07:12:46 +01:00
										 |  |  |           if regex_match_group(desc, /^#{@formula_name} /i) | 
					
						
							|  |  |  |             problem "Description shouldn't start with the formula name" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 19:10:48 +00:00
										 |  |  |           # Check if a full stop is used at the end of a formula's desc (apart from in the case of "etc.") | 
					
						
							| 
									
										
										
										
											2020-04-13 14:35:03 +01:00
										 |  |  |           if regex_match_group(desc, /\.$/) && !string_content(desc).end_with?("etc.") | 
					
						
							|  |  |  |             problem "Description shouldn't end with a full stop" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 14:35:03 +01:00
										 |  |  |           desc_length = "#{@formula_name}: #{string_content(desc)}".length | 
					
						
							|  |  |  |           max_desc_length = 80
 | 
					
						
							|  |  |  |           return if desc_length <= max_desc_length | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           problem "Description is too long. \"name: desc\" should be less than #{max_desc_length} characters. " \ | 
					
						
							|  |  |  |                   "Length is calculated as #{@formula_name} + desc. (currently #{desc_length})" | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def autocorrect(node) | 
					
						
							|  |  |  |           lambda do |corrector| | 
					
						
							|  |  |  |             correction = node.source | 
					
						
							|  |  |  |             first_word = string_content(node).split.first | 
					
						
							|  |  |  |             unless VALID_LOWERCASE_WORDS.include?(first_word) | 
					
						
							|  |  |  |               first_char = first_word.to_s.chars.first | 
					
						
							|  |  |  |               correction.sub!(/^(['"]?)([a-z])/, "\\1#{first_char.upcase}") | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             correction.sub!(/^(['"]?)an?\s/i, "\\1") | 
					
						
							|  |  |  |             correction.gsub!(/(ommand ?line)/i, "ommand-line") | 
					
						
							|  |  |  |             correction.gsub!(/(^|[^a-z])#{@formula_name}([^a-z]|$)/i, "\\1\\2") | 
					
						
							|  |  |  |             correction.gsub!(/^(['"]?)\s+/, "\\1") | 
					
						
							|  |  |  |             correction.gsub!(/\s+(['"]?)$/, "\\1") | 
					
						
							| 
									
										
										
										
											2017-11-01 19:17:17 +00:00
										 |  |  |             correction.gsub!(/\.(['"]?)$/, "\\1") | 
					
						
							| 
									
										
										
										
											2018-08-06 18:43:26 +01:00
										 |  |  |             correction.gsub!(/^\s+/, "") | 
					
						
							|  |  |  |             correction.gsub!(/\s+$/, "") | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |             corrector.insert_before(node.source_range, correction) | 
					
						
							|  |  |  |             corrector.remove(node.source_range) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-03-12 02:55:21 +08:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-03-02 01:10:36 +05:30
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |