ruby-syntax-tree / syntax_tree

Interact with the Ruby syntax tree

Home Page:https://ruby-syntax-tree.github.io/syntax_tree/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handling of macro methods with optional parenthesis

andyw8 opened this issue · comments

For macro methods, parentheses are usually optional, which results in quite different ASTs, e.g. for

test "it does something" do
  a = 1
  assert true
end

the AST is:

(program
  (statements
    ((command
        (ident "test")
        (args ((string_literal ((tstring_content "it does something")))))
        (block (bodystmt (statements ((assign (var_field (ident "a")) (int "1")), (command (ident "assert") (args ((var_ref (kw "true")))))))))))))

but with test("it does something")

(program
  (statements
    ((method_add_block
        (call nil nil (ident "test") (arg_paren (args ((string_literal ((tstring_content "it does something")))))))
        (block (bodystmt (statements ((assign (var_field (ident "a")) (int "1")), (command (ident "assert") (args ((var_ref (kw "true")))))))))))))

This means it's necessary to implement both visit_command and visit_method_add_block.

@Morriar suggested exploring if there could be a common ancestor so that only a single visit_ would be needed.

Yeah for sure. This is part of my backlog to unify the call nodes, which is part of the larger ticket of using the YARP AST instead of the current one.

Thanks @kddnewton, feel free to close this if it's already planned.

Thanks @andyw8 yeah I'll go ahead and close this for now.