How to remove comments from Elixir code?

I found Code.string_to_quoted/2 returns quoted expression, but couldn’t find a way to rebuild it to code. Also, it has all location information and complicated nest format… so it’s doable but doesn’t seem “easy” at the first glance (e.g. no documentation on the structure? )

I already have a shell script to remove any line starting with # (with optional leading white spaces)… but of course that can break things (e.g. # in multiline string)

Any tips?

2 Likes

Maybe sed?

https://www.commandlinefu.com/commands/view/10104/remove-comments-from-files

or awk?

awk '!/^ *#/ && NF' file
2 Likes

I’m already using sed but I’d like to handle it with AST so that it doesn’t remove markdown header from multistring @doc, for example

Here is a command I usually run after mix new

sed -i '/^ *#/d' \
  .gitignore \
  config/*.exs \
  lib/**/*.ex \
  mix.exs \
  test/**/*.ex \
  test/**/*.exs

mix format

You are looking for https://hexdocs.pm/elixir/Macro.html#to_string/2

2 Likes

You can use Macro.to_string/1/2:

iex(1)> """
...(1)> defmodule M do
...(1)>   # a function
...(1)>   def foo, do: :foo
...(1)> end
...(1)> """
"defmodule M do\n  # a function\n  def foo, do: :foo\nend\n"
iex(2)> Code.string_to_quoted(v(1))
{:ok,
 {:defmodule, [line: 1],
  [
    {:__aliases__, [line: 1], [:M]},
    [do: {:def, [line: 3], [{:foo, [line: 3], nil}, [do: :foo]]}]
  ]}}
iex(3)> v(2) |> elem(1) |> Macro.to_string
"defmodule(M) do\n  def(foo) do\n    :foo\n  end\nend"
iex(4)> IO.puts(v(3))
defmodule(M) do
  def(foo) do
    :foo
  end
end
:ok
4 Likes

Thanks - but I found it does not return same format as mix format for .exs

remove-comment.exs file

# top-level comment

defmodule RemoveComment do
  # in-module comment

  def run(code_string) do
    # in-func comment
    code_string
    |> Code.string_to_quoted!()
    |> Macro.to_string()
  end
end

"./remove-comment.exs"
|> File.read!()
|> RemoveComment.run()
|> IO.puts()

$ elixir --version
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.9.0 (compiled with Erlang/OTP 22)

$ elixir remove-comment.exs
(
  defmodule(RemoveComment) do
    def(run(code_string)) do
      code_string |> Code.string_to_quoted!() |> Macro.to_string()
    end
  end
  "./remove-comment.exs" |> File.read!() |> RemoveComment.run() |> IO.puts()
)

Thats properly because the formatter use https://hexdocs.pm/elixir/Inspect.Algebra.html

1 Like