From a92e1eda27d732e0de75592d8fb52e93bb5e0d33 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Mon, 7 Aug 2017 14:08:22 +0530 Subject: audit: Port rules from line_problems to rubocop part 4(WIP) --- Library/Homebrew/dev-cmd/audit.rb | 23 ------------------- Library/Homebrew/rubocops/lines_cop.rb | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 23 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 80b9824aa..db99656cb 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -927,15 +927,6 @@ class FormulaAuditor problem "Use MacOS.full_version instead of MACOS_FULL_VERSION" end - cats = %w[leopard snow_leopard lion mountain_lion].join("|") - if line =~ /MacOS\.(?:#{cats})\?/ - problem "\"#{$&}\" is deprecated, use a comparison to MacOS.version instead" - end - - if line =~ /depends_on [A-Z][\w:]+\.new$/ - problem "`depends_on` can take requirement classes instead of instances" - end - if line =~ /^def (\w+).*$/ problem "Define method #{Regexp.last_match(1).inspect} in the class body, not at the top-level" end @@ -956,20 +947,6 @@ class FormulaAuditor conditional_dep_problems(Regexp.last_match(1), Regexp.last_match(2), $&) end - if line =~ /(Dir\[("[^\*{},]+")\])/ - problem "#{Regexp.last_match(1)} is unnecessary; just use #{Regexp.last_match(2)}" - end - - if line =~ /system (["'](#{FILEUTILS_METHODS})["' ])/o - system = Regexp.last_match(1) - method = Regexp.last_match(2) - problem "Use the `#{method}` Ruby method instead of `system #{system}`" - end - - if line =~ /assert [^!]+\.include?/ - problem "Use `assert_match` instead of `assert ...include?`" - end - return unless @strict problem "`#{Regexp.last_match(1)}` in formulae is deprecated" if line =~ /(env :(std|userpaths))/ diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 7bf9e6056..338a3256a 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -1,3 +1,4 @@ +require 'FileUtils' require_relative "./extend/formula_cop" module RuboCop @@ -123,6 +124,37 @@ module RuboCop find_instance_method_call(body_node, "ENV", :x11) do problem 'Use "depends_on :x11" instead of "ENV.x11"' end + + find_every_method_call_by_name(body_node, :assert).each do |m| + if method_called?(m, :include?) && !method_called?(m, :!) + problem "Use `assert_match` instead of `assert ...include?`" + end + end + + find_every_method_call_by_name(body_node, :depends_on).each do |m| + next unless method_called?(m, :new) + problem "`depends_on` can take requirement classes instead of instances" + end + + os = [:leopard?, :snow_leopard?, :lion?, :mountain_lion?] + os.each do |version| + find_instance_method_call(body_node, :MacOS, version) do |m| + problem "\"#{m.source}\" is deprecated, use a comparison to MacOS.version instead" + end + end + + dirPattern(body_node) do |m| + next unless m =~ /\[("[^\*{},]+")\]/ + problem "Dir(#{Regexp.last_match(1)}) is unnecessary; just use #{Regexp.last_match(1)}" + end + + fileUtils_methods= FileUtils.singleton_methods(false).map { |m| Regexp.escape(m) }.join "|" + find_method_with_args(body_node, :system, /fileUtils_methods/) do |m| + method = string_content(@offensive_node) + problem "Use the `#{method}` Ruby method instead of `#{m.source}`" + end + + end # This is Pattern Matching method for AST @@ -131,7 +163,16 @@ module RuboCop def_node_search :languageNode?, <<-PATTERN (const (const nil :Language) :Node) PATTERN + + def_node_search :dirPattern, <<-PATTERN + (send (const nil :Dir) :[] (str $_)) + PATTERN end end end end + +# Strict rules ported early +# find_method_with_args(@processed_source.ast, :require, "formula") do |m| +# problem "#{m.source} is now unnecessary" +# end -- cgit v1.2.3