mbj / unparser

Turn Ruby AST into semantically equivalent Ruby source

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Forward args (...)

petekinnecom opened this issue · comments

Hi there, thanks for the library. I'm working on a refactor that involves parsing and unparsing (because the file is like 1k+ lines long and a bit wacky 😹 ) and am finding your gem quite useful.

I'm wondering if this output is wrong for the given input. It believe it should include the (...) on the stuff method in order to avoid argument errors.

 ruby -r unparser -r parser <<HEREDOC
  puts(RUBY_VERSION)
  puts Parser::VERSION
  puts Gem.loaded_specs['unparser'].version.to_s
  puts '---'
  ast = Parser::CurrentRuby.parse('def stuff(...); end')
  puts ast
  puts '---'
  puts(Unparser.unparse(ast))
HEREDOC

3.3.0
3.3.0.5
0.6.13
---
(def :stuff
  (forward-args) nil)
---
def stuff
end

The lack of the forward args can cause an error. I'm not exactly sure where parser ends and unparser begins. If this is an issue, I'm happy to to take a crack at fixing it.

Thanks.

@petekinnecom I cannot reproduce this with unparsers build in self debugger:

bundle exec unparser --verbose -e 'def stuff(...); end'
Original-Source:
def stuff(...); end
Generated-Source:
def stuff(...)
end
Original-Node:
(def :stuff
  (args
    (forward-arg)) nil)
Generated-Node:
(def :stuff
  (args
    (forward-arg)) nil)

But that self debugger uses the modern AST format, if I explicitly opt your example into the modern AST format it passes:

ruby -r unparser -r parser <<HEREDOC
  Parser::Builders::Default.modernize
  puts(RUBY_VERSION)
  puts Parser::VERSION
  puts Gem.loaded_specs['unparser'].version.to_s
  puts '---'
  ast = Parser::CurrentRuby.parse('def stuff(...); end')
  puts ast
  puts '---'
  puts(Unparser.unparse(ast))
HEREDOC
3.2.3
3.3.0.5
0.6.13
---
(def :stuff
  (args
    (forward-arg)) nil)
---
def stuff(...)
end

Unparser requires the modern AST format, its simply to hard to support all the various combinations of flags so I do not even try and only support the modern format. You can also use Unparser.parse which defaults to the modern format without globally configuring the global parser to use the modern format.

and am finding your gem quite useful.

TY, any support is appreciated. (I have a sponsor button).

BTW its in unparsers README that I require the modern AST format, PRs to make this more prominent are welcome.