zorn

zorn

Anyone have a regex to de-nest alaises in an Elixir code base?

My team is considering enabling the credo rule https://hexdocs.pm/credo/Credo.Check.Readability.MultiAlias.html that would force you to not nest aliases.

We have a lot of code like:

      alias MyApp.{
        Cat,
        Dog,
        Parrot
      }

And I’m looking for an automated way to do a search and replace into something like:

      alias MyApp.Cat
      alias MyApp.Dog
      alias MyApp.Parrot

I suspect this might be doable with regex but I have very little skills for that. Any chance someone here has that sitting around in a notebook somewhere or other suggestion?

PS: As to why we want to do this, it is to improve the grep-ability of the source. We want to have confidence when we search for MyApp.Dog we find those module references.

Most Liked

al2o3cr

al2o3cr

That kind of transformation is safer to do on AST (versus regexing code). The formatter provides some inspiration:

In between calls to Code.string_to_quoted_with_comments! and Code.Formatter.to_algebra, you could find alias nodes in the AST and transform them into the desired shape.

OR

You (could use the already-baked version included as a tutorial in the Sourceror docs! :tada:

See also the release announcement thread:

mudasobwa

mudasobwa

Creator of Cure

What would be wrong with using the aforementioned regex (assuming you have one) with grep leaving the code as-is for better readability?

grep -Pz '(?s)alias\s+(MyApp\.Dog|MyApp.{.*?,\s*Dog.*?})' $FILES

Just make the shell function to wrap this grep call, or even create an escript using Elixir regexes (the regex itself would be simpler, I believe.)

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement