Skip to content

Commit c58466e

Browse files
committed
Fix an AST and token incompatibility for Prism::Translation::Parser
Fixes #2515. This PR fixes an AST and token incompatibility between Parser gem and `Prism::Translation::Parser` for string literal with line breaks.
1 parent c5f5a02 commit c58466e

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

lib/prism/translation/parser/compiler.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,9 +1487,23 @@ def visit_string_node(node)
14871487
elsif node.opening == "?"
14881488
builder.character([node.unescaped, srange(node.location)])
14891489
else
1490+
parts = if node.unescaped.lines.count <= 1
1491+
[builder.string_internal([node.unescaped, srange(node.content_loc)])]
1492+
else
1493+
start_offset = node.content_loc.start_offset
1494+
1495+
node.unescaped.lines.map do |line|
1496+
end_offset = start_offset + line.length
1497+
offsets = srange_offsets(start_offset, end_offset)
1498+
start_offset = end_offset
1499+
1500+
builder.string_internal([line, offsets])
1501+
end
1502+
end
1503+
14901504
builder.string_compose(
14911505
token(node.opening_loc),
1492-
[builder.string_internal([node.unescaped, srange(node.content_loc)])],
1506+
parts,
14931507
token(node.closing_loc)
14941508
)
14951509
end

lib/prism/translation/parser/lexer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def to_a
281281
value = ""
282282
location = Range.new(source_buffer, offset_cache[next_location.start_offset], offset_cache[next_location.end_offset])
283283
index += 1
284-
elsif ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_CONTENT && (next_next_token = lexed[index + 1][0]) && next_next_token.type == :STRING_END
284+
elsif ["\"", "'"].include?(value) && (next_token = lexed[index][0]) && next_token.type == :STRING_CONTENT && next_token.value.lines.count <= 1 && (next_next_token = lexed[index + 1][0]) && next_next_token.type == :STRING_END
285285
next_location = token.location.join(next_next_token.location)
286286
type = :tSTRING
287287
value = next_token.value

test/prism/fixtures/dstring.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"foo
2+
bar"

test/prism/snapshots/dstring.txt

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

0 commit comments

Comments
 (0)