mbj / unparser

Turn Ruby AST into semantically equivalent Ruby source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support 2.7 AST

mbj opened this issue · comments

Ruby 2.7 comes with many additions to the AST.

It might not be a good idea to open an issue against a development branch, so I'm just indicating here the following anomaly I observed on the 2.7 branch with current state of affairs.

$ pwd
/home/csaba/ruby/src/unparser
$ git show -s  --pretty=oneline
42d2ac80240dd11b3e616d6c88344aaa1a223d6e (HEAD -> add/2.7-support, origin/add/2.7-support) Add 2.7 support
$ echo  'p (not String === :a)'  | ruby
true
$ echo  'p (not String === :a)'  | ruby  -I ~/ruby/src/unparser/lib  -runparser -e 'puts Unparser.unparse(Unparser.parse $<.read)'  2>/dev/null
p (!String === :a)
$ echo  'p (not String === :a)'  | ruby  -I ~/ruby/src/unparser/lib  -runparser -e 'puts Unparser.unparse(Unparser.parse $<.read)'  2>/dev/null  | ruby
false

@csabahenk This is fine, but to be expected at this point this PR is not yet ready for prime time.

Curious what are you using unparser for?

Mangling my mushrooming oneliners to sensible shape when they are ripe to harvest from the REPL for being carved into a script.

I know Unparser is not exactly a code formatter; it strips comments and does not respect personal taste where multiple syntaxes are available, but such a shame I didn't find a working bona fide code formatter in Rubyland that can be used standalone like gofmt or rustfmt. I wonder is there such a thing? Or such functionality is only baked into IDE-s?

Anyway, Unparser is really a breath of fresh air, and I don't mind above issues. after all, I rarely have comments in what I type into the REPL.

I wonder is there such a thing? Or such functionality is only baked into IDE-s?

You are probably looking for the tree rewriter in parser? It keeps umodified syntax untouched.

it strips comments

Unparser can reproduce comments, 2nd arg in Unparser.unparse.

Rubyland that can be used standalone like gofmt or rustfmt. I wonder is there such a thing? Or such functionality is only baked into IDE-s?

If you where to use the API that can reproduce comments, and would be fine with the unparser syntax - that is not configurable (intentionally) - you "have" such a tool in unparser ;).

But unparsers syntax is meant to be correct, not correct and beautiful. As I mostly use it for machine generated code.

Beautiful ruby code (and correct) is right now outside my focus, the complexity would be too high for the time I can invest.

Thanks for the comment hint.

Yeah, it's probably not "#{"hey "}#{person}#{" there"}" what comes to my mind when I'm to have "hey #{person} there" cleaned up, but it's not a problem for my use case. Proper indentation is what mostly matters and that's finely delivered by Unparser.

The case you mention above is likely to be solved soon. As a side effect of other work in this branch.

I do not have lots of time for unparser itself lately. Looking for ways to free some time via GH sponsors. But I live in a jurisdiction Stripe connect does not allow (Right now).

@chastell The replacement PR #148 should actually be much stronger in 2.7 support than the original one I closed today. I've to re-work every node which does dynamic strings, but than this project should be much stronger.

its still all WIP, sorry for that. OSS economy you know ;)

Closed by #148

Yeah, it's probably not "#{"hey "}#{person}#{" there"}" what comes to my mind when I'm to have "hey #{person} there" cleaned up, but it's not a problem for my use case. Proper indentation is what mostly matters and that's finely delivered by Unparser.

That dynstring now emits nicely:

mutant-dev@mbj ~/devel/unparser (master*) $ bundle exec unparser -e '"hey #{person} there"' --verbose
(string)
Original-Source:
"hey #{person} there"
Generated-Source:
"hey #{person} there"
Original-Node:
(dstr
  (str "hey ")
  (begin
    (send nil :person))
  (str " there"))
Generated-Node:
(dstr
  (str "hey ")
  (begin
    (send nil :person))
  (str " there"))