chulkilee

chulkilee

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?

Most Liked

NobbZ

NobbZ

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
kokolegorille

kokolegorille

Schultzer

Schultzer

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

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement