dorgan
Sourceror - Utilities to work with Elixir source code
Now that the next Elixir version will add Code.quoted_to_algebra/2 and Code.string_to_quoted_with_comments/2, we’re able to take some source code, parse it, change it and turn it back to formatted text. There are a couple gotchas if you change the ast in certain ways: since quoted_to_algebra requires the ast and comments to be given as separate arguments, we need to reconcile the line numbers of ast nodes and comments if we want the comments to be correctly placed.
So I wrote Sourceror, an (experimental) library that provides utilities to perform manipulations of the source code. I’m still working on more docs and examples(and tests), but this is an example of a function that expands multi alias syntax(ie: Foo.{Bar, Baz}) into their own lines:
Or a function to add a dependency to mix.exs ala npm install:
Since the new functions are only available in Elixir master, Sourceror depends on Elixir 1.13.0-dev and can only be installed via git dependency.
Most Liked
dorgan
Sourceror is now available on hex.pm and supports Elixir versions down to 1.10 
dorgan
I finally got some time work on Sourceror, and there have been a bunch of bug fixes, and a new 0.9 version! Thanks to the folks that helped to test and iron out issues in both my comments syncer and Elixir 1.13 release candidate ![]()
There are some slight changes to the API, and more functionality is exposed. Also, the notebooks are now available as guide pages in hexdocs.
v0.9.0
1. Enhancements
- [Sourceror]
to_string/2now supports options forCode.quoted_to_algebra, likelocals_without_parens - [Sourceror]
get_range/2no longer considers comments when calculating the range. This can be enabled by passing theinclude_comments: trueoption - [Sourceror.Patch] Introduced
Sourceror.Patchwith utilities to generate patches for the most common rewriting operations - [Sourceror.Identifier]
Sourceror.Identifieris now public
v0.8.x summary of bug fixes
- [Sourceror] Fixed comment spacing on binary operators
- [Sourceror] Take comment end of line counts into account to preserve spacing
- [Sourceror] Fixed an issue that caused comments in lists to be misplaced
- [Sourceror] Fixed issues that caused comments to be misplaced.
- [Sourceror] Updated internal normalizer to match latest Elixir 1.13 version.
- [Sourceror] Fixed an issue that caused newlines to be wrongly removed.
- [Sourceror] Fixed an issue that caused comments in pipelines to be misplaced.
- [Sourceror] Fixed issue that prevented keyword lists from preserving their
original format in tuples. - [Sourceror]
get_range/1now properly handles naked AST lists, like the ones
coming from partial keyword lists, or stabs likea -> b. - [Sourceror]
get_range/1now handles partial keyword list syntax instead of
crashing. - [Sourceror.Zipper]
down/1now correctly usesnilas the right siblings if
the branch node has a single child. - [Sourceror]
Sourceror.get_range/1now correctly calculates the range when
there is a comment in the same line as the node.
dorgan
The target audience is primarily tool authors, like elixir-ls or credo.
Yes, those are the kind of use cases I had in mind
The Sourceror.to_string/2 function has an option to set the indentation level of the resulting code for that particular use case. I will probably add functions to know how many lines an ast node uses, so one could replace a line range instead of the whole file.
This started while exploring ways to allow credo to autofix some of the issues it finds, the multi alias expansion example derived from that.
Mostly finding what people find most cumbersome or confusing to do, I think the most important thing right now is to start experimenting. There may be some bugs in Code.quoted_to_algebra/2 too, some experiments in that front would be nice as well so we can add more regression tests to core Elixir ![]()







