sourceror icon indicating copy to clipboard operation
sourceror copied to clipboard

Trailing comments misplaced

Open aronglia opened this issue 2 years ago • 4 comments

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.

aronglia avatar Apr 02 '22 08:04 aronglia

Yup, this is a bug, I'm working on a fix :)

doorgan avatar Apr 03 '22 19:04 doorgan

v0.11 is out with a fix, please let me know if you're still experiencing this issue

doorgan avatar Apr 03 '22 20:04 doorgan

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
]

xxdavid avatar Jul 27 '22 10:07 xxdavid

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
]

NickNeck avatar Oct 12 '23 03:10 NickNeck