Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the (anonymous) function body include brackets? #110

Open
rawhat opened this issue Feb 2, 2025 · 1 comment · May be fixed by #111
Open

Should the (anonymous) function body include brackets? #110

rawhat opened this issue Feb 2, 2025 · 1 comment · May be fixed by #111

Comments

@rawhat
Copy link
Contributor

rawhat commented Feb 2, 2025

Hello!

I've tried these changes locally and for what I need it does work. But I wanted to run it by here before just throwing up a PR. Currently, both function and anonymous function define the actual body as:

optional(
  seq(
    "{",
    optional(field("body", alias($._statement_seq, $.function_body))),
    "}"
  )
)

What this means is that a TS query will not include the brackets as part of the "function body". Conceptually this makes sense, they're just a syntatic choice. However, I'd like to be able to use https://github.com/Wansmer/treesj to split/join the function body, i.e.

fn() { tod|o } // -> press split
fn() {
  tod|o
}
// and join returns to the top pattern

I think because the brackets aren't included, it doesn't know how to parse the actual function body in a way that works with split/join.

What I've changed locally (that seems to work) is to update function and anonymous_function to use

optional(field("body", alias($.block, $.function_body)))

instead.

Just curious, does this make sense? If so, I can put up a PR.

@the-mikedavis
Copy link
Member

That makes senes to me. Other similar languages like Rust use a block node for function bodies. We could consider removing the alias as well - it's already easy to pattern match in a query since it's in the body field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants