Skip to content

Commit e9f4361

Browse files
authored
Heredocs can create Interpolated(X)StringNodes or (X)StringNodes (#1427)
Prior to this commit, heredocs were automatically InterpolatedNodes regardless of whether there was actually interpolation. With this commit, heredocs are only interpolate if there is actually interpolation
1 parent 407a8cb commit e9f4361

File tree

58 files changed

+681
-1002
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+681
-1002
lines changed

src/yarp.c

Lines changed: 201 additions & 132 deletions
Large diffs are not rendered by default.

test/yarp/heredoc_dedent_test.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ class HeredocDedentTest < TestCase
88

99
File.read(filepath).split(/(?=\n)\n(?=<)/).each_with_index do |heredoc, index|
1010
define_method "test_heredoc_#{index}" do
11-
parts = YARP.parse(heredoc).value.statements.body.first.parts
12-
actual = parts.map { |part| part.is_a?(StringNode) ? part.unescaped : "1" }.join
11+
node = YARP.parse(heredoc).value.statements.body.first
12+
if node.is_a? StringNode
13+
actual = node.unescaped
14+
else
15+
actual = node.parts.map { |part| part.is_a?(StringNode) ? part.unescaped : "1" }.join
16+
end
1317

1418
assert_equal(eval(heredoc), actual, "Expected heredocs to match.")
1519
end

test/yarp/location_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def test_InterpolatedRegularExpressionNode
460460

461461
def test_InterpolatedStringNode
462462
assert_location(InterpolatedStringNode, "\"foo \#@bar baz\"")
463-
assert_location(InterpolatedStringNode, "<<~A\nhello world\nA", 0...4)
463+
assert_location(InterpolatedStringNode, "<<~A\nhello \#{1} world\nA", 0...4)
464464
end
465465

466466
def test_InterpolatedSymbolNode

test/yarp/parse_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ def assert_non_overlapping_locations(node)
186186
# We only want to compare parent/child location overlap in the case that
187187
# we are not looking at a heredoc. That's because heredoc locations are
188188
# special in that they only use the declaration of the heredoc.
189-
compare = !(current.is_a?(InterpolatedStringNode) || current.is_a?(InterpolatedXStringNode)) || !current.opening&.start_with?("<<")
189+
compare = !(current.is_a?(StringNode) ||
190+
current.is_a?(XStringNode) ||
191+
current.is_a?(InterpolatedStringNode) ||
192+
current.is_a?(InterpolatedXStringNode)) ||
193+
!current.opening&.start_with?("<<")
190194

191195
current.child_nodes.each do |child|
192196
# child_nodes can return nil values, so we need to skip those.

test/yarp/snapshots/dash_heredocs.txt

Lines changed: 40 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/dos_endings.txt

Lines changed: 8 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/heredoc_with_escaped_newline_at_start.txt

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/heredoc_with_trailing_newline.txt

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/heredocs_nested.txt

Lines changed: 4 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/heredocs_with_ignored_newlines.txt

Lines changed: 8 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)