2012-07-23 22:54:35 +02:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2012-11-24 17:36:47 +01:00
|
|
|
# Matcher for subjects that are a specific method
|
2012-08-09 23:07:22 +02:00
|
|
|
class Method < self
|
2014-09-16 18:39:27 +00:00
|
|
|
include Adamantium::Flat, Concord::Public.new(:env, :scope, :target_method)
|
2014-09-16 19:30:45 +00:00
|
|
|
include AST::NodePredicates, Equalizer.new(:identification)
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2013-04-17 20:31:21 -07:00
|
|
|
# Methods within rbx kernel directory are precompiled and their source
|
2014-06-15 19:16:57 +00:00
|
|
|
# cannot be accessed via reading source location. Same for methods created by eval.
|
|
|
|
BLACKLIST = %r{\Akernel/|(eval)}.freeze
|
2013-01-03 23:32:17 +01:00
|
|
|
|
2012-07-23 22:54:35 +02:00
|
|
|
# Enumerate matches
|
|
|
|
#
|
2013-07-05 00:54:50 +02:00
|
|
|
# @return [Enumerable<Subject>]
|
2013-09-11 20:48:44 +02:00
|
|
|
# if no block given
|
2012-07-23 22:54:35 +02:00
|
|
|
#
|
|
|
|
# @return [self]
|
2013-09-11 20:48:44 +02:00
|
|
|
# otherwise
|
2012-07-23 22:54:35 +02:00
|
|
|
#
|
|
|
|
# @api private
|
2013-04-17 20:31:21 -07:00
|
|
|
#
|
2013-06-27 22:18:07 +02:00
|
|
|
def each
|
2012-07-29 22:44:53 +02:00
|
|
|
return to_enum unless block_given?
|
2012-12-07 23:27:21 +01:00
|
|
|
|
2014-05-10 11:41:04 +00:00
|
|
|
if !skip? && subject
|
|
|
|
yield subject
|
2013-06-27 22:18:07 +02:00
|
|
|
end
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2012-07-23 22:54:35 +02:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-10-26 11:24:29 +02:00
|
|
|
private
|
|
|
|
|
2014-06-15 19:27:57 +00:00
|
|
|
# Test if method should be skipped
|
2013-01-03 23:32:17 +01:00
|
|
|
#
|
2014-06-15 19:27:57 +00:00
|
|
|
# @return [Boolean]
|
2013-01-03 23:32:17 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def skip?
|
|
|
|
location = source_location
|
2013-07-30 02:14:27 -07:00
|
|
|
if location.nil? || BLACKLIST.match(location.first)
|
2014-09-16 19:30:45 +00:00
|
|
|
env.warn(format('%s does not have valid source location unable to emit subject', target_method.inspect))
|
|
|
|
true
|
|
|
|
elsif matched_node_path.any?(&method(:n_block?))
|
|
|
|
env.warn(format('%s is defined from a 3rd party lib unable to emit subject', target_method.inspect))
|
2013-07-30 02:14:27 -07:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
2013-01-03 23:32:17 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-27 17:43:17 +02:00
|
|
|
# Return method name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def method_name
|
2014-09-16 18:39:27 +00:00
|
|
|
target_method.name
|
2013-04-27 17:43:17 +02:00
|
|
|
end
|
|
|
|
|
2012-12-07 23:27:21 +01:00
|
|
|
# Return context
|
2012-07-31 04:10:37 +02:00
|
|
|
#
|
2012-12-07 23:27:21 +01:00
|
|
|
# @return [Context::Scope]
|
2012-07-23 22:54:35 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-07 23:27:21 +01:00
|
|
|
def context
|
2012-12-12 22:11:35 +01:00
|
|
|
Context::Scope.new(scope, source_path)
|
2012-12-07 23:27:21 +01:00
|
|
|
end
|
2012-07-23 22:54:35 +02:00
|
|
|
|
|
|
|
# Return full ast
|
|
|
|
#
|
2013-06-04 10:25:13 +02:00
|
|
|
# @return [Parser::AST::Node]
|
2012-07-23 22:54:35 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def ast
|
2014-06-30 13:06:57 +00:00
|
|
|
env.cache.parse(source_path)
|
2012-07-23 22:54:35 +02:00
|
|
|
end
|
|
|
|
|
2012-08-14 12:26:56 +02:00
|
|
|
# Return path to source
|
2012-07-23 22:54:35 +02:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 12:26:56 +02:00
|
|
|
def source_path
|
2012-07-23 22:54:35 +02:00
|
|
|
source_location.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source file line
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 22:45:34 +02:00
|
|
|
def source_line
|
2012-07-23 22:54:35 +02:00
|
|
|
source_location.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source location
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_location
|
2014-09-16 18:39:27 +00:00
|
|
|
target_method.source_location
|
2012-07-23 22:54:35 +02:00
|
|
|
end
|
|
|
|
|
2012-08-01 18:34:03 +02:00
|
|
|
# Return subject
|
2012-07-24 01:41:08 +02:00
|
|
|
#
|
2012-08-01 18:34:03 +02:00
|
|
|
# @return [Subject]
|
2013-09-11 20:48:44 +02:00
|
|
|
# if there is a matched node
|
2012-08-01 18:34:03 +02:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-07-24 01:41:08 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-01 18:34:03 +02:00
|
|
|
def subject
|
2014-09-16 18:39:27 +00:00
|
|
|
node = matched_node_path.last
|
2012-08-16 04:10:54 +02:00
|
|
|
return unless node
|
2014-07-03 21:16:12 +00:00
|
|
|
self.class::SUBJECT_CLASS.new(env.config, context, node)
|
2012-07-24 01:41:08 +02:00
|
|
|
end
|
2012-08-01 18:34:03 +02:00
|
|
|
memoize :subject
|
2012-08-16 19:10:24 +02:00
|
|
|
|
2014-09-16 18:39:27 +00:00
|
|
|
# Return matched node path
|
2012-08-16 19:10:24 +02:00
|
|
|
#
|
2014-09-16 18:39:27 +00:00
|
|
|
# @return [Array<Parser::AST::Node>]
|
2012-08-16 19:10:24 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2014-09-16 18:39:27 +00:00
|
|
|
def matched_node_path
|
|
|
|
AST.find_last_path(ast, &method(:match?))
|
2012-08-16 19:10:24 +02:00
|
|
|
end
|
2014-09-16 18:39:27 +00:00
|
|
|
memoize :matched_node_path
|
2013-04-27 17:43:17 +02:00
|
|
|
|
2013-06-04 10:25:13 +02:00
|
|
|
end # Method
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|