smorin

smorin

Parse Elixir Module AST to replace functions and replace the original source file?

I am looking for a way to read in a elixir module and replace functions with code you want to run. Anyone know of any examples of code on how to parse a module’s AST to match against a function and replace it?

See example here: GitHub - smorin/BootstrapEx · GitHub

path = “lib/test_module.ex”
path
  |> File.read!()
  |> Code.string_to_quoted()
  |> Insert.print()

path = “some_path”
path
  |> get_file
  |> get_ast
  |> replace_function_that_matches_name
  |> ast_to_string
  |> string_to_file
  |> format_file

First 4 of 4 Posts Switch mode

chvanikoff

chvanikoff

TLDR: you can do this, but you can’t preserve code formatting, comments and other stuff which is not part of AST. I’ve been investigating a similar case here: Mix deps.add functionality - #3 by chvanikoff

smorin

smorin OP

Thanks, formatting I don’t care about but want to support comments. Not having that sucks.

smorin

smorin OP

Checked out your thread and actually was looking to do the same thing. Maybe we can suggest adding comments to the AST.

Eiji

Eiji

I don’t believe that’s good idea. Even if we would still not have formatting saved. I think that looking at formatter mix task may give more ideas, because it’s actually do not remove comments when changing file.

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement