free_mutant/lib/mutant/matcher/method.rb

146 lines
2.8 KiB
Ruby
Raw Normal View History

module Mutant
class Matcher
# Matcher for subjects that are a specific method
2012-08-09 17:07:22 -04:00
class Method < self
include Adamantium::Flat, Concord::Public.new(:env, :scope, :method)
include Equalizer.new(:identification)
2013-04-17 23:31:21 -04:00
# Methods within rbx kernel directory are precompiled and their source
# cannot be accessed via reading source location. Same for methods created by eval.
BLACKLIST = %r{\Akernel/|(eval)}.freeze
# Enumerate matches
#
# @return [Enumerable<Subject>]
# if no block given
#
# @return [self]
# otherwise
#
# @api private
2013-04-17 23:31:21 -04:00
#
def each
return to_enum unless block_given?
2012-12-07 17:27:21 -05:00
if !skip? && subject
yield subject
end
self
end
private
2014-06-15 15:27:57 -04:00
# Test if method should be skipped
#
2014-06-15 15:27:57 -04:00
# @return [Boolean]
#
# @api private
#
def skip?
location = source_location
if location.nil? || BLACKLIST.match(location.first)
env.warn(format('%s does not have valid source location unable to emit matcher', method.inspect))
true
else
false
end
end
# Return method name
#
# @return [String]
#
# @api private
#
def method_name
method.name
end
2012-12-07 17:27:21 -05:00
# Return context
#
2012-12-07 17:27:21 -05:00
# @return [Context::Scope]
#
# @api private
#
2012-12-07 17:27:21 -05:00
def context
Context::Scope.new(scope, source_path)
2012-12-07 17:27:21 -05:00
end
# Return full ast
#
# @return [Parser::AST::Node]
#
# @api private
#
def ast
env.cache.parse(source_path)
end
2012-08-14 06:26:56 -04:00
# Return path to source
#
# @return [String]
#
# @api private
#
2012-08-14 06:26:56 -04:00
def source_path
source_location.first
end
# Return source file line
#
# @return [Integer]
#
# @api private
#
2012-08-14 16:45:34 -04:00
def source_line
source_location.last
end
# Return source location
#
# @return [Array]
#
# @api private
#
def source_location
method.source_location
end
2012-08-01 12:34:03 -04:00
# Return subject
#
2012-08-01 12:34:03 -04:00
# @return [Subject]
# if there is a matched node
2012-08-01 12:34:03 -04:00
#
# @return [nil]
# otherwise
#
# @api private
#
2012-08-01 12:34:03 -04:00
def subject
node = matched_node
return unless node
self.class::SUBJECT_CLASS.new(env.config, context, node)
end
2012-08-01 12:34:03 -04:00
memoize :subject
# Return matched node
#
# @return [Parser::AST::Node]
# if node could be found
#
# @return [nil]
# otherwise
#
# @api private
#
def matched_node
2014-07-14 09:38:48 -04:00
AST.find_last(ast) do |node|
match?(node)
end
end
end # Method
end # Matcher
end # Mutant