summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-09-08 14:33:21 -0400
committergit <svn-admin@ruby-lang.org>2023-09-08 19:38:17 +0000
commitc0f162caab6098b1709cce893c2c86fec88d0d4c (patch)
tree2ab5f397b023efda122a25c1cf374e3ef9c4da97 /lib
parent5d73c0f3dfa489ec3380b997dd151a07e790562a (diff)
[ruby/yarp] Template out a comment_targets method
https://github.com/ruby/yarp/commit/a94af7c4c8
Diffstat (limited to 'lib')
-rw-r--r--lib/yarp/parse_result/comments.rb8
-rw-r--r--lib/yarp/parse_result/newlines.rb7
2 files changed, 6 insertions, 9 deletions
diff --git a/lib/yarp/parse_result/comments.rb b/lib/yarp/parse_result/comments.rb
index 58eb80af59..88240609b1 100644
--- a/lib/yarp/parse_result/comments.rb
+++ b/lib/yarp/parse_result/comments.rb
@@ -100,9 +100,7 @@ module YARP
comment_end = comment.location.end_offset
targets = []
- node.deconstruct_keys(nil).each do |key, value|
- next if key == :location
-
+ node.comment_targets.map do |value|
case value
when StatementsNode
targets.concat(value.body.map { |node| NodeTarget.new(node) })
@@ -110,8 +108,6 @@ module YARP
targets << NodeTarget.new(value)
when Location
targets << LocationTarget.new(value)
- when Array
- targets.concat(value.map { |node| NodeTarget.new(node) }) if value.first.is_a?(Node)
end
end
@@ -166,6 +162,8 @@ module YARP
end
end
+ private_constant :Comments
+
# Attach the list of comments to their respective locations in the tree.
def attach_comments!
Comments.new(self).attach!
diff --git a/lib/yarp/parse_result/newlines.rb b/lib/yarp/parse_result/newlines.rb
index 6f35543478..d16600afd0 100644
--- a/lib/yarp/parse_result/newlines.rb
+++ b/lib/yarp/parse_result/newlines.rb
@@ -17,7 +17,7 @@ module YARP
# Note that the logic in this file should be kept in sync with the Java
# MarkNewlinesVisitor, since that visitor is responsible for marking the
# newlines for JRuby/TruffleRuby.
- class MarkNewlinesVisitor < Visitor
+ class Newlines < Visitor
def initialize(newline_marked)
@newline_marked = newline_marked
end
@@ -50,12 +50,11 @@ module YARP
end
end
- private_constant :MarkNewlinesVisitor
+ private_constant :Newlines
# Walk the tree and mark nodes that are on a new line.
def mark_newlines!
- newline_marked = Array.new(1 + source.offsets.size, false)
- value.accept(MarkNewlinesVisitor.new(newline_marked))
+ value.accept(Newlines.new(Array.new(1 + source.offsets.size, false)))
end
end
end