2017-05-30 01:22:47 +05:30
|
|
|
require "parser/current"
|
|
|
|
|
2017-03-02 20:26:29 +05:30
|
|
|
module RuboCop
|
|
|
|
module Cop
|
2017-04-23 04:09:13 +05:30
|
|
|
class FormulaCop < Cop
|
|
|
|
@registry = Cop.registry
|
|
|
|
|
|
|
|
# This method is called by RuboCop and is the main entry point
|
|
|
|
def on_class(node)
|
|
|
|
file_path = processed_source.buffer.name
|
|
|
|
return unless file_path_allowed?(file_path)
|
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)
|
2017-05-30 01:22:47 +05:30
|
|
|
class_node, parent_class_node, @body = *node
|
2017-04-23 04:09:13 +05:30
|
|
|
@formula_name = class_name(class_node)
|
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
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Checks for regex match of pattern in the node and
|
|
|
|
# Sets the appropriate instance variables to report the match
|
|
|
|
def regex_match_group(node, pattern)
|
|
|
|
string_repr = string_content(node)
|
|
|
|
match_object = string_repr.match(pattern)
|
|
|
|
return unless match_object
|
|
|
|
node_begin_pos = start_column(node)
|
|
|
|
line_begin_pos = line_start_column(node)
|
2017-05-22 13:09:49 +05:30
|
|
|
if node_begin_pos == line_begin_pos
|
|
|
|
@column = node_begin_pos + match_object.begin(0) - line_begin_pos
|
|
|
|
else
|
|
|
|
@column = node_begin_pos + match_object.begin(0) - line_begin_pos + 1
|
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
@length = match_object.to_s.length
|
|
|
|
@line_no = line_number(node)
|
|
|
|
@source_buf = source_buffer(node)
|
|
|
|
@offense_source_range = source_range(@source_buf, @line_no, @column, @length)
|
|
|
|
@offensive_node = node
|
|
|
|
match_object
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-05-22 13:09:49 +05:30
|
|
|
# Returns all string nodes among the descendants of given node
|
|
|
|
def find_strings(node)
|
|
|
|
return [] if node.nil?
|
|
|
|
node.each_descendant(:str)
|
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns method_node matching method_name
|
|
|
|
def find_node_method_by_name(node, method_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_child_node(:send) do |method_node|
|
|
|
|
next unless method_node.method_name == method_name
|
|
|
|
@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
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# Set the given node as the offending node when required in custom cops
|
|
|
|
def offending_node(node)
|
|
|
|
@offensive_node = node
|
|
|
|
@offense_source_range = node.source_range
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of method call nodes matching method_name inside node with depth first order (Children nodes)
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_method_calls_by_name(node, method_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_child_node(:send).select { |method_node| method_name == method_node.method_name }
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# 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)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_descendant(:send).select { |method_node| method_name == method_node.method_name }
|
|
|
|
end
|
|
|
|
|
2017-05-30 01:22:47 +05:30
|
|
|
# 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)
|
|
|
|
methods = find_every_method_call_by_name(node, method_name)
|
|
|
|
methods.each do |method|
|
|
|
|
next unless parameters_passed?(method, *args)
|
|
|
|
yield method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Matches a method with a receiver,
|
|
|
|
# EX: to match `Formula.factory(name)`
|
|
|
|
# call `find_instance_method_call(node, "Formula", :factory)`
|
|
|
|
# yields to a block with matching method node
|
|
|
|
def find_instance_method_call(node, instance, method_name)
|
|
|
|
methods = find_every_method_call_by_name(node, method_name)
|
|
|
|
methods.each do |method|
|
|
|
|
next unless method.receiver.const_name == instance
|
|
|
|
@offense_source_range = method.source_range
|
|
|
|
@offensive_node = method
|
|
|
|
yield method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# Returns nil if does not depend on dependency_name
|
2017-05-30 01:22:47 +05:30
|
|
|
# args: node - dependency_name - dependency's name
|
|
|
|
def depends_on?(dependency_name)
|
|
|
|
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|
|
|
|
|
depends_on_name_type?(n, dependency_name, :required) ||
|
|
|
|
depends_on_name_type?(n, dependency_name, :build) ||
|
|
|
|
depends_on_name_type?(n, dependency_name, :optional) ||
|
|
|
|
depends_on_name_type?(n, dependency_name, :recommended) ||
|
|
|
|
depends_on_name_type?(n, dependency_name, :run)
|
|
|
|
end
|
2017-05-30 01:22:47 +05:30
|
|
|
return if idx.nil?
|
2017-05-24 00:07:06 +05:30
|
|
|
@offense_source_range = dependency_nodes[idx].source_range
|
|
|
|
@offensive_node = dependency_nodes[idx]
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
if name
|
|
|
|
name_match = false
|
|
|
|
else
|
|
|
|
name_match = true # Match only by type when name is nil
|
|
|
|
end
|
|
|
|
|
|
|
|
case type
|
2017-05-24 00:07:06 +05:30
|
|
|
when :required
|
2017-05-30 01:22:47 +05:30
|
|
|
type_match = !node.method_args.nil? && node.method_args.first.str_type?
|
|
|
|
if type_match && !name_match
|
|
|
|
name_match = node_equals?(node.method_args.first, name)
|
|
|
|
end
|
|
|
|
when :build, :optional, :recommended, :run
|
|
|
|
type_match = !node.method_args.nil? &&
|
|
|
|
node.method_args.first.hash_type? &&
|
|
|
|
node.method_args.first.values.first.children.first == type
|
|
|
|
if type_match && !name_match
|
|
|
|
name_match = node_equals?(node.method_args.first.keys.first.children.first, name)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
|
# To compare node with appropriate Ruby variable
|
|
|
|
def node_equals?(node, var)
|
|
|
|
node == Parser::CurrentRuby.parse(var.inspect)
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns a block named block_name inside node
|
|
|
|
def find_block(node, block_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_child_node(:block) do |block_node|
|
|
|
|
next if block_node.method_name != block_name
|
|
|
|
@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
|
|
|
|
2017-06-01 00:52:49 +05:30
|
|
|
# Returns an array of block nodes of depth first order named block_name below node
|
2017-04-23 04:09:13 +05:30
|
|
|
def find_blocks(node, block_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_child_node(:block).select { |block_node| block_name == block_node.method_name }
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-06-01 00:52:49 +05:30
|
|
|
# Returns an array of block nodes of any depth below node in AST
|
|
|
|
def find_all_blocks(node, block_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_descendant(:block).select { |block_node| block_name == block_node.method_name}
|
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns a method definition node with method_name
|
|
|
|
def find_method_def(node, method_name)
|
|
|
|
return if node.nil?
|
|
|
|
node.each_child_node(:def) do |def_node|
|
|
|
|
def_method_name = method_name(def_node)
|
|
|
|
next unless method_name == def_method_name
|
|
|
|
@offensive_node = def_node
|
|
|
|
@offense_source_range = def_node.source_range
|
|
|
|
return def_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-04-08 15:10:44 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Check if a method is called inside a block
|
|
|
|
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
|
|
|
|
@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
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Check if method_name is called among the direct children nodes in the given node
|
|
|
|
def method_called?(node, method_name)
|
|
|
|
node.each_child_node(:send) do |call_node|
|
|
|
|
next unless call_node.method_name == method_name
|
|
|
|
@offensive_node = call_node
|
|
|
|
@offense_source_range = call_node.source_range
|
|
|
|
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
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# Check if method_name is called among every descendant node of given node
|
|
|
|
def method_called_ever?(node, method_name)
|
|
|
|
node.each_descendant(:send) do |call_node|
|
|
|
|
next unless call_node.method_name == method_name
|
|
|
|
@offensive_node = call_node
|
|
|
|
@offense_source_range = call_node.source_range
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Checks for precedence, returns the first pair of precedence violating nodes
|
|
|
|
def check_precedence(first_nodes, next_nodes)
|
|
|
|
next_nodes.each do |each_next_node|
|
|
|
|
first_nodes.each do |each_first_node|
|
|
|
|
if component_precedes?(each_first_node, each_next_node)
|
|
|
|
return [each_first_node, each_next_node]
|
2017-04-08 15:10:44 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
nil
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# If first node does not precede next_node, sets appropriate instance variables for reporting
|
|
|
|
def component_precedes?(first_node, next_node)
|
|
|
|
return false if line_number(first_node) < line_number(next_node)
|
|
|
|
@offense_source_range = first_node.source_range
|
|
|
|
@offensive_node = first_node
|
|
|
|
true
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-05-22 13:09:49 +05:30
|
|
|
# Return all the caveats' string nodes in an array
|
|
|
|
def caveats_strings
|
|
|
|
find_strings(find_method_def(@body, :caveats))
|
|
|
|
end
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the array of arguments of the method_node
|
|
|
|
def parameters(method_node)
|
2017-06-01 00:52:49 +05:30
|
|
|
return unless method_node.send_type? || method_node.block_type?
|
2017-04-23 04:09:13 +05:30
|
|
|
method_node.method_args
|
|
|
|
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
|
|
|
|
# and sets the method call as the offending node
|
2017-05-30 01:22:47 +05:30
|
|
|
# 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|
|
|
|
|
if given_param.class == Regexp
|
|
|
|
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
|
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the begin position of the node's line in source code
|
|
|
|
def line_start_column(node)
|
|
|
|
node.source_range.source_buffer.line_range(node.loc.line).begin_pos
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the begin position of the node in source code
|
|
|
|
def start_column(node)
|
|
|
|
node.source_range.begin_pos
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the line number of the node
|
|
|
|
def line_number(node)
|
|
|
|
node.loc.line
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the class node's name, nil if not a class node
|
|
|
|
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
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the method name for a def node
|
|
|
|
def method_name(node)
|
|
|
|
node.children[0] if node.def_type?
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the node size in the source code
|
|
|
|
def size(node)
|
|
|
|
node.source_range.size
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns the block length of the block node
|
|
|
|
def block_size(block)
|
|
|
|
block_length(block)
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Source buffer is required as an argument to report style violations
|
|
|
|
def source_buffer(node)
|
|
|
|
node.source_range.source_buffer
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-05-24 00:07:06 +05:30
|
|
|
# Returns the string representation if node is of type str(plain) or dstr(interpolated) or const
|
2017-04-23 04:09:13 +05:30
|
|
|
def string_content(node)
|
2017-05-24 00:07:06 +05:30
|
|
|
case node.type
|
|
|
|
when :str
|
|
|
|
return node.str_content if node.type == :str
|
|
|
|
when :dstr
|
|
|
|
return node.each_child_node(:str).map(&:str_content).join("") if node.type == :dstr
|
|
|
|
when :const
|
|
|
|
return node.const_name if node.type == :const
|
2017-05-22 13:09:49 +05:30
|
|
|
else
|
|
|
|
""
|
2017-05-24 00:07:06 +05:30
|
|
|
end
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
# Returns printable component name
|
|
|
|
def format_component(component_node)
|
|
|
|
return component_node.method_name if component_node.send_type? || component_node.block_type?
|
|
|
|
method_name(component_node) if component_node.def_type?
|
|
|
|
end
|
2017-04-08 15:10:44 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
def problem(msg)
|
|
|
|
add_offense(@offensive_node, @offense_source_range, msg)
|
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
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
|
|
|
|
class_node && string_content(class_node) == "Formula"
|
2017-04-23 04:09:13 +05:30
|
|
|
end
|
2017-03-16 23:49:43 +05:30
|
|
|
|
2017-04-23 04:09:13 +05:30
|
|
|
def file_path_allowed?(file_path)
|
|
|
|
paths_to_exclude = [%r{/Library/Homebrew/compat/},
|
|
|
|
%r{/Library/Homebrew/test/}]
|
|
|
|
return true if file_path.nil? # file_path is nil when source is directly passed to the cop eg., in specs
|
|
|
|
file_path !~ Regexp.union(paths_to_exclude)
|
2017-03-02 20:26:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|