unparser icon indicating copy to clipboard operation
unparser copied to clipboard

Some backslashes are dropped from heredocs

Open akimd opened this issue 1 year ago • 1 comments

Hi Markus,

When there is an interpolation in the heredoc, it comes out underescaped.

require 'unparser'

ast = Unparser.parse(<<~'OUTER')
    <<~RUBY
        validates :#{field_sid}, format: {
            with: /\\A\\-+(\\w+\\-+)|(\\w+\\.)\\Z/i
         }
    RUBY
OUTER
p ast
puts Unparser.unparse(ast)
s(:dstr,
  s(:str, "validates :"),
  s(:begin,
    s(:send, nil, :field_sid)),
  s(:str, ", format: {\n"),
  s(:str, "    with: /\\A\\-+(\\w+\\-+)|(\\w+\\.)\\Z/i\n"),
  s(:str, " }\n"))
<<-HEREDOC
validates :#{field_sid}, format: {
    with: /\A\-+(\w+\-+)|(\w+\.)\Z/i
 }
HEREDOC

All the backslashes should be doubled. And they are if I remove the interpolation.

Cheers!

akimd avatar Jul 11 '24 15:07 akimd

Its very likely this is solved by #366, will test it. Sadly #366 is not ready yet as its a deeper refactor.

mbj avatar Jul 12 '24 17:07 mbj

Fixed with #366 which just got merged.

mbj avatar Mar 16 '25 05:03 mbj

🎉🙏

akimd avatar Mar 16 '25 15:03 akimd

@akimd Also released as 0.7.0. This was as significant blob of work. And more work is to come to support 3.4 correctly as parser is being deprecated in favor of prism. Feel free to reach out in private should your org be able to support this efforts.

mbj avatar Mar 16 '25 20:03 mbj