[ruby/yarp] Move node inspector into its own file
https://github.com/ruby/yarp/commit/1c843d2f22
This commit is contained in:
parent
978f91a10c
commit
3e8aa3d1cc
66
lib/yarp.rb
66
lib/yarp.rb
@ -293,71 +293,6 @@ module YARP
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# This object is responsible for generating the output for the inspect method
|
|
||||||
# implementations of child nodes.
|
|
||||||
class NodeInspector
|
|
||||||
attr_reader :prefix, :output
|
|
||||||
|
|
||||||
def initialize(prefix = "")
|
|
||||||
@prefix = prefix
|
|
||||||
@output = +""
|
|
||||||
end
|
|
||||||
|
|
||||||
# Appends a line to the output with the current prefix.
|
|
||||||
def <<(line)
|
|
||||||
output << "#{prefix}#{line}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# This generates a string that is used as the header of the inspect output
|
|
||||||
# for any given node.
|
|
||||||
def header(node)
|
|
||||||
output = +"@ #{node.class.name.split("::").last} ("
|
|
||||||
output << "location: (#{node.location.start_line},#{node.location.start_column})-(#{node.location.end_line},#{node.location.end_column})"
|
|
||||||
output << ", newline: true" if node.newline?
|
|
||||||
output << ")\n"
|
|
||||||
output
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generates a string that represents a list of nodes. It handles properly
|
|
||||||
# using the box drawing characters to make the output look nice.
|
|
||||||
def list(prefix, nodes)
|
|
||||||
output = +"(length: #{nodes.length})\n"
|
|
||||||
last_index = nodes.length - 1
|
|
||||||
|
|
||||||
nodes.each_with_index do |node, index|
|
|
||||||
pointer, preadd = (index == last_index) ? ["└── ", " "] : ["├── ", "│ "]
|
|
||||||
node_prefix = "#{prefix}#{preadd}"
|
|
||||||
output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}")
|
|
||||||
end
|
|
||||||
|
|
||||||
output
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generates a string that represents a location field on a node.
|
|
||||||
def location(value)
|
|
||||||
if value
|
|
||||||
"(#{value.start_line},#{value.start_column})-(#{value.end_line},#{value.end_column}) = #{value.slice.inspect}"
|
|
||||||
else
|
|
||||||
"∅"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generates a string that represents a child node.
|
|
||||||
def child_node(node, append)
|
|
||||||
node.inspect(child_inspector(append)).delete_prefix(prefix)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns a new inspector that can be used to inspect a child node.
|
|
||||||
def child_inspector(append)
|
|
||||||
NodeInspector.new("#{prefix}#{append}")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns the output as a string.
|
|
||||||
def to_str
|
|
||||||
output
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# There are many files in YARP that are templated to handle every node type,
|
# There are many files in YARP that are templated to handle every node type,
|
||||||
# which means the files can end up being quite large. We autoload them to make
|
# which means the files can end up being quite large. We autoload them to make
|
||||||
# our require speed faster since consuming libraries are unlikely to use all
|
# our require speed faster since consuming libraries are unlikely to use all
|
||||||
@ -369,6 +304,7 @@ module YARP
|
|||||||
autoload :Dispatcher, "yarp/dispatcher"
|
autoload :Dispatcher, "yarp/dispatcher"
|
||||||
autoload :DSL, "yarp/dsl"
|
autoload :DSL, "yarp/dsl"
|
||||||
autoload :MutationCompiler, "yarp/mutation_compiler"
|
autoload :MutationCompiler, "yarp/mutation_compiler"
|
||||||
|
autoload :NodeInspector, "yarp/node_inspector"
|
||||||
autoload :RipperCompat, "yarp/ripper_compat"
|
autoload :RipperCompat, "yarp/ripper_compat"
|
||||||
autoload :Pack, "yarp/pack"
|
autoload :Pack, "yarp/pack"
|
||||||
autoload :Pattern, "yarp/pattern"
|
autoload :Pattern, "yarp/pattern"
|
||||||
|
68
lib/yarp/node_inspector.rb
Normal file
68
lib/yarp/node_inspector.rb
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module YARP
|
||||||
|
# This object is responsible for generating the output for the inspect method
|
||||||
|
# implementations of child nodes.
|
||||||
|
class NodeInspector
|
||||||
|
attr_reader :prefix, :output
|
||||||
|
|
||||||
|
def initialize(prefix = "")
|
||||||
|
@prefix = prefix
|
||||||
|
@output = +""
|
||||||
|
end
|
||||||
|
|
||||||
|
# Appends a line to the output with the current prefix.
|
||||||
|
def <<(line)
|
||||||
|
output << "#{prefix}#{line}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# This generates a string that is used as the header of the inspect output
|
||||||
|
# for any given node.
|
||||||
|
def header(node)
|
||||||
|
output = +"@ #{node.class.name.split("::").last} ("
|
||||||
|
output << "location: (#{node.location.start_line},#{node.location.start_column})-(#{node.location.end_line},#{node.location.end_column})"
|
||||||
|
output << ", newline: true" if node.newline?
|
||||||
|
output << ")\n"
|
||||||
|
output
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generates a string that represents a list of nodes. It handles properly
|
||||||
|
# using the box drawing characters to make the output look nice.
|
||||||
|
def list(prefix, nodes)
|
||||||
|
output = +"(length: #{nodes.length})\n"
|
||||||
|
last_index = nodes.length - 1
|
||||||
|
|
||||||
|
nodes.each_with_index do |node, index|
|
||||||
|
pointer, preadd = (index == last_index) ? ["└── ", " "] : ["├── ", "│ "]
|
||||||
|
node_prefix = "#{prefix}#{preadd}"
|
||||||
|
output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}")
|
||||||
|
end
|
||||||
|
|
||||||
|
output
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generates a string that represents a location field on a node.
|
||||||
|
def location(value)
|
||||||
|
if value
|
||||||
|
"(#{value.start_line},#{value.start_column})-(#{value.end_line},#{value.end_column}) = #{value.slice.inspect}"
|
||||||
|
else
|
||||||
|
"∅"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generates a string that represents a child node.
|
||||||
|
def child_node(node, append)
|
||||||
|
node.inspect(child_inspector(append)).delete_prefix(prefix)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a new inspector that can be used to inspect a child node.
|
||||||
|
def child_inspector(append)
|
||||||
|
NodeInspector.new("#{prefix}#{append}")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the output as a string.
|
||||||
|
def to_str
|
||||||
|
output
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -68,6 +68,7 @@ Gem::Specification.new do |spec|
|
|||||||
"lib/yarp/lex_compat.rb",
|
"lib/yarp/lex_compat.rb",
|
||||||
"lib/yarp/mutation_compiler.rb",
|
"lib/yarp/mutation_compiler.rb",
|
||||||
"lib/yarp/node.rb",
|
"lib/yarp/node.rb",
|
||||||
|
"lib/yarp/node_inspector.rb",
|
||||||
"lib/yarp/pack.rb",
|
"lib/yarp/pack.rb",
|
||||||
"lib/yarp/pattern.rb",
|
"lib/yarp/pattern.rb",
|
||||||
"lib/yarp/ripper_compat.rb",
|
"lib/yarp/ripper_compat.rb",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user