diff options
author | Earlopain <[email protected]> | 2025-01-07 15:56:39 +0100 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2025-01-11 19:09:05 -0500 |
commit | 80fe9a1c777e4636419e84f160c7079c152696e1 (patch) | |
tree | 2f4219c726cf20167ee7a04d8d678971f71ba7fe /lib | |
parent | 9f38ee11cb4554ec4e08fb22a9c804c746cce029 (diff) |
[ruby/prism] Better handle multiline interpolated strings in the parser translator
Much of this logic should be shared between interpolated symbols and regexps.
It's also incorrect when the node contains a literal `\\n` (same as for plain string nodes at the moment)
https://github.com/ruby/prism/commit/561914f99b
Diffstat (limited to 'lib')
-rw-r--r-- | lib/prism/translation/parser/compiler.rb | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/lib/prism/translation/parser/compiler.rb b/lib/prism/translation/parser/compiler.rb index a20136a03d..27af56e03e 100644 --- a/lib/prism/translation/parser/compiler.rb +++ b/lib/prism/translation/parser/compiler.rb @@ -1085,24 +1085,22 @@ module Prism return visit_heredoc(node) { |children, closing| builder.string_compose(token(node.opening_loc), children, closing) } end - parts = if node.parts.one? { |part| part.type == :string_node } - node.parts.flat_map do |node| - if node.type == :string_node && node.unescaped.lines.count >= 2 - start_offset = node.content_loc.start_offset + parts = node.parts.flat_map do |node| + # When the content of a string node is split across multiple lines, the + # parser gem creates individual string nodes for each line the content is part of. + if node.type == :string_node && node.content.include?("\n") && node.opening_loc.nil? + start_offset = node.content_loc.start_offset - node.unescaped.lines.map do |line| - end_offset = start_offset + line.bytesize - offsets = srange_offsets(start_offset, end_offset) - start_offset = end_offset + node.unescaped.lines.map do |line| + end_offset = start_offset + line.bytesize + offsets = srange_offsets(start_offset, end_offset) + start_offset = end_offset - builder.string_internal([line, offsets]) - end - else - visit(node) + builder.string_internal([line, offsets]) end + else + visit(node) end - else - visit_all(node.parts) end builder.string_compose( |