sodapopcan

sodapopcan

I go through bouts of playing around with re-writing source-code using Sourceror. It generally goes: I learn a whole bunch, start getting comfortable with it, stop doing it for many months and forget everything :expressionless:

I’m back at it and just finished creating a function that will inline single pipes. IE:

socket
|> assign(:foo, "foo")

becomes:

assign(socket, :foo, "foo")

I was looking to get some feedback on my solution.

I initially thought I could get away with simply pre- or postwalking with some clever pattern-matching, but that proved to be difficult for me. I ended up using a zipper which made life a LOT easier getting me to a solution quite quickly. There are still a few issues with line-length but I’m otherwise quite happy with the clarity of it.

Still, I’m wondering:

  • Is this possible using walking with an accumulator?
  • Do you have a solution that’s different/better than mine?
  • Do you have any other feedback?

TIA

def unpipe(ast) do
  Sourceror.Zipper.zip(ast)
  |> Sourceror.Zipper.traverse(fn
    %{node: {:|>, _, _} = node} = zipper ->
      prev = Sourceror.Zipper.prev(zipper)
      next = Sourceror.Zipper.next(zipper)

      with false <- match?(%{node: {:|>, _, _}}, prev),
           false <- match?(%{node: {:|>, _, _}}, next),
           {:|>, _, [var, {func, meta, args}]} <- node do
        Sourceror.Zipper.replace(zipper, {func, meta, [var | args]})
      else
        _ ->
          zipper
      end

    zipper ->
      zipper
  end)
  |> Sourceror.Zipper.topmost_root()
end

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

Is the idea here to unpipe everything next/down from the current zipper? I think your code would turn this

socket
|> assign(:foo, "foo" |> String.trim())

into this:

assign(socket, :foo, String.trim("foo"))

Is that what you’re aiming for?

sodapopcan

sodapopcan OP

Yes, any single pipes, though that particular case I did not account for! It does indeed work here though it adds new lines (which is a separate general issue I’m dealing with).

These were my test cases so far (not super thorough):

defp run(string, func) do
  result =
    string
    |> Sourceror.parse_string!()
    |> func.()
    |> Sourceror.to_string()

  result <> "\n"
end

describe "unpipe" do
  test "changes single pipes into inline calls" do
    source = ~S"""
    socket
    |> assign(:foo, "foo")
    """

    result = run(source, &unpipe/1)

    assert result == ~S"""
           assign(socket, :foo, "foo")
           """
  end

  test "leaves longer pipelines alone" do
    source = ~S"""
    socket
    |> assign(:foo, "foo")
    |> assign(:bar, "bar")
    """

    result = run(source, &unpipe/1)

    assert result == ~S"""
           socket
           |> assign(:foo, "foo")
           |> assign(:bar, "bar")
           """
  end

  test "handles nested pipes" do
    source = ~S"""
    foo
    |> bar()
    |> baz(fn n ->
      n
      |> bar()
      |> baz(fn m ->
        m
        |> foo()
      end)
    end)
    """

    result = run(source, &unpipe/1)

    assert result == ~S"""
           foo
           |> bar()
           |> baz(fn n ->
             n
             |> bar()
             |> baz(fn m ->
               foo(m)
             end)
           end)
           """
  end
zachdaniel

zachdaniel

Creator of Ash

Ahhh, interesting. Only pipes where there isn’t a 2 or more pipes. Neat idea :slight_smile: In that case your traverse + prev/next strategy looks like the best way to go about this IMO. It’s a benefit of Zipper that you actually don’t need an accumulator to do this kind of thing. It would only add complexity IMO.

zachdaniel

zachdaniel

Creator of Ash

The new line change likely is just coming from the fact that the code is formatted when you stringify it? Probably nothing to be done there?

sodapopcan

sodapopcan OP

Yes, I just started on a new team that inherited a codebase where it’s rampant. I’m not too fussed about “no single pipes” depending on how they are used, but they are everywhere to point it’s making code exhausting to read. There is even stuff like this:

"string"
|> String.capitalize()

In any event, we’re getting all of our bikeshedding out of the way upfront :slight_smile: And it’s also nice to have a task to run in CI.

That’s good to know! Zipper certainly allowed me to write code inline with how I was thinking about the problem. It’s more of an academic interest if it’s even possible with basic walking as it was hurting my brain trying to figure it out. I’m happy to move on from it, though.

The problem is is that it’s doing it for any function calls that have more than one arg, even if the line is within the limit. I believe it has to do with how Sourceror adds :__block__ around stuff to help maintain the original format. I’m pretty sure I just need to be more explicit in how I return the transformed node. I believe the answer might be in here somewhere.

Thanks for your response, Zach!

zachdaniel

zachdaniel

Creator of Ash

Ah, right. I think what you can do is check if the node previous to the top node is {:__block__, meta, [just_one_thing]} and if so, replace that node instead? Might mess w/ your traversal though.

sodapopcan

sodapopcan OP

Ok, I may be celebrating early but simply setting empty meta solved it.

       {:|>, _, [var, {func, _, args}]} <- node do
    Sourceror.Zipper.replace(zipper, {func, [], [var | args]})
sodapopcan

sodapopcan OP

I’ll keep an eye out for this, thanks!!

sodapopcan

sodapopcan OP

Turns out the real answer is “just use recode” which I totally forgot about. Was a good learning experience regardless :slight_smile:

zachdaniel

zachdaniel

Creator of Ash

Based on your proclivity for source code rewriting, you may also be interested in the igniter project :slight_smile:

It’s similar to recode in some ways, but designed for building generators, installers, and upgraders. GitHub - ash-project/igniter: A code generation and project patching framework. · GitHub

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews