Skip to content

Commit 1c843d2

Browse files
committed
Move node inspector into its own file
1 parent c6a0abd commit 1c843d2

File tree

3 files changed

+70
-65
lines changed

3 files changed

+70
-65
lines changed

lib/yarp.rb

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -293,71 +293,6 @@ def pretty_print(q)
293293
end
294294
end
295295

296-
# This object is responsible for generating the output for the inspect method
297-
# implementations of child nodes.
298-
class NodeInspector
299-
attr_reader :prefix, :output
300-
301-
def initialize(prefix = "")
302-
@prefix = prefix
303-
@output = +""
304-
end
305-
306-
# Appends a line to the output with the current prefix.
307-
def <<(line)
308-
output << "#{prefix}#{line}"
309-
end
310-
311-
# This generates a string that is used as the header of the inspect output
312-
# for any given node.
313-
def header(node)
314-
output = +"@ #{node.class.name.split("::").last} ("
315-
output << "location: (#{node.location.start_line},#{node.location.start_column})-(#{node.location.end_line},#{node.location.end_column})"
316-
output << ", newline: true" if node.newline?
317-
output << ")\n"
318-
output
319-
end
320-
321-
# Generates a string that represents a list of nodes. It handles properly
322-
# using the box drawing characters to make the output look nice.
323-
def list(prefix, nodes)
324-
output = +"(length: #{nodes.length})\n"
325-
last_index = nodes.length - 1
326-
327-
nodes.each_with_index do |node, index|
328-
pointer, preadd = (index == last_index) ? ["└── ", " "] : ["├── ", "│ "]
329-
node_prefix = "#{prefix}#{preadd}"
330-
output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}")
331-
end
332-
333-
output
334-
end
335-
336-
# Generates a string that represents a location field on a node.
337-
def location(value)
338-
if value
339-
"(#{value.start_line},#{value.start_column})-(#{value.end_line},#{value.end_column}) = #{value.slice.inspect}"
340-
else
341-
"∅"
342-
end
343-
end
344-
345-
# Generates a string that represents a child node.
346-
def child_node(node, append)
347-
node.inspect(child_inspector(append)).delete_prefix(prefix)
348-
end
349-
350-
# Returns a new inspector that can be used to inspect a child node.
351-
def child_inspector(append)
352-
NodeInspector.new("#{prefix}#{append}")
353-
end
354-
355-
# Returns the output as a string.
356-
def to_str
357-
output
358-
end
359-
end
360-
361296
# There are many files in YARP that are templated to handle every node type,
362297
# which means the files can end up being quite large. We autoload them to make
363298
# our require speed faster since consuming libraries are unlikely to use all
@@ -369,6 +304,7 @@ def to_str
369304
autoload :Dispatcher, "yarp/dispatcher"
370305
autoload :DSL, "yarp/dsl"
371306
autoload :MutationCompiler, "yarp/mutation_compiler"
307+
autoload :NodeInspector, "yarp/node_inspector"
372308
autoload :RipperCompat, "yarp/ripper_compat"
373309
autoload :Pack, "yarp/pack"
374310
autoload :Pattern, "yarp/pattern"

lib/yarp/node_inspector.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# frozen_string_literal: true
2+
3+
module YARP
4+
# This object is responsible for generating the output for the inspect method
5+
# implementations of child nodes.
6+
class NodeInspector
7+
attr_reader :prefix, :output
8+
9+
def initialize(prefix = "")
10+
@prefix = prefix
11+
@output = +""
12+
end
13+
14+
# Appends a line to the output with the current prefix.
15+
def <<(line)
16+
output << "#{prefix}#{line}"
17+
end
18+
19+
# This generates a string that is used as the header of the inspect output
20+
# for any given node.
21+
def header(node)
22+
output = +"@ #{node.class.name.split("::").last} ("
23+
output << "location: (#{node.location.start_line},#{node.location.start_column})-(#{node.location.end_line},#{node.location.end_column})"
24+
output << ", newline: true" if node.newline?
25+
output << ")\n"
26+
output
27+
end
28+
29+
# Generates a string that represents a list of nodes. It handles properly
30+
# using the box drawing characters to make the output look nice.
31+
def list(prefix, nodes)
32+
output = +"(length: #{nodes.length})\n"
33+
last_index = nodes.length - 1
34+
35+
nodes.each_with_index do |node, index|
36+
pointer, preadd = (index == last_index) ? ["└── ", " "] : ["├── ", "│ "]
37+
node_prefix = "#{prefix}#{preadd}"
38+
output << node.inspect(NodeInspector.new(node_prefix)).sub(node_prefix, "#{prefix}#{pointer}")
39+
end
40+
41+
output
42+
end
43+
44+
# Generates a string that represents a location field on a node.
45+
def location(value)
46+
if value
47+
"(#{value.start_line},#{value.start_column})-(#{value.end_line},#{value.end_column}) = #{value.slice.inspect}"
48+
else
49+
"∅"
50+
end
51+
end
52+
53+
# Generates a string that represents a child node.
54+
def child_node(node, append)
55+
node.inspect(child_inspector(append)).delete_prefix(prefix)
56+
end
57+
58+
# Returns a new inspector that can be used to inspect a child node.
59+
def child_inspector(append)
60+
NodeInspector.new("#{prefix}#{append}")
61+
end
62+
63+
# Returns the output as a string.
64+
def to_str
65+
output
66+
end
67+
end
68+
end

yarp.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Gem::Specification.new do |spec|
6868
"lib/yarp/lex_compat.rb",
6969
"lib/yarp/mutation_compiler.rb",
7070
"lib/yarp/node.rb",
71+
"lib/yarp/node_inspector.rb",
7172
"lib/yarp/pack.rb",
7273
"lib/yarp/pattern.rb",
7374
"lib/yarp/ripper_compat.rb",

0 commit comments

Comments
 (0)