2012-07-26 13:25:23 -04:00
|
|
|
module Mutant
|
2012-07-30 22:10:37 -04:00
|
|
|
class Context
|
2012-08-15 22:10:54 -04:00
|
|
|
# Scope context for mutation (Class or Module)
|
|
|
|
class Scope < self
|
2012-12-12 16:11:35 -05:00
|
|
|
include Adamantium::Flat, Equalizer.new(:scope, :source_path)
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-12-12 16:11:35 -05:00
|
|
|
# Return AST wrapping mutated node
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Node]
|
2012-12-12 16:11:35 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def root(node)
|
|
|
|
nesting.reverse.inject(node) do |current, scope|
|
|
|
|
self.class.wrap(scope, current)
|
|
|
|
end
|
2012-08-14 06:26:56 -04:00
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2013-01-13 16:27:03 -05:00
|
|
|
# Return identification
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @ai private
|
|
|
|
#
|
|
|
|
def identification
|
|
|
|
scope.name
|
|
|
|
end
|
|
|
|
|
2012-12-12 16:11:35 -05:00
|
|
|
# Wrap node into ast node
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2013-04-17 23:31:21 -04:00
|
|
|
# @param [Class, Module] scope
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Class]
|
2012-12-12 16:11:35 -05:00
|
|
|
# if scope is of kind Class
|
2012-08-14 06:26:56 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Module]
|
2012-12-12 16:11:35 -05:00
|
|
|
# if scope is of kind module
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-12 16:11:35 -05:00
|
|
|
def self.wrap(scope, node)
|
2012-12-29 14:12:48 -05:00
|
|
|
name = scope.name.split('::').last.to_sym
|
2012-12-12 16:11:35 -05:00
|
|
|
case scope
|
|
|
|
when ::Class
|
2012-12-29 14:12:48 -05:00
|
|
|
::Rubinius::AST::Class.new(0, name, nil, node)
|
2012-12-12 16:11:35 -05:00
|
|
|
when ::Module
|
2012-12-29 14:12:48 -05:00
|
|
|
::Rubinius::AST::Module.new(0, name, node)
|
2012-12-12 16:11:35 -05:00
|
|
|
else
|
|
|
|
raise "Cannot wrap scope: #{scope.inspect}"
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-12-12 16:11:35 -05:00
|
|
|
# Return nesting
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2012-12-12 16:11:35 -05:00
|
|
|
# @return [Enumerable<Class,Module>]
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-12 16:11:35 -05:00
|
|
|
def nesting
|
|
|
|
const = ::Object
|
|
|
|
name_nesting.each_with_object([]) do |name, nesting|
|
|
|
|
const = const.const_get(name)
|
|
|
|
nesting << const
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-12-12 16:11:35 -05:00
|
|
|
memoize :nesting
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return unqualified name of scope
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2012-07-30 22:10:37 -04:00
|
|
|
# @return [String]
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def unqualified_name
|
|
|
|
name_nesting.last
|
|
|
|
end
|
|
|
|
|
2013-01-21 17:54:25 -05:00
|
|
|
# Return name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
scope.name
|
|
|
|
end
|
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return scope wrapped by context
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [::Module|::Class]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-10-26 05:24:29 -04:00
|
|
|
attr_reader :scope
|
2012-08-20 11:53:41 -04:00
|
|
|
|
|
|
|
private
|
2012-07-26 13:25:23 -04:00
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
2012-08-15 22:10:54 -04:00
|
|
|
# @param [Object] scope
|
|
|
|
# @param [String] source_path
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-15 22:10:54 -04:00
|
|
|
def initialize(scope, source_path)
|
|
|
|
super(source_path)
|
|
|
|
@scope = scope
|
2012-08-14 06:26:56 -04:00
|
|
|
end
|
|
|
|
|
2012-07-30 22:10:37 -04:00
|
|
|
# Return new root ast
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Node]
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def root_ast
|
2013-01-21 17:54:25 -05:00
|
|
|
"#{keyword} #{name}; end".to_ast
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return nesting of names of scope
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def name_nesting
|
2012-10-26 05:24:29 -04:00
|
|
|
scope.name.split('::')
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
2012-10-26 05:24:29 -04:00
|
|
|
memoize :name_nesting
|
2013-06-04 04:25:13 -04:00
|
|
|
|
|
|
|
end # Scope
|
|
|
|
end # Context
|
|
|
|
end # Mutant
|