Skip to content

Some multiline dynamic strings do not satisfy the roundtrip test #412

@prikha

Description

@prikha

I think this PR #366 while having positive impact can turn working code into broken one after wrapping dynamic strings into HEREDOC:

> code = "\"\\n\n \#{x}\""
> ast = Unparser.parse(code)
=> s(:dstr,
  s(:str, "\n\n"),
  s(:str, " "),
  s(:begin,
    s(:send, nil, :x)))
> processed_code = Unparser.unparse(ast)
=> "<<-HEREDOC\n\n\n \#{x}HEREDOC\n"
> Unparser.parse(processed_code)
=> Parser::SyntaxError: unterminated string meets end of file (Parser::SyntaxError)

Feels like we are missing a newline before the closing HEREDOC.

PS
There is an "easy" fix that would check the content of the heredoc and see whether there is a newline at the end, if not perform chomp. But that deviates from the original AST meaning the provided code is not semantically the same.

Haven't found a better solution though. At least it produces parseable code and evaluates to the same value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions