| 
									
										
										
										
											2017-03-02 20:26:29 +05:30
										 |  |  | require_relative "./extend/formula_cop" | 
					
						
							|  |  |  | require_relative "../extend/string" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 01:10:36 +05:30
										 |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							| 
									
										
										
										
											2017-04-23 04:09:13 +05:30
										 |  |  |     module FormulaAuditStrict | 
					
						
							| 
									
										
										
										
											2017-03-02 20:26:29 +05:30
										 |  |  |       # This cop audits `desc` in Formulae | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # - Checks for existence of `desc` | 
					
						
							|  |  |  |       # - Checks if size of `desc` > 80 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |       class DescLength < FormulaCop | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2017-03-02 01:10:36 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           # Check if a formula's desc is present | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           if desc_call.nil? | 
					
						
							|  |  |  |             problem "Formula should have a desc (Description)." | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-03-02 01:10:36 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-03-12 02:55:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           # Check if a formula's desc is too long | 
					
						
							| 
									
										
										
										
											2017-03-16 23:49:43 +05:30
										 |  |  |           desc = parameters(desc_call).first | 
					
						
							|  |  |  |           desc_length = "#{@formula_name}: #{string_content(desc)}".length | 
					
						
							| 
									
										
										
										
											2017-03-12 02:55:21 +08:00
										 |  |  |           max_desc_length = 80
 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           return if desc_length <= max_desc_length | 
					
						
							|  |  |  |           problem <<-EOS.undent
 | 
					
						
							|  |  |  |             Description is too long. "name: desc" should be less than #{max_desc_length} characters. | 
					
						
							|  |  |  |             Length is calculated as #{@formula_name} + desc. (currently #{desc_length}) | 
					
						
							|  |  |  |           EOS | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # This cop audits `desc` in Formulae | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # - 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 | 
					
						
							|  |  |  |       class Desc < FormulaCop | 
					
						
							|  |  |  |         VALID_LOWERCASE_WORDS = %w[
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:37:22 +01:00
										 |  |  |           ex | 
					
						
							|  |  |  |           eXtensible | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           iOS | 
					
						
							|  |  |  |           macOS | 
					
						
							| 
									
										
										
										
											2017-06-13 12:37:22 +01:00
										 |  |  |           malloc | 
					
						
							|  |  |  |           ooc | 
					
						
							|  |  |  |           preexec | 
					
						
							| 
									
										
										
										
											2017-08-01 11:36:44 -04:00
										 |  |  |           x86 | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           xUnit | 
					
						
							|  |  |  |         ].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) | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |           return if desc_call.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           desc = parameters(desc_call).first | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03: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 | 
					
						
							|  |  |  |           return unless regex_match_group(desc, /^#{@formula_name} /i) | 
					
						
							|  |  |  |           problem "Description shouldn't start with the formula name" | 
					
						
							| 
									
										
										
										
											2017-06-08 15:13:06 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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") | 
					
						
							|  |  |  |             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 |