Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix: open scope "function_definition" more often #537

Merged
merged 1 commit into from
Jun 27, 2023
Merged

Conversation

nbacquey
Copy link
Member

Closes #529
Modifies #536

@nbacquey
Copy link
Member Author

Fun fact: I wanted to write

(
  _ @begin_scope
  .
  (function_expression
    "function" @append_spaced_scoped_softline
  ) @end_scope
  (#scope_id! "function_definition")
)

With _ instead of (_), to capture both named and non-named nodes, and get rid of the two cases above which open the function_definition scope.
Alas, the snippet above is not syntactically correct, for some reason?

@nbacquey nbacquey merged commit 7d2dcd6 into main Jun 27, 2023
@nbacquey nbacquey deleted the fix_function_scope branch June 27, 2023 10:05
@nbacquey nbacquey mentioned this pull request Jun 27, 2023
@torhovland
Copy link
Member

Unfortunately, your "fun" fact is causing problems.

(_) is not capturing the else block, because it is unnamed. And _ causes a query error, as you say. This actually works, but is not very general:

(
  "else" @begin_scope
  .
  (function_expression
    "function" @append_spaced_scoped_softline
  ) @end_scope
  (#scope_id! "function_definition")
)

@torhovland
Copy link
Member

This also doesn't work, and I'm not sure why:

(
  (_) @begin_scope
  .
  (
    (function_expression
      "function" @append_spaced_scoped_softline
    ) @end_scope
  )
  (#scope_id! "function_definition")
)

@nbacquey
Copy link
Member Author

I've thought about something: even if the following was a correct query, we shouldn't use it:

(
  _ @begin_scope
  .
  (function_expression
    "function" @append_spaced_scoped_softline
  ) @end_scope
  (#scope_id! "function_definition")
)

That's because we know . can skip anonymous nodes, so if we have something like (let) "=" (function_expression), then _ @begin_scope would match on both (let) and "=", and the scope would be opened twice, in different places.

@torhovland
Copy link
Member

Right, but both of them would also be closed. Wouldn't that be OK?

@nbacquey
Copy link
Member Author

That would be okay-ish. Both scopes would be closed indeed, but weird thing could happen if there were matches that use the function_definition scope between (let) and "=", like a scoped newline, or worse, closing a previously opened scope.

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

Successfully merging this pull request may close these issues.

Idempotency error (and other peculiar behaviours) with function inside an if-then-else construct
2 participants