Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 875de00

Browse files
committed
Replicate content for __END__ keyword exactly.
1 parent d4c2f95 commit 875de00

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
3030
- Allow conditionals to take modifier form if they are using the `then` keyword with a `VoidStmt`.
3131
- `UntilMod` and `WhileMod` nodes that wrap a `Begin` should be forced into their modifier forms.
3232
- Ensure `For` loops keep their trailing commas.
33+
- Replicate content for `__END__` keyword exactly.
3334

3435
### Removed
3536

lib/syntax_tree.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def on_END(statements)
612612
# some other content that is not executed by the program
613613
#
614614
class EndContent
615-
# [String] the content after the script
615+
# [Array[ String ]] the content after the script
616616
attr_reader :value
617617

618618
# [Location] the location of this node
@@ -634,24 +634,26 @@ def child_nodes
634634
def format(q)
635635
q.text("__END__")
636636
q.breakable(force: true)
637-
q.text(value)
637+
638+
q.seplist(value, -> { q.breakable(literal: true, force: true) }) do |line|
639+
q.text(line)
640+
end
638641
end
639642

640643
def pretty_print(q)
641644
q.group(2, "(", ")") do
642645
q.text("__end__")
643646

644647
q.breakable
645-
q.pp(value)
648+
q.pp(value.join("\n"))
646649

647650
q.pp(Comment::List.new(comments))
648651
end
649652
end
650653

651654
def to_json(*opts)
652-
{ type: :__end__, value: value, loc: location, cmts: comments }.to_json(
653-
*opts
654-
)
655+
{ type: :__end__, value: value.join("\n"), loc: location, cmts: comments }
656+
.to_json(*opts)
655657
end
656658
end
657659

@@ -660,7 +662,7 @@ def to_json(*opts)
660662
def on___end__(value)
661663
@__end__ =
662664
EndContent.new(
663-
value: lines[lineno..-1].join("\n"),
665+
value: lines[lineno..-1],
664666
location: Location.token(line: lineno, char: char_pos, size: value.size)
665667
)
666668
end
@@ -9042,7 +9044,11 @@ def child_nodes
90429044

90439045
def format(q)
90449046
q.format(statements)
9045-
q.breakable(force: true)
9047+
9048+
# We're going to put a newline on the end so that it always has one unless
9049+
# it ends with the special __END__ syntax. In that case we want to
9050+
# replicate the text exactly so we will just let it be.
9051+
q.breakable(force: true) unless statements.body.last.is_a?(EndContent)
90469052
end
90479053

90489054
def pretty_print(q)

0 commit comments

Comments
 (0)