Skip to content

Macro.to_string renders broken heredocs #15354

@lukaszsamson

Description

@lukaszsamson

Elixir and Erlang/OTP versions

Erlang/OTP 28 [erts-16.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit]

Elixir 1.19.5 (compiled with Erlang/OTP 28)

Operating system

any

Current behavior

Repro:

tq = ~S(""")

source =
  "Logger.warning(" <>
    tq <>
    "\n  Reference: \#{x}\\" <>
    "\n  " <>
    tq <>
    ")\n"

{:ok, ast} =
  Code.string_to_quoted(source, columns: true, token_metadata: true)

rendered = Macro.to_string(ast)

Code.string_to_quoted(rendered)

{:error,
 {[
    opening_delimiter: :"\"\"\"",
    expected_delimiter: :"\"\"\"",
    line: 1,
    column: 16,
    end_line: 2,
    end_column: 20
  ], "missing terminator: \"\"\" (for heredoc starting at line 1)", ""}}

IO.puts(rendered)
Logger.warning("""
Reference: #{x}""")

It looks like it is not counting newlines correctly and renders end delimiter on the same line

Expected behavior

I expect Macro.to_string to produce valid parseable code from valid AST

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions