2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2012-11-24 11:36:47 -05:00
|
|
|
# Matcher for subjects that are a specific method
|
2012-08-09 17:07:22 -04:00
|
|
|
class Method < self
|
2013-06-27 16:18:07 -04:00
|
|
|
include Adamantium::Flat, Concord::Public.new(:cache, :scope, :method)
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2013-04-17 23:31:21 -04:00
|
|
|
# Methods within rbx kernel directory are precompiled and their source
|
2013-01-03 17:32:17 -05:00
|
|
|
# cannot be accessed via reading source location
|
2013-07-28 15:16:45 -04:00
|
|
|
SKIP_METHODS = %w[kernel/ (eval)].freeze
|
|
|
|
BLACKLIST = /\A#{Regexp.union(*SKIP_METHODS)}/.freeze
|
2013-01-03 17:32:17 -05:00
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Enumerate matches
|
|
|
|
#
|
2013-07-04 18:54:50 -04:00
|
|
|
# @return [Enumerable<Subject>]
|
2012-07-23 16:54:35 -04:00
|
|
|
# returns enumerable when no block given
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# returns self when block given
|
|
|
|
#
|
|
|
|
# @api private
|
2013-04-17 23:31:21 -04:00
|
|
|
#
|
2013-06-27 16:18:07 -04:00
|
|
|
def each
|
2012-07-29 16:44:53 -04:00
|
|
|
return to_enum unless block_given?
|
2012-12-07 17:27:21 -05:00
|
|
|
|
2013-06-27 16:18:07 -04:00
|
|
|
unless skip?
|
2013-07-02 18:32:48 -04:00
|
|
|
if subject
|
|
|
|
yield subject
|
|
|
|
else
|
2013-07-28 18:34:25 -04:00
|
|
|
message = sprintf(
|
2013-07-28 14:07:57 -04:00
|
|
|
'Cannot find definition of: %s in %s',
|
|
|
|
identification,
|
|
|
|
source_location.join(':')
|
|
|
|
)
|
|
|
|
$stderr.puts(message)
|
2013-07-02 18:32:48 -04:00
|
|
|
end
|
2013-06-27 16:18:07 -04:00
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-10-26 05:24:29 -04:00
|
|
|
private
|
|
|
|
|
2013-01-03 17:32:17 -05:00
|
|
|
# Test if method is skipped
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# true and print warning if location must be filtered
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def skip?
|
|
|
|
location = source_location
|
|
|
|
if location.nil? or BLACKLIST.match(location.first)
|
2013-07-28 14:07:57 -04:00
|
|
|
message = sprintf(
|
|
|
|
'%s does not have valid source location unable to emit matcher',
|
|
|
|
method.inspect
|
|
|
|
)
|
|
|
|
$stderr.puts(message)
|
2013-01-03 17:32:17 -05:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2013-04-27 11:43:17 -04:00
|
|
|
# Return method name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def method_name
|
|
|
|
method.name
|
|
|
|
end
|
|
|
|
|
2012-12-07 17:27:21 -05:00
|
|
|
# Return context
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
# @return [Context::Scope]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-07 17:27:21 -05:00
|
|
|
def context
|
2012-12-12 16:11:35 -05:00
|
|
|
Context::Scope.new(scope, source_path)
|
2012-12-07 17:27:21 -05:00
|
|
|
end
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return full ast
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Node]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def ast
|
2013-06-27 16:18:07 -04:00
|
|
|
cache.parse(source_path)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
2012-08-14 06:26:56 -04:00
|
|
|
# Return path to source
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 06:26:56 -04:00
|
|
|
def source_path
|
2012-07-23 16:54:35 -04:00
|
|
|
source_location.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source file line
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-14 16:45:34 -04:00
|
|
|
def source_line
|
2012-07-23 16:54:35 -04:00
|
|
|
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-07-23 19:41:08 -04:00
|
|
|
#
|
2012-08-01 12:34:03 -04:00
|
|
|
# @return [Subject]
|
|
|
|
# returns subject if there is a matched node
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-01 12:34:03 -04:00
|
|
|
def subject
|
2012-07-26 13:25:23 -04:00
|
|
|
node = matched_node
|
2012-08-15 22:10:54 -04:00
|
|
|
return unless node
|
2013-01-14 06:54:31 -05:00
|
|
|
self.class::SUBJECT_CLASS.new(context, node)
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
2012-08-01 12:34:03 -04:00
|
|
|
memoize :subject
|
2012-08-16 13:10:24 -04:00
|
|
|
|
|
|
|
# Return matched node
|
|
|
|
#
|
2013-06-04 13:22:33 -04:00
|
|
|
# @return [Parser::AST::Node]
|
|
|
|
# if node could be found
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
2012-08-16 13:10:24 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def matched_node
|
2013-06-04 13:22:33 -04:00
|
|
|
Finder.run(ast) do |node|
|
|
|
|
match?(node)
|
2012-08-16 13:10:24 -04:00
|
|
|
end
|
|
|
|
end
|
2013-04-27 11:43:17 -04:00
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Method
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|