sourceror
sourceror copied to clipboard
Trailing comments misplaced
Given the following source code in file bug.ex
:
a()
# test
When we execute the following File.read!("bug.ex") |> Sourceror.parse_string!() |> Sourceror.to_string() |> IO.puts()
, then the output is:
a()
# test
However, when we run File.read!("bug.ex") |> Code.format_string!() |> IO.iodata_to_binary() |> IO.puts()
, then the output is:
a()
# test
If I understood correctly, then Sourceror should format the code the same way as Elixir formatter which does not seem to be the case here.
Yup, this is a bug, I'm working on a fix :)
v0.11 is out with a fix, please let me know if you're still experiencing this issue
Thank you for the fix (and also for the amazing library! ❤️) but it looks like the bug persists for lists (and maybe also for some other contexts).
Let bug.ex
be:
[
a()
# test
]
File.read!("bug.ex") |> Sourceror.parse_string!() |> Sourceror.to_string() |> IO.puts()
outputs:
[
a()
# test
]
Whereas File.read!("bug.ex") |> Code.format_string!() |> IO.iodata_to_binary() |> IO.puts()
outputs:
[
a()
# test
]
Hi @doorgan in recode
another example of misplaced comments was reported (https://github.com/hrzndhrn/recode/issues/76).
The following script shows the issue:
Mix.install([{:sourceror, "~> 0.14"}])
code = """
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]
"""
write = fn code, i ->
IO.puts("--- code #{i} ---\n#{code}\n")
code
end
format = fn code ->
code
|> Sourceror.parse_string!()
|> Sourceror.to_string()
end
Enum.reduce(0..4, code, fn i, code ->
code
|> write.(i)
|> format.()
end)
--- code 0 ---
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]
--- code 1 ---
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]
--- code 2 ---
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]
--- code 3 ---
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]
--- code 4 ---
[
:field_1,
#######################################################
### Another comment
#######################################################
:field_2
]