ruby-syntax-tree / syntax_tree

Interact with the Ruby syntax tree
https://ruby-syntax-tree.github.io/syntax_tree/
MIT License
563 stars 54 forks source link

Handling of macro methods with optional parenthesis #330

Closed andyw8 closed 1 year ago

andyw8 commented 1 year ago

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.

kddnewton commented 1 year ago

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.

andyw8 commented 1 year ago

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

kddnewton commented 1 year ago

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