From a8294a81c0434682319c26716626d6100d9d68d2 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Tue, 10 Apr 2018 03:22:28 +0530 Subject: [PATCH] urls_cop: Search for func calls to match `url` and `mirror` calls --- Library/Homebrew/rubocops/extend/formula_cop.rb | 11 +++++++++++ Library/Homebrew/rubocops/urls_cop.rb | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index ae343a9fc2..55f22bbc61 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -99,6 +99,17 @@ module RuboCop end end + # Returns array of function call nodes matching func_name in every descendant of node + # Ex. function call: foo(*args, **kwargs) + # Does not match method calls: foo.bar(*args, **kwargs) + # Returns every function calls if no func_name is passed + def find_every_func_call_by_name(node, func_name = nil) + return if node.nil? + node.each_descendant(:send).select do |func_node| + func_node.receiver.nil? && (func_name.nil? || func_name == func_node.method_name) + end + end + # Given a method_name and arguments, yields to a block with # matching method passed as a parameter to the block def find_method_with_args(node, method_name, *args) diff --git a/Library/Homebrew/rubocops/urls_cop.rb b/Library/Homebrew/rubocops/urls_cop.rb index 4ef801689d..0ffa19bdf2 100644 --- a/Library/Homebrew/rubocops/urls_cop.rb +++ b/Library/Homebrew/rubocops/urls_cop.rb @@ -6,8 +6,8 @@ module RuboCop # This cop audits urls and mirrors in Formulae class Urls < FormulaCop def audit_formula(_node, _class_node, _parent_class_node, body_node) - urls = find_every_method_call_by_name(body_node, :url) - mirrors = find_every_method_call_by_name(body_node, :mirror) + urls = find_every_func_call_by_name(body_node, :url) + mirrors = find_every_func_call_by_name(body_node, :mirror) # GNU urls; doesn't apply to mirrors gnu_pattern = %r{^(?:https?|ftp)://ftpmirror.gnu.org/(.*)} @@ -195,8 +195,8 @@ module RuboCop module FormulaAuditStrict class PyPiUrls < FormulaCop def audit_formula(_node, _class_node, _parent_class_node, body_node) - urls = find_every_method_call_by_name(body_node, :url) - mirrors = find_every_method_call_by_name(body_node, :mirror) + urls = find_every_func_call_by_name(body_node, :url) + mirrors = find_every_func_call_by_name(body_node, :mirror) urls += mirrors # Check pypi urls