2020-10-10 14:16:11 +02:00
|
|
|
# typed: false
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-26 16:24:48 +02:00
|
|
|
# Silence compatibility warning.
|
|
|
|
begin
|
|
|
|
old_verbosity = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
require "parser/current"
|
|
|
|
ensure
|
|
|
|
$VERBOSE = old_verbosity
|
|
|
|
end
|
|
|
|
|
2018-09-02 23:30:07 +02:00
|
|
|
require "extend/string"
|
2020-07-30 16:29:12 +01:00
|
|
|
require "rubocops/shared/helper_functions"
|
2017-05-30 01:22:47 +05:30
|
|
|
|
2017-03-02 20:26:29 +05:30
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2020-08-26 02:50:44 +02:00
|
|
|
# Superclass for all formula cops.
|
|
|
|
#
|
|
|
|
# @api private
|
2017-04-23 04:09:13 +05:30
|
|
|
class FormulaCop < Cop
|
2018-03-07 16:14:55 +00:00
|
|
|
include RangeHelp
|
2020-07-30 16:29:12 +01:00
|
|
|
include HelperFunctions
|
2018-03-07 16:14:55 +00:00
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
attr_accessor :file_path
|
2020-05-12 08:32:27 +01:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@registry = Cop.registry
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# This method is called by RuboCop and is the main entry point.
|
2017-04-23 04:09:13 +05:30
|
|
|
def on_class(node)
|
2017-08-04 01:18:00 +05:30
|
|
|
@file_path = processed_source.buffer.name
|
|
|
|
return unless file_path_allowed?
|
2017-05-24 00:07:06 +05:30
|
|
|
return unless formula_class?(node)
|
2017-04-23 04:09:13 +05:30
|
|
|
return unless respond_to?(:audit_formula)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-05-30 01:22:47 +05:30
|
|
|
class_node, parent_class_node, @body = *node
|
2017-08-30 15:48:41 +05:30
|
|
|
@formula_name = Pathname.new(@file_path).basename(".rb").to_s
|
2020-11-27 01:23:07 -05:00
|
|
|
@tap_style_exceptions = nil
|
2017-05-30 01:22:47 +05:30
|
|
|
audit_formula(node, class_node, parent_class_node, @body)
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-02 20:26:29 +05:30
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Yields to block when there is a match.
|
2020-11-05 17:17:03 -05:00
|
|
|
#
|
2018-10-18 21:42:43 -04:00
|
|
|
# @param urls [Array] url/mirror method call nodes
|
2020-11-05 17:17:03 -05:00
|
|
|
# @param regex [Regexp] pattern to match URLs
|
2017-07-30 12:57:57 +05:30
|
|
|
def audit_urls(urls, regex)
|
|
|
|
urls.each do |url_node|
|
|
|
|
url_string_node = parameters(url_node).first
|
|
|
|
url_string = string_content(url_string_node)
|
|
|
|
match_object = regex_match_group(url_string_node, regex)
|
|
|
|
next unless match_object
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-30 12:57:57 +05:30
|
|
|
offending_node(url_string_node.parent)
|
|
|
|
yield match_object, url_string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns all string nodes among the descendants of given node.
|
2017-05-22 13:09:49 +05:30
|
|
|
def find_strings(node)
|
|
|
|
return [] if node.nil?
|
2018-05-05 21:11:16 +05:30
|
|
|
return [node] if node.str_type?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-05-22 13:09:49 +05:30
|
|
|
node.each_descendant(:str)
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns method_node matching method_name.
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_node_method_by_name(node, method_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:send) do |method_node|
|
|
|
|
next unless method_node.method_name == method_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@offensive_node = method_node
|
|
|
|
@offense_source_range = method_node.source_range
|
|
|
|
return method_node
|
|
|
|
end
|
|
|
|
# If not found then, parent node becomes the offensive node
|
|
|
|
@offensive_node = node.parent
|
|
|
|
@offense_source_range = node.parent.source_range
|
|
|
|
nil
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Sets the given node as the offending node when required in custom cops.
|
2017-05-24 00:07:06 +05:30
|
|
|
def offending_node(node)
|
|
|
|
@offensive_node = node
|
|
|
|
@offense_source_range = node.source_range
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns an array of method call nodes matching method_name inside node with depth first order (child nodes).
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_method_calls_by_name(node, method_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:send).select { |method_node| method_name == method_node.method_name }
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns an array of method call nodes matching method_name in every descendant of node.
|
|
|
|
# Returns every method call if no method_name is passed.
|
2017-07-31 14:30:37 +05:30
|
|
|
def find_every_method_call_by_name(node, method_name = nil)
|
2017-05-24 00:07:06 +05:30
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-31 14:30:37 +05:30
|
|
|
node.each_descendant(:send).select do |method_node|
|
|
|
|
method_name.nil? ||
|
|
|
|
method_name == method_node.method_name
|
|
|
|
end
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns array of function call nodes matching func_name in every descendant of node.
|
|
|
|
#
|
2019-04-01 16:02:13 -04:00
|
|
|
# - matches function call: `foo(*args, **kwargs)`
|
|
|
|
# - does not match method calls: `foo.bar(*args, **kwargs)`
|
2020-11-05 17:17:03 -05:00
|
|
|
# - returns every function call if no func_name is passed
|
2018-04-10 03:22:28 +05:30
|
|
|
def find_every_func_call_by_name(node, func_name = nil)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-04-10 03:22:28 +05:30
|
|
|
node.each_descendant(:send).select do |func_node|
|
|
|
|
func_node.receiver.nil? && (func_name.nil? || func_name == func_node.method_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 01:22:47 +05:30
|
|
|
# Given a method_name and arguments, yields to a block with
|
2020-11-05 17:17:03 -05:00
|
|
|
# matching method passed as a parameter to the block.
|
2017-05-30 01:22:47 +05:30
|
|
|
def find_method_with_args(node, method_name, *args)
|
|
|
|
methods = find_every_method_call_by_name(node, method_name)
|
|
|
|
methods.each do |method|
|
2017-08-10 19:57:53 +05:30
|
|
|
next unless parameters_passed?(method, *args)
|
|
|
|
return true unless block_given?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-10 19:57:53 +05:30
|
|
|
yield method
|
2017-05-30 01:22:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 16:36:48 -05:00
|
|
|
# Matches a method with a receiver. Yields to a block with matching method node.
|
2018-10-18 21:42:43 -04:00
|
|
|
#
|
2020-11-03 16:36:48 -05:00
|
|
|
# @example to match `Formula.factory(name)`
|
|
|
|
# find_instance_method_call(node, "Formula", :factory)
|
|
|
|
# @example to match `build.head?`
|
|
|
|
# find_instance_method_call(node, :build, :head?)
|
2017-05-30 01:22:47 +05:30
|
|
|
def find_instance_method_call(node, instance, method_name)
|
|
|
|
methods = find_every_method_call_by_name(node, method_name)
|
|
|
|
methods.each do |method|
|
2017-08-04 01:18:00 +05:30
|
|
|
next if method.receiver.nil?
|
|
|
|
next if method.receiver.const_name != instance &&
|
2018-09-17 02:45:00 +02:00
|
|
|
!(method.receiver.send_type? && method.receiver.method_name == instance)
|
|
|
|
|
2017-05-30 01:22:47 +05:30
|
|
|
@offense_source_range = method.source_range
|
|
|
|
@offensive_node = method
|
2017-08-10 19:57:53 +05:30
|
|
|
return true unless block_given?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-05-30 01:22:47 +05:30
|
|
|
yield method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 16:36:48 -05:00
|
|
|
# Matches receiver part of method. Yields to a block with parent node of receiver.
|
2018-10-18 21:42:43 -04:00
|
|
|
#
|
2020-11-03 16:36:48 -05:00
|
|
|
# @example to match `ARGV.<whatever>()`
|
|
|
|
# find_instance_call(node, "ARGV")
|
2017-12-02 17:03:11 +05:30
|
|
|
def find_instance_call(node, name)
|
|
|
|
node.each_descendant(:send) do |method_node|
|
|
|
|
next if method_node.receiver.nil?
|
|
|
|
next if method_node.receiver.const_name != name &&
|
2018-09-17 02:45:00 +02:00
|
|
|
!(method_node.receiver.send_type? && method_node.receiver.method_name == name)
|
|
|
|
|
2017-12-02 17:03:11 +05:30
|
|
|
@offense_source_range = method_node.receiver.source_range
|
|
|
|
@offensive_node = method_node.receiver
|
|
|
|
return true unless block_given?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-12-02 17:03:11 +05:30
|
|
|
yield method_node
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns nil if does not depend on dependency_name.
|
2020-11-05 17:17:03 -05:00
|
|
|
#
|
2018-10-18 21:42:43 -04:00
|
|
|
# @param dependency_name dependency's name
|
2017-05-24 21:07:50 +05:30
|
|
|
def depends_on?(dependency_name, *types)
|
2018-01-07 14:08:58 +00:00
|
|
|
types = [:any] if types.empty?
|
2017-05-30 01:22:47 +05:30
|
|
|
dependency_nodes = find_every_method_call_by_name(@body, :depends_on)
|
2017-05-24 00:07:06 +05:30
|
|
|
idx = dependency_nodes.index do |n|
|
2017-05-24 21:07:50 +05:30
|
|
|
types.any? { |type| depends_on_name_type?(n, dependency_name, type) }
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
2017-05-30 01:22:47 +05:30
|
|
|
return if idx.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
@offense_source_range = dependency_nodes[idx].source_range
|
|
|
|
@offensive_node = dependency_nodes[idx]
|
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns true if given dependency name and dependency type exist in given dependency method call node.
|
2017-05-30 01:22:47 +05:30
|
|
|
# TODO: Add case where key of hash is an array
|
|
|
|
def depends_on_name_type?(node, name = nil, type = :required)
|
2020-03-13 21:15:06 +00:00
|
|
|
name_match = if name
|
|
|
|
false
|
2017-05-30 01:22:47 +05:30
|
|
|
else
|
2020-03-13 21:15:06 +00:00
|
|
|
true # Match only by type when name is nil
|
2017-05-30 01:22:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
case type
|
2017-05-24 00:07:06 +05:30
|
|
|
when :required
|
2017-08-06 14:48:39 +05:30
|
|
|
type_match = required_dependency?(node)
|
2018-01-07 14:08:58 +00:00
|
|
|
name_match ||= required_dependency_name?(node, name) if type_match
|
2018-03-19 10:11:08 +00:00
|
|
|
when :build, :test, :optional, :recommended
|
2017-08-06 14:48:39 +05:30
|
|
|
type_match = dependency_type_hash_match?(node, type)
|
2018-01-07 14:08:58 +00:00
|
|
|
name_match ||= dependency_name_hash_match?(node, name) if type_match
|
|
|
|
when :any
|
|
|
|
type_match = true
|
|
|
|
name_match ||= required_dependency_name?(node, name)
|
|
|
|
name_match ||= dependency_name_hash_match?(node, name)
|
2017-05-24 21:07:50 +05:30
|
|
|
else
|
|
|
|
type_match = false
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
2017-05-30 01:22:47 +05:30
|
|
|
|
|
|
|
if type_match || name_match
|
2017-05-24 00:07:06 +05:30
|
|
|
@offensive_node = node
|
|
|
|
@offense_source_range = node.source_range
|
|
|
|
end
|
2017-05-30 01:22:47 +05:30
|
|
|
type_match && name_match
|
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Find CONSTANTs in the source.
|
|
|
|
# If block given, yield matching nodes.
|
2017-08-10 19:57:53 +05:30
|
|
|
def find_const(node, const_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-15 00:42:56 +05:30
|
|
|
node.each_descendant(:const) do |const_node|
|
|
|
|
next unless const_node.const_name == const_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-10 19:57:53 +05:30
|
|
|
@offensive_node = const_node
|
|
|
|
@offense_source_range = const_node.source_range
|
|
|
|
yield const_node if block_given?
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
def_node_search :required_dependency?, <<~EOS
|
2017-10-21 03:12:50 +02:00
|
|
|
(send nil? :depends_on ({str sym} _))
|
2017-08-06 14:48:39 +05:30
|
|
|
EOS
|
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
def_node_search :required_dependency_name?, <<~EOS
|
2017-10-21 03:12:50 +02:00
|
|
|
(send nil? :depends_on ({str sym} %1))
|
2017-08-06 14:48:39 +05:30
|
|
|
EOS
|
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
def_node_search :dependency_type_hash_match?, <<~EOS
|
2017-08-06 14:48:39 +05:30
|
|
|
(hash (pair ({str sym} _) ({str sym} %1)))
|
|
|
|
EOS
|
|
|
|
|
2017-10-15 02:28:32 +02:00
|
|
|
def_node_search :dependency_name_hash_match?, <<~EOS
|
2018-01-09 20:08:22 +00:00
|
|
|
(hash (pair ({str sym} %1) (...)))
|
2017-08-06 14:48:39 +05:30
|
|
|
EOS
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# To compare node with appropriate Ruby variable.
|
2017-05-30 01:22:47 +05:30
|
|
|
def node_equals?(node, var)
|
|
|
|
node == Parser::CurrentRuby.parse(var.inspect)
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns a block named block_name inside node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_block(node, block_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:block) do |block_node|
|
|
|
|
next if block_node.method_name != block_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@offensive_node = block_node
|
|
|
|
@offense_source_range = block_node.source_range
|
|
|
|
return block_node
|
|
|
|
end
|
|
|
|
# If not found then, parent node becomes the offensive node
|
|
|
|
@offensive_node = node.parent
|
|
|
|
@offense_source_range = node.parent.source_range
|
|
|
|
nil
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns an array of block nodes named block_name inside node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_blocks(node, block_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:block).select { |block_node| block_name == block_node.method_name }
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns an array of block nodes of any depth below node in AST.
|
2017-07-31 14:30:37 +05:30
|
|
|
# If a block is given then yields matching block node to the block!
|
2017-06-01 00:57:24 +05:30
|
|
|
def find_all_blocks(node, block_name)
|
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-31 14:30:37 +05:30
|
|
|
blocks = node.each_descendant(:block).select { |block_node| block_name == block_node.method_name }
|
|
|
|
return blocks unless block_given?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-07-31 14:30:37 +05:30
|
|
|
blocks.each do |block_node|
|
|
|
|
offending_node(block_node)
|
|
|
|
yield block_node
|
|
|
|
end
|
2017-06-01 00:57:24 +05:30
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Returns a method definition node with method_name.
|
|
|
|
# Returns first method def if method_name is nil.
|
2017-08-10 19:57:53 +05:30
|
|
|
def find_method_def(node, method_name = nil)
|
2017-04-23 04:09:13 +05:30
|
|
|
return if node.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:def) do |def_node|
|
|
|
|
def_method_name = method_name(def_node)
|
2017-08-10 19:57:53 +05:30
|
|
|
next unless method_name == def_method_name || method_name.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@offensive_node = def_node
|
|
|
|
@offense_source_range = def_node.source_range
|
|
|
|
return def_node
|
|
|
|
end
|
2017-08-14 01:25:44 +05:30
|
|
|
return if node.parent.nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# If not found then, parent node becomes the offensive node
|
|
|
|
@offensive_node = node.parent
|
|
|
|
@offense_source_range = node.parent.source_range
|
|
|
|
nil
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Check if a method is called inside a block.
|
2017-04-23 04:09:13 +05:30
|
|
|
def method_called_in_block?(node, method_name)
|
|
|
|
block_body = node.children[2]
|
|
|
|
block_body.each_child_node(:send) do |call_node|
|
|
|
|
next unless call_node.method_name == method_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@offensive_node = call_node
|
|
|
|
@offense_source_range = call_node.source_range
|
|
|
|
return true
|
2017-03-16 23:49:43 +05:30
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
false
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# Check if method_name is called among the direct children nodes in the given node.
|
|
|
|
# Check if the node itself is the method.
|
2017-04-23 04:09:13 +05:30
|
|
|
def method_called?(node, method_name)
|
2017-08-14 02:14:20 +05:30
|
|
|
if node.send_type? && node.method_name == method_name
|
|
|
|
offending_node(node)
|
|
|
|
return true
|
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
node.each_child_node(:send) do |call_node|
|
|
|
|
next unless call_node.method_name == method_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-14 02:14:20 +05:30
|
|
|
offending_node(call_node)
|
2017-04-23 04:09:13 +05:30
|
|
|
return true
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
false
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Check if method_name is called among every descendant node of given node.
|
2017-05-24 00:07:06 +05:30
|
|
|
def method_called_ever?(node, method_name)
|
|
|
|
node.each_descendant(:send) do |call_node|
|
|
|
|
next unless call_node.method_name == method_name
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
@offensive_node = call_node
|
|
|
|
@offense_source_range = call_node.source_range
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Checks for precedence; returns the first pair of precedence-violating nodes.
|
2017-04-23 04:09:13 +05:30
|
|
|
def check_precedence(first_nodes, next_nodes)
|
|
|
|
next_nodes.each do |each_next_node|
|
|
|
|
first_nodes.each do |each_first_node|
|
2019-02-19 13:11:32 +00:00
|
|
|
return [each_first_node, each_next_node] if component_precedes?(each_first_node, each_next_node)
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
nil
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# If first node does not precede next_node, sets appropriate instance variables for reporting.
|
2017-04-23 04:09:13 +05:30
|
|
|
def component_precedes?(first_node, next_node)
|
|
|
|
return false if line_number(first_node) < line_number(next_node)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
@offense_source_range = first_node.source_range
|
|
|
|
@offensive_node = first_node
|
|
|
|
true
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Check if negation is present in the given node.
|
2017-10-25 16:05:29 +05:30
|
|
|
def expression_negated?(node)
|
2018-09-17 02:45:00 +02:00
|
|
|
return false unless node.parent&.send_type?
|
2017-10-25 16:05:29 +05:30
|
|
|
return false unless node.parent.method_name.equal?(:!)
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-10-25 16:05:29 +05:30
|
|
|
offending_node(node.parent)
|
2017-08-14 02:18:46 +05:30
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Return all the caveats' string nodes in an array.
|
2017-05-22 13:09:49 +05:30
|
|
|
def caveats_strings
|
|
|
|
find_strings(find_method_def(@body, :caveats))
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the array of arguments of the method_node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def parameters(method_node)
|
2017-10-07 22:31:23 +02:00
|
|
|
method_node.arguments if method_node.send_type? || method_node.block_type?
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# Returns true if the given parameters are present in method call
|
2018-10-18 21:42:43 -04:00
|
|
|
# and sets the method call as the offending node.
|
|
|
|
# Params can be string, symbol, array, hash, matching regex.
|
2017-05-24 00:07:06 +05:30
|
|
|
def parameters_passed?(method_node, *params)
|
|
|
|
method_params = parameters(method_node)
|
|
|
|
@offensive_node = method_node
|
|
|
|
@offense_source_range = method_node.source_range
|
|
|
|
params.all? do |given_param|
|
2017-05-30 01:22:47 +05:30
|
|
|
method_params.any? do |method_param|
|
2020-10-09 11:12:06 +02:00
|
|
|
if given_param.instance_of?(Regexp)
|
2017-05-30 01:22:47 +05:30
|
|
|
regex_match_group(method_param, given_param)
|
|
|
|
else
|
|
|
|
node_equals?(method_param, given_param)
|
|
|
|
end
|
|
|
|
end
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the sha256 str node given a sha256 call node.
|
2017-06-16 19:44:14 +05:30
|
|
|
def get_checksum_node(call)
|
|
|
|
return if parameters(call).empty? || parameters(call).nil?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-06-16 19:44:14 +05:30
|
|
|
if parameters(call).first.str_type?
|
|
|
|
parameters(call).first
|
|
|
|
# sha256 is passed as a key-value pair in bottle blocks
|
|
|
|
elsif parameters(call).first.hash_type?
|
|
|
|
parameters(call).first.keys.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Yields to a block with comment text as parameter.
|
2017-07-30 20:14:59 +05:30
|
|
|
def audit_comments
|
|
|
|
@processed_source.comments.each do |comment_node|
|
|
|
|
@offensive_node = comment_node
|
|
|
|
@offense_source_range = :expression
|
|
|
|
yield comment_node.text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the ending position of the node in source code.
|
2017-07-30 20:14:59 +05:30
|
|
|
def end_column(node)
|
|
|
|
node.source_range.end_pos
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the class node's name, or nil if not a class node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def class_name(node)
|
|
|
|
@offensive_node = node
|
|
|
|
@offense_source_range = node.source_range
|
|
|
|
node.const_name
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the method name for a def node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def method_name(node)
|
|
|
|
node.children[0] if node.def_type?
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the node size in the source code.
|
2017-04-23 04:09:13 +05:30
|
|
|
def size(node)
|
|
|
|
node.source_range.size
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the block length of the block node.
|
2017-04-23 04:09:13 +05:30
|
|
|
def block_size(block)
|
2018-01-07 15:40:42 +00:00
|
|
|
block.loc.end.line - block.loc.begin.line
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns true if the formula is versioned.
|
2017-05-24 13:04:55 +05:30
|
|
|
def versioned_formula?
|
2017-08-30 15:48:41 +05:30
|
|
|
@formula_name.include?("@")
|
2017-05-24 13:04:55 +05:30
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns printable component name.
|
2017-04-23 04:09:13 +05:30
|
|
|
def format_component(component_node)
|
|
|
|
return component_node.method_name if component_node.send_type? || component_node.block_type?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
method_name(component_node) if component_node.def_type?
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns the formula tap.
|
2017-08-04 01:18:00 +05:30
|
|
|
def formula_tap
|
|
|
|
return unless match_obj = @file_path.match(%r{/(homebrew-\w+)/})
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
match_obj[1]
|
|
|
|
end
|
|
|
|
|
2020-11-27 13:13:09 -05:00
|
|
|
# Returns whether the given formula exists in the given style exception list.
|
|
|
|
# Defaults to the current formula being checked.
|
|
|
|
def tap_style_exception?(list, formula = nil)
|
2020-11-27 01:23:07 -05:00
|
|
|
if @tap_style_exceptions.blank? && formula_tap.present?
|
|
|
|
@tap_style_exceptions = {}
|
|
|
|
|
|
|
|
style_exceptions_dir = "#{File.dirname(File.dirname(@file_path))}/style_exceptions/*.json"
|
|
|
|
Pathname.glob(style_exceptions_dir).each do |exception_file|
|
|
|
|
list_name = exception_file.basename.to_s.chomp(".json").to_sym
|
|
|
|
list_contents = begin
|
|
|
|
JSON.parse exception_file.read
|
|
|
|
rescue JSON::ParserError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
next if list_contents.blank?
|
|
|
|
|
|
|
|
@tap_style_exceptions[list_name] = list_contents
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false if @tap_style_exceptions.blank?
|
|
|
|
return false unless @tap_style_exceptions.key? list
|
|
|
|
|
2020-11-27 13:13:09 -05:00
|
|
|
@tap_style_exceptions[list].include?(formula || @formula_name)
|
2020-11-27 01:23:07 -05:00
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
private
|
2017-03-02 20:26:29 +05:30
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
def formula_class?(node)
|
|
|
|
_, class_node, = *node
|
2017-09-04 13:47:05 +05:30
|
|
|
class_names = %w[
|
|
|
|
Formula
|
|
|
|
GithubGistFormula
|
|
|
|
ScriptFileFormula
|
|
|
|
AmazonWebServicesFormula
|
|
|
|
]
|
|
|
|
|
|
|
|
class_node && class_names.include?(string_content(class_node))
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
def file_path_allowed?
|
2017-04-23 04:09:13 +05:30
|
|
|
paths_to_exclude = [%r{/Library/Homebrew/compat/},
|
|
|
|
%r{/Library/Homebrew/test/}]
|
2019-04-08 12:47:15 -04:00
|
|
|
return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-08-04 01:18:00 +05:30
|
|
|
@file_path !~ Regexp.union(paths_to_exclude)
|
2017-03-02 20:26:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|