Skip to content

Commit 3c98f36

Browse files
committed
Add new node, HeredocEnd
1 parent 9ad1cdd commit 3c98f36

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

lib/syntax_tree/node.rb

+42-3
Original file line numberDiff line numberDiff line change
@@ -4813,7 +4813,7 @@ class Heredoc < Node
48134813
# [HeredocBeg] the opening of the heredoc
48144814
attr_reader :beginning
48154815

4816-
# [String] the ending of the heredoc
4816+
# [HeredocEnd] the ending of the heredoc
48174817
attr_reader :ending
48184818

48194819
# [Integer] how far to dedent the heredoc
@@ -4847,7 +4847,7 @@ def accept(visitor)
48474847
end
48484848

48494849
def child_nodes
4850-
[beginning, *parts]
4850+
[beginning, *parts, ending]
48514851
end
48524852

48534853
alias deconstruct child_nodes
@@ -4883,7 +4883,7 @@ def format(q)
48834883
end
48844884
end
48854885

4886-
q.text(ending)
4886+
q.format(ending)
48874887
end
48884888
end
48894889
end
@@ -4929,6 +4929,45 @@ def format(q)
49294929
end
49304930
end
49314931

4932+
# HeredocEnd represents the closing declaration of a heredoc.
4933+
#
4934+
# <<~DOC
4935+
# contents
4936+
# DOC
4937+
#
4938+
# In the example above the HeredocEnd node represents the closing DOC.
4939+
class HeredocEnd < Node
4940+
# [String] the closing declaration of the heredoc
4941+
attr_reader :value
4942+
4943+
# [Array[ Comment | EmbDoc ]] the comments attached to this node
4944+
attr_reader :comments
4945+
4946+
def initialize(value:, location:, comments: [])
4947+
@value = value
4948+
@location = location
4949+
@comments = comments
4950+
end
4951+
4952+
def accept(visitor)
4953+
visitor.visit_heredoc_end(self)
4954+
end
4955+
4956+
def child_nodes
4957+
[]
4958+
end
4959+
4960+
alias deconstruct child_nodes
4961+
4962+
def deconstruct_keys(_keys)
4963+
{ value: value, location: location, comments: comments }
4964+
end
4965+
4966+
def format(q)
4967+
q.text(value)
4968+
end
4969+
end
4970+
49324971
# HshPtn represents matching against a hash pattern using the Ruby 2.7+
49334972
# pattern matching syntax.
49344973
#

lib/syntax_tree/parser.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1640,9 +1640,19 @@ def on_heredoc_dedent(string, width)
16401640
def on_heredoc_end(value)
16411641
heredoc = @heredocs[-1]
16421642

1643+
location =
1644+
Location.token(
1645+
line: lineno,
1646+
char: char_pos,
1647+
column: current_column,
1648+
size: value.size + 1
1649+
)
1650+
1651+
heredoc_end = HeredocEnd.new(value: value.chomp, location: location)
1652+
16431653
@heredocs[-1] = Heredoc.new(
16441654
beginning: heredoc.beginning,
1645-
ending: value.chomp,
1655+
ending: heredoc_end,
16461656
dedent: heredoc.dedent,
16471657
parts: heredoc.parts,
16481658
location:

lib/syntax_tree/visitor.rb

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ class Visitor < BasicVisitor
194194
# Visit a HeredocBeg node.
195195
alias visit_heredoc_beg visit_child_nodes
196196

197+
# Visit a HeredocEnd node.
198+
alias visit_heredoc_end visit_child_nodes
199+
197200
# Visit a HshPtn node.
198201
alias visit_hshptn visit_child_nodes
199202

lib/syntax_tree/visitor/field_visitor.rb

+4
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ def visit_heredoc_beg(node)
497497
visit_token(node, "heredoc_beg")
498498
end
499499

500+
def visit_heredoc_end(node)
501+
visit_token(node, "heredoc_end")
502+
end
503+
500504
def visit_hshptn(node)
501505
node(node, "hshptn") do
502506
field("constant", node.constant) if node.constant

0 commit comments

Comments
 (0)