Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing comments misplaced #53

Closed
aronglia opened this issue Apr 2, 2022 · 4 comments · Fixed by #128
Closed

Trailing comments misplaced #53

aronglia opened this issue Apr 2, 2022 · 4 comments · Fixed by #128
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@aronglia
Copy link

aronglia commented Apr 2, 2022

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.

@doorgan doorgan added bug Something isn't working good first issue Good for newcomers labels Apr 3, 2022
@doorgan doorgan self-assigned this Apr 3, 2022
@doorgan
Copy link
Owner

doorgan commented Apr 3, 2022

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

@doorgan
Copy link
Owner

doorgan commented Apr 3, 2022

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

@doorgan doorgan closed this as completed Apr 8, 2022
@xxdavid
Copy link

xxdavid commented Jul 27, 2022

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
]

@NickNeck
Copy link
Contributor

Hi @doorgan in recode another example of misplaced comments was reported (hrzndhrn/recode#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
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
4 participants