2012-07-23 16:54:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2012-07-26 13:25:23 -04:00
|
|
|
# Matcher to find AST for method
|
2012-07-23 16:54:35 -04:00
|
|
|
class Method < Matcher
|
2012-08-02 18:19:01 -04:00
|
|
|
include Immutable
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Parse a method string into filter
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
#
|
|
|
|
# @return [Matcher::Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.parse(input)
|
2012-07-23 16:57:29 -04:00
|
|
|
Classifier.run(input)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Enumerate matches
|
|
|
|
#
|
|
|
|
# @return [Enumerable]
|
|
|
|
# returns enumerable when no block given
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# returns self when block given
|
|
|
|
#
|
|
|
|
# @api private
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
def each(&block)
|
2012-07-29 16:44:53 -04:00
|
|
|
return to_enum unless block_given?
|
2012-08-01 12:34:03 -04:00
|
|
|
subject.tap do |subject|
|
|
|
|
yield subject if subject
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return context of matcher
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
# @return [Context]
|
|
|
|
# returns the context this matcher matches AST nodes
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @api private
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
def context
|
|
|
|
Context::Constant.build(constant)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-07-23 19:41:08 -04:00
|
|
|
|
|
|
|
# Return method name
|
|
|
|
#
|
|
|
|
# @return [String]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
attr_reader :method_name
|
|
|
|
private :method_name
|
2012-07-23 16:54:35 -04:00
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return constant name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :constant_name
|
|
|
|
private :constant_name
|
|
|
|
|
|
|
|
|
2012-07-23 19:41:08 -04:00
|
|
|
# Initialize method filter
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @param [String] constant_name
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize(constant_name, method_name)
|
2012-07-30 22:10:37 -04:00
|
|
|
@constant_name, @method_name = constant_name, method_name
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return method
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2012-07-23 16:54:35 -04:00
|
|
|
# @return [UnboundMethod]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
abstract :method
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return node classes this matcher matches
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @return [Rubinius::AST::Node]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
abstract :node_class
|
2012-07-23 16:54:35 -04:00
|
|
|
|
2012-07-30 22:10:37 -04:00
|
|
|
# Check if node is matched
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
|
|
|
# @param [Rubinius::AST::Node] node
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# returns true if node matches method
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @return [false]
|
|
|
|
# returns false if node NOT matches method
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
def match?(node)
|
2012-07-30 22:10:37 -04:00
|
|
|
node.line == source_file_line &&
|
|
|
|
node.class == node_class &&
|
2012-07-23 19:41:08 -04:00
|
|
|
node.name == method_name
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return full ast
|
|
|
|
#
|
|
|
|
# @return [Rubinius::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def ast
|
|
|
|
File.read(source_filename).to_ast
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source filename
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_filename
|
|
|
|
source_location.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source file line
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_file_line
|
|
|
|
source_location.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source location
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_location
|
|
|
|
method.source_location
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return matched node
|
|
|
|
#
|
|
|
|
# @return [Rubinis::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:10:37 -04:00
|
|
|
abstract :matched_node
|
2012-07-26 13:25:23 -04:00
|
|
|
|
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
|
|
|
|
if node
|
2012-08-01 12:34:03 -04:00
|
|
|
Subject.new(context, node)
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Return constant
|
|
|
|
#
|
|
|
|
# @return [Class|Module]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def constant
|
2012-07-30 22:10:37 -04:00
|
|
|
constant_name.split('::').inject(::Object) do |parent, name|
|
2012-07-26 13:25:23 -04:00
|
|
|
parent.const_get(name)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-01 12:34:03 -04:00
|
|
|
memoize :subject
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|