| 
									
										
										
										
											2017-07-29 16:36:32 +05:30
										 |  |  | require_relative "./extend/formula_cop" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module RuboCop | 
					
						
							|  |  |  |   module Cop | 
					
						
							|  |  |  |     module FormulaAudit | 
					
						
							|  |  |  |       # This cop checks for various miscellaneous Homebrew coding styles | 
					
						
							|  |  |  |       class Lines < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, _body_node) | 
					
						
							| 
									
										
										
										
											2017-12-30 18:58:41 +00:00
										 |  |  |           [:automake, :ant, :autoconf, :emacs, :expat, :libtool, :mysql, :perl, | 
					
						
							|  |  |  |            :postgresql, :python, :python3, :rbenv, :ruby].each do |dependency| | 
					
						
							| 
									
										
										
										
											2017-07-29 16:36:32 +05:30
										 |  |  |             next unless depends_on?(dependency) | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |             problem ":#{dependency} is deprecated. Usage should be \"#{dependency}\"." | 
					
						
							| 
									
										
										
										
											2017-07-29 16:36:32 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 18:58:41 +00:00
										 |  |  |           { apr: "apr-util", fortran: "gcc", gpg: "gnupg", hg: "mercurial", | 
					
						
							|  |  |  |             mpi: "open-mpi", python2: "python" }.each do |requirement, dependency| | 
					
						
							| 
									
										
										
										
											2017-12-23 16:38:24 +00:00
										 |  |  |             next unless depends_on?(requirement) | 
					
						
							|  |  |  |             problem ":#{requirement} is deprecated. Usage should be \"#{dependency}\"." | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |           problem ":tex is deprecated." if depends_on?(:tex) | 
					
						
							| 
									
										
										
										
											2017-07-29 16:36:32 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |       class ClassInheritance < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, class_node, parent_class_node, _body_node) | 
					
						
							|  |  |  |           begin_pos = start_column(parent_class_node) | 
					
						
							|  |  |  |           end_pos = end_column(class_node) | 
					
						
							|  |  |  |           return unless begin_pos-end_pos != 3
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           problem "Use a space in class inheritance: class #{@formula_name.capitalize} < #{class_name(parent_class_node)}" | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class Comments < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, _body_node) | 
					
						
							|  |  |  |           audit_comments do |comment| | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |               "# PLEASE REMOVE", | 
					
						
							|  |  |  |               "# Documentation:", | 
					
						
							|  |  |  |               "# if this fails, try separate make/make install steps", | 
					
						
							|  |  |  |               "# The URL of the archive", | 
					
						
							|  |  |  |               "## Naming --", | 
					
						
							|  |  |  |               "# if your formula requires any X11/XQuartz components", | 
					
						
							|  |  |  |               "# if your formula fails when building in parallel", | 
					
						
							|  |  |  |               "# Remove unrecognized options if warned by configure", | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  |               '# system "cmake', | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  |             ].each do |template_comment| | 
					
						
							|  |  |  |               next unless comment.include?(template_comment) | 
					
						
							|  |  |  |               problem "Please remove default template comments" | 
					
						
							|  |  |  |               break | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           audit_comments do |comment| | 
					
						
							|  |  |  |             # Commented-out depends_on | 
					
						
							|  |  |  |             next unless comment =~ /#\s*depends_on\s+(.+)\s*$/ | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  |             problem "Commented-out dependency #{Regexp.last_match(1)}" | 
					
						
							| 
									
										
										
										
											2017-07-30 20:14:59 +05:30
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-31 14:30:37 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |       class AssertStatements < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           find_every_method_call_by_name(body_node, :assert).each do |method| | 
					
						
							|  |  |  |             if method_called_ever?(method, :include?) && !method_called_ever?(method, :!) | 
					
						
							|  |  |  |               problem "Use `assert_match` instead of `assert ...include?`" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if method_called_ever?(method, :exist?) && !method_called_ever?(method, :!) | 
					
						
							|  |  |  |               problem "Use `assert_predicate <path_to_file>, :exist?` instead of `#{method.source}`" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if method_called_ever?(method, :exist?) && method_called_ever?(method, :!) | 
					
						
							|  |  |  |               problem "Use `refute_predicate <path_to_file>, :exist?` instead of `#{method.source}`" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if method_called_ever?(method, :executable?) && !method_called_ever?(method, :!) | 
					
						
							|  |  |  |               problem "Use `assert_predicate <path_to_file>, :executable?` instead of `#{method.source}`" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       class OptionDeclarations < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           if find_method_def(body_node, :options) | 
					
						
							|  |  |  |             problem "Use new-style option definitions" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :without?) do |method| | 
					
						
							|  |  |  |             next unless unless_modifier?(method.parent) | 
					
						
							|  |  |  |             correct = method.source.gsub("out?", "?") | 
					
						
							|  |  |  |             problem "Use if #{correct} instead of unless #{method.source}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :with?) do |method| | 
					
						
							|  |  |  |             next unless unless_modifier?(method.parent) | 
					
						
							|  |  |  |             correct = method.source.gsub("?", "out?") | 
					
						
							|  |  |  |             problem "Use if #{correct} instead of unless #{method.source}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :with?) do |method| | 
					
						
							| 
									
										
										
										
											2017-10-25 16:05:29 +05:30
										 |  |  |             next unless expression_negated?(method) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Don't negate 'build.with?': use 'build.without?'" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :without?) do |method| | 
					
						
							| 
									
										
										
										
											2017-10-25 16:05:29 +05:30
										 |  |  |             next unless expression_negated?(method) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Don't negate 'build.without?': use 'build.with?'" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :without?) do |method| | 
					
						
							|  |  |  |             arg = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(arg, /^-?-?without-(.*)/) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Don't duplicate 'without': Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\"" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :with?) do |method| | 
					
						
							|  |  |  |             arg = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(arg, /^-?-?with-(.*)/) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\"" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :include?) do |method| | 
					
						
							|  |  |  |             arg = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(arg, /^with(out)?-(.*)/) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Use build.with#{match[1]}? \"#{match[2]}\" instead of build.include? 'with#{match[1]}-#{match[2]}'" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, :build, :include?) do |method| | 
					
						
							|  |  |  |             arg = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(arg, /^\-\-(.*)$/) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "Reference '#{match[1]}' without dashes" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def unless_modifier?(node) | 
					
						
							|  |  |  |           return false unless node.if_type? | 
					
						
							|  |  |  |           node.modifier_form? && node.unless? | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-31 14:30:37 +05:30
										 |  |  |       class Miscellaneous < FormulaCop | 
					
						
							|  |  |  |         def audit_formula(_node, _class_node, _parent_class_node, body_node) | 
					
						
							|  |  |  |           # FileUtils is included in Formula | 
					
						
							|  |  |  |           # encfs modifies a file with this name, so check for some leading characters | 
					
						
							|  |  |  |           find_instance_method_call(body_node, "FileUtils", nil) do |method_node| | 
					
						
							|  |  |  |             problem "Don't need 'FileUtils.' before #{method_node.method_name}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # Check for long inreplace block vars | 
					
						
							|  |  |  |           find_all_blocks(body_node, :inreplace) do |node| | 
					
						
							|  |  |  |             block_arg = node.arguments.children.first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless block_arg.source.size > 1
 | 
					
						
							| 
									
										
										
										
											2017-07-31 14:30:37 +05:30
										 |  |  |             problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"." | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           [:rebuild, :version_scheme].each do |method_name| | 
					
						
							|  |  |  |             find_method_with_args(body_node, method_name, 0) do | 
					
						
							|  |  |  |               problem "'#{method_name} 0' should be removed" | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           [:mac?, :linux?].each do |method_name| | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             next unless formula_tap == "homebrew-core" | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             find_instance_method_call(body_node, "OS", method_name) do |check| | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |               problem "Don't use #{check.source}; Homebrew/core only supports macOS" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-02 17:03:11 +05:30
										 |  |  |           find_instance_call(body_node, "ARGV") do |method_node| | 
					
						
							|  |  |  |             next if [:debug?, :verbose?, :value].index(method_node.method_name) | 
					
						
							|  |  |  |             problem "Use build instead of ARGV to check options" | 
					
						
							| 
									
										
										
										
											2017-08-14 15:22:44 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-14 15:41:03 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_instance_method_call(body_node, :man, :+) do |method| | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(parameters(method).first, /^man[1-8]$/) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             problem "\"#{method.source}\" should be \"#{match[0]}\"" | 
					
						
							| 
									
										
										
										
											2017-08-14 15:41:03 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 19:58:39 +05:30
										 |  |  |           # Avoid hard-coding compilers | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :system).each do |method| | 
					
						
							|  |  |  |             param = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-24 16:03:13 +01:00
										 |  |  |             if match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)\s?}) | 
					
						
							| 
									
										
										
										
											2017-08-14 19:58:39 +05:30
										 |  |  |               problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\"" | 
					
						
							| 
									
										
										
										
											2017-10-27 14:58:07 +01:00
										 |  |  |             elsif match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?}) | 
					
						
							| 
									
										
										
										
											2017-08-14 19:58:39 +05:30
										 |  |  |               problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-14 20:10:45 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_instance_method_call(body_node, "ENV", :[]=) do |method| | 
					
						
							|  |  |  |             param = parameters(method)[1] | 
					
						
							| 
									
										
										
										
											2017-10-24 16:03:13 +01:00
										 |  |  |             if match = regex_match_group(param, %r{^(/usr/bin/)?(gcc|llvm-gcc|clang)\s?}) | 
					
						
							| 
									
										
										
										
											2017-08-14 20:10:45 +05:30
										 |  |  |               problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\"" | 
					
						
							| 
									
										
										
										
											2017-10-24 16:03:13 +01:00
										 |  |  |             elsif match = regex_match_group(param, %r{^(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?}) | 
					
						
							| 
									
										
										
										
											2017-08-14 20:10:45 +05:30
										 |  |  |               problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  |           # Prefer formula path shortcuts in strings | 
					
						
							|  |  |  |           formula_path_strings(body_node, :share) do |p| | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(p, %r{^(/(man))/?}) | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |             problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\"" | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 22:44:28 +05:30
										 |  |  |           formula_path_strings(body_node, :prefix) do |p| | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             if match = regex_match_group(p, %r{^(/share/(info|man))$}) | 
					
						
							| 
									
										
										
										
											2017-08-14 22:44:28 +05:30
										 |  |  |               problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2]}}\"" | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             if match = regex_match_group(p, %r{^((/share/man/)(man[1-8]))}) | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  |               problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[3]}}\"" | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             if match = regex_match_group(p, %r{^(/(bin|include|libexec|lib|sbin|share|Frameworks))}i) | 
					
						
							| 
									
										
										
										
											2017-08-14 22:44:28 +05:30
										 |  |  |               problem "\"\#\{prefix}#{match[1]}\" should be \"\#{#{match[2].downcase}}\"" | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-14 23:32:06 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :depends_on).each do |method| | 
					
						
							|  |  |  |             key, value = destructure_hash(parameters(method).first) | 
					
						
							| 
									
										
										
										
											2017-10-21 01:39:04 +05:30
										 |  |  |             next if key.nil? || value.nil? | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(value, /^(lua|perl|python|ruby)(\d*)/) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             problem "#{match[1]} modules should be vendored rather than use deprecated #{method.source}`" | 
					
						
							| 
									
										
										
										
											2017-08-14 23:05:00 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-14 21:34:01 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :system).each do |method| | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(parameters(method).first, /^(env|export)(\s+)?/) | 
					
						
							| 
									
										
										
										
											2017-08-14 23:32:06 +05:30
										 |  |  |             problem "Use ENV instead of invoking '#{match[1]}' to modify the environment" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :depends_on).each do |method| | 
					
						
							|  |  |  |             next if modifier?(method.parent) | 
					
						
							|  |  |  |             param = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-08-15 00:05:50 +05:30
										 |  |  |             dep, option = hash_dep(param) | 
					
						
							|  |  |  |             next if dep.nil? || option.nil? | 
					
						
							|  |  |  |             offending_node(param) | 
					
						
							|  |  |  |             problem "Dependency #{string_content(dep)} should not use option #{string_content(option)}" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_instance_method_call(body_node, :version, :==) do |method| | 
					
						
							|  |  |  |             next unless parameters_passed?(method, "HEAD") | 
					
						
							| 
									
										
										
										
											2017-08-15 00:29:58 +05:30
										 |  |  |             problem "Use 'build.head?' instead of inspecting 'version'" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_instance_method_call(body_node, "ARGV", :include?) do |method| | 
					
						
							|  |  |  |             param = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |             next unless match = regex_match_group(param, /^--(HEAD|devel)/) | 
					
						
							| 
									
										
										
										
											2017-08-15 00:36:37 +05:30
										 |  |  |             problem "Use \"if build.#{match[1].downcase}?\" instead" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 00:42:56 +05:30
										 |  |  |           find_const(body_node, "MACOS_VERSION") do | 
					
						
							|  |  |  |             problem "Use MacOS.version instead of MACOS_VERSION" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_const(body_node, "MACOS_FULL_VERSION") do | 
					
						
							|  |  |  |             problem "Use MacOS.full_version instead of MACOS_FULL_VERSION" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-15 16:09:32 +05:30
										 |  |  |           conditional_dependencies(body_node) do |node, method, param, dep_node| | 
					
						
							|  |  |  |             dep = string_content(dep_node) | 
					
						
							|  |  |  |             if node.if? | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |               if (method == :include? && regex_match_group(param, /^with-#{dep}$/)) || | 
					
						
							|  |  |  |                  (method == :with? && regex_match_group(param, /^#{dep}$/)) | 
					
						
							| 
									
										
										
										
											2017-08-15 16:09:32 +05:30
										 |  |  |                 offending_node(dep_node.parent) | 
					
						
							|  |  |  |                 problem "Replace #{node.source} with #{dep_node.parent.source} => :optional" | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             elsif node.unless? | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |               if (method == :include? && regex_match_group(param, /^without-#{dep}$/)) || | 
					
						
							|  |  |  |                  (method == :without? && regex_match_group(param, /^#{dep}$/)) | 
					
						
							| 
									
										
										
										
											2017-08-15 16:09:32 +05:30
										 |  |  |                 offending_node(dep_node.parent) | 
					
						
							|  |  |  |                 problem "Replace #{node.source} with #{dep_node.parent.source} => :recommended" | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           find_method_with_args(body_node, :fails_with, :llvm) do | 
					
						
							|  |  |  |             problem "'fails_with :llvm' is now a no-op so should be removed" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 19:15:52 +00:00
										 |  |  |           find_method_with_args(body_node, :needs, :openmp) do | 
					
						
							|  |  |  |             problem "'needs :openmp' should be replaced with 'depends_on \"gcc\"'" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 23:40:04 +05:30
										 |  |  |           find_method_with_args(body_node, :system, /^(otool|install_name_tool|lipo)/) do | 
					
						
							| 
									
										
										
										
											2017-08-30 15:48:41 +05:30
										 |  |  |             next if @formula_name == "cctools" | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             problem "Use ruby-macho instead of calling #{@offensive_node.source}" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |           find_every_method_call_by_name(body_node, :system).each do |method_node| | 
					
						
							| 
									
										
										
										
											2017-08-30 15:48:41 +05:30
										 |  |  |             # Skip Kibana: npm cache edge (see formula for more details) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             next if @formula_name =~ /^kibana(\@\d+(\.\d+)?)?$/i | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             first_param, second_param = parameters(method_node) | 
					
						
							|  |  |  |             next if !node_equals?(first_param, "npm") || | 
					
						
							|  |  |  |                     !node_equals?(second_param, "install") | 
					
						
							|  |  |  |             offending_node(method_node) | 
					
						
							|  |  |  |             problem "Use Language::Node for npm install args" unless languageNodeModule?(method_node) | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           if find_method_def(body_node, :test) | 
					
						
							|  |  |  |             problem "Use new-style test definitions (test do)" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_method_with_args(body_node, :skip_clean, :all) do | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |             problem "`skip_clean :all` is deprecated; brew no longer strips symbols. " \ | 
					
						
							|  |  |  |                     "Pass explicit paths to prevent Homebrew from removing empty folders." | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 12:50:49 +05:30
										 |  |  |           if find_method_def(@processed_source.ast) | 
					
						
							|  |  |  |             problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           find_instance_method_call(body_node, :build, :universal?) do | 
					
						
							| 
									
										
										
										
											2017-08-30 15:48:41 +05:30
										 |  |  |             next if @formula_name == "wine" | 
					
						
							|  |  |  |             problem "macOS has been 64-bit only since 10.6 so build.universal? is deprecated." | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, "ENV", :universal_binary) do | 
					
						
							| 
									
										
										
										
											2017-10-08 21:33:23 -04:00
										 |  |  |             next if @formula_name == "wine" | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |             problem "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated." | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           find_instance_method_call(body_node, "ENV", :x11) do | 
					
						
							|  |  |  |             problem 'Use "depends_on :x11" instead of "ENV.x11"' | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-07 14:08:22 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :depends_on).each do |method| | 
					
						
							|  |  |  |             next unless method_called?(method, :new) | 
					
						
							| 
									
										
										
										
											2017-08-07 14:08:22 +05:30
										 |  |  |             problem "`depends_on` can take requirement classes instead of instances" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-13 14:50:29 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |           os = [:leopard?, :snow_leopard?, :lion?, :mountain_lion?] | 
					
						
							|  |  |  |           os.each do |version| | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             find_instance_method_call(body_node, "MacOS", version) do |method| | 
					
						
							|  |  |  |               problem "\"#{method.source}\" is deprecated, use a comparison to MacOS.version instead" | 
					
						
							| 
									
										
										
										
											2017-08-13 14:50:29 +05:30
										 |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-08-14 00:02:44 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_instance_method_call(body_node, "Dir", :[]) do |method| | 
					
						
							| 
									
										
										
										
											2017-11-24 23:39:38 +05:30
										 |  |  |             next unless parameters(method).size == 1
 | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             path = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-21 01:39:04 +05:30
										 |  |  |             next unless path.str_type? | 
					
						
							| 
									
										
										
										
											2017-08-14 00:02:44 +05:30
										 |  |  |             next unless match = regex_match_group(path, /^[^\*{},]+$/) | 
					
						
							|  |  |  |             problem "Dir([\"#{string_content(path)}\"]) is unnecessary; just use \"#{match[0]}\"" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 01:39:04 +05:30
										 |  |  |           fileutils_methods = Regexp.new(FileUtils.singleton_methods(false).map { |m| "(?-mix:^" + Regexp.escape(m) + "$)" }.join("|")) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |           find_every_method_call_by_name(body_node, :system).each do |method| | 
					
						
							|  |  |  |             param = parameters(method).first | 
					
						
							| 
									
										
										
										
											2017-10-21 01:39:04 +05:30
										 |  |  |             next unless match = regex_match_group(param, fileutils_methods) | 
					
						
							| 
									
										
										
										
											2017-10-12 00:29:19 +05:30
										 |  |  |             problem "Use the `#{match}` Ruby method instead of `#{method.source}`" | 
					
						
							| 
									
										
										
										
											2017-08-14 01:09:06 +05:30
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2017-07-31 14:30:37 +05:30
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  |         def modifier?(node) | 
					
						
							| 
									
										
										
										
											2017-08-15 00:05:50 +05:30
										 |  |  |           return false unless node.if_type? | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  |           node.modifier_form? | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 13:26:25 +05:30
										 |  |  |         def_node_search :conditional_dependencies, <<~EOS | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |           {$(if (send (send nil? :build) ${:include? :with? :without?} $(str _)) | 
					
						
							|  |  |  |               (send nil? :depends_on $({str sym} _)) nil?) | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |            $(if (send (send nil? :build) ${:include? :with? :without?} $(str _)) nil? | 
					
						
							|  |  |  |               (send nil? :depends_on $({str sym} _)))} | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Match depends_on with hash as argument | 
					
						
							| 
									
										
										
										
											2017-10-21 13:26:25 +05:30
										 |  |  |         def_node_matcher :hash_dep, <<~EOS | 
					
						
							| 
									
										
										
										
											2017-08-15 00:05:50 +05:30
										 |  |  |           {(hash (pair $(str _) $(str _))) | 
					
						
							|  |  |  |            (hash (pair $(str _) (array $(str _) ...)))} | 
					
						
							| 
									
										
										
										
											2017-08-10 19:57:53 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 13:26:25 +05:30
										 |  |  |         def_node_matcher :destructure_hash, <<~EOS | 
					
						
							| 
									
										
										
										
											2017-08-14 23:05:00 +05:30
										 |  |  |           (hash (pair $(str _) $(sym _))) | 
					
						
							| 
									
										
										
										
											2017-08-12 20:50:43 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-21 13:26:25 +05:30
										 |  |  |         def_node_search :formula_path_strings, <<~EOS | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |           {(dstr (begin (send nil? %1)) $(str _ )) | 
					
						
							|  |  |  |            (dstr _ (begin (send nil? %1)) $(str _ ))} | 
					
						
							| 
									
										
										
										
											2017-08-12 20:50:43 +05:30
										 |  |  |         EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |         # Node Pattern search for Language::Node | 
					
						
							| 
									
										
										
										
											2017-10-15 02:28:32 +02:00
										 |  |  |         def_node_search :languageNodeModule?, <<~EOS | 
					
						
							| 
									
										
										
										
											2017-10-21 03:12:50 +02:00
										 |  |  |           (const (const nil? :Language) :Node) | 
					
						
							| 
									
										
										
										
											2017-08-04 01:18:00 +05:30
										 |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2017-07-31 14:30:37 +05:30
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2017-07-29 16:36:32 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |