audit: Port rules from line_problems to rubocop part 4(WIP)

This commit is contained in:
Gautham Goli 2017-08-07 14:08:22 +05:30
parent d9c81901c3
commit a92e1eda27
2 changed files with 41 additions and 23 deletions

View File

@ -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))/

View File

@ -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