mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
audit: Port FileUtils, inreplace audit rules in audit_lines to rubocop
This commit is contained in:
parent
bc2bcef1ba
commit
b5da76e28d
@ -809,17 +809,6 @@ class FormulaAuditor
|
|||||||
end
|
end
|
||||||
|
|
||||||
def line_problems(line, _lineno)
|
def line_problems(line, _lineno)
|
||||||
# FileUtils is included in Formula
|
|
||||||
# encfs modifies a file with this name, so check for some leading characters
|
|
||||||
if line =~ %r{[^'"/]FileUtils\.(\w+)}
|
|
||||||
problem "Don't need 'FileUtils.' before #{Regexp.last_match(1)}."
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check for long inreplace block vars
|
|
||||||
if line =~ /inreplace .* do \|(.{2,})\|/
|
|
||||||
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{Regexp.last_match(1)}|\"."
|
|
||||||
end
|
|
||||||
|
|
||||||
# Check for string interpolation of single values.
|
# Check for string interpolation of single values.
|
||||||
if line =~ /(system|inreplace|gsub!|change_make_var!).*[ ,]"#\{([\w.]+)\}"/
|
if line =~ /(system|inreplace|gsub!|change_make_var!).*[ ,]"#\{([\w.]+)\}"/
|
||||||
problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}"
|
problem "Don't need to interpolate \"#{Regexp.last_match(2)}\" with #{Regexp.last_match(1)}"
|
||||||
|
@ -85,9 +85,13 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of method call nodes matching method_name in every descendant of node
|
# Returns an array of method call nodes matching method_name in every descendant of node
|
||||||
def find_every_method_call_by_name(node, method_name)
|
# Returns every method call if no method_name is passed
|
||||||
|
def find_every_method_call_by_name(node, method_name = nil)
|
||||||
return if node.nil?
|
return if node.nil?
|
||||||
node.each_descendant(:send).select { |method_node| method_name == method_node.method_name }
|
node.each_descendant(:send).select do |method_node|
|
||||||
|
method_name.nil? ||
|
||||||
|
method_name == method_node.method_name
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Given a method_name and arguments, yields to a block with
|
# Given a method_name and arguments, yields to a block with
|
||||||
@ -107,7 +111,7 @@ module RuboCop
|
|||||||
def find_instance_method_call(node, instance, method_name)
|
def find_instance_method_call(node, instance, method_name)
|
||||||
methods = find_every_method_call_by_name(node, method_name)
|
methods = find_every_method_call_by_name(node, method_name)
|
||||||
methods.each do |method|
|
methods.each do |method|
|
||||||
next unless method.receiver.const_name == instance
|
next unless method.receiver && method.receiver.const_name == instance
|
||||||
@offense_source_range = method.source_range
|
@offense_source_range = method.source_range
|
||||||
@offensive_node = method
|
@offensive_node = method
|
||||||
yield method
|
yield method
|
||||||
@ -188,9 +192,15 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of block nodes of any depth below node in AST
|
# Returns an array of block nodes of any depth below node in AST
|
||||||
|
# If a block is given then yields matching block node to the block!
|
||||||
def find_all_blocks(node, block_name)
|
def find_all_blocks(node, block_name)
|
||||||
return if node.nil?
|
return if node.nil?
|
||||||
node.each_descendant(:block).select { |block_node| block_name == block_node.method_name }
|
blocks = node.each_descendant(:block).select { |block_node| block_name == block_node.method_name }
|
||||||
|
return blocks unless block_given?
|
||||||
|
blocks.each do |block_node|
|
||||||
|
offending_node(block_node)
|
||||||
|
yield block_node
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns a method definition node with method_name
|
# Returns a method definition node with method_name
|
||||||
|
@ -58,6 +58,23 @@ module RuboCop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
next unless block_arg.source.size>1
|
||||||
|
problem "\"inreplace <filenames> do |s|\" is preferred over \"|#{block_arg.source}|\"."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user