Skip to content

Native support for else and else if in block statements - #175

Open
mogstad wants to merge 2 commits into
ember-tooling:mainfrom
mogstad:push-nksopsusrmto
Open

Native support for else and else if in block statements#175
mogstad wants to merge 2 commits into
ember-tooling:mainfrom
mogstad:push-nksopsusrmto

Conversation

@mogstad

@mogstad mogstad commented Mar 19, 2025

Copy link
Copy Markdown

As there isn't a native concept of if statements in the grammar, it makes it especially hard to query else if statements, as they've previously just been exposed as a helper. It makes it cumbersome to query else blocks and not possible—I think at least—to query and capture else if as the same capture.

else is supported for all block_statements in Ember, but else if isn't, but it wouldn't be valid syntax that we want to capture any other way, so I haven't tried to be cleaver about it.

I’m unsure if this is the way to solve, but I don’t think it’s unreasonable to have “native” support for it.

@mogstad
mogstad force-pushed the push-nksopsusrmto branch 3 times, most recently from 47f68db to 8e0ab76 Compare March 26, 2025 15:27
@NullVoxPopuli

Copy link
Copy Markdown
Member

sorry it took so long to see this PR! :(

what is the motivation for this change? I'm not sure I understand

@mogstad

mogstad commented Jul 6, 2026

Copy link
Copy Markdown
Author

The motivation is to move the burden of selecting "else" and "else if" in block statements from queries, to the parser, and treat else and else if a first class concepts (as block_statement_inverse). Querying else if is today rather compiled.

To query else you can do: (which is complicated, but fine, and is equal to if)

(mustache_statement
	(identifier) @keyword
	(#eq? @keyword "else"))

But "else if" on the other hand: I wasn't able to implement in Zed, so I don't have the actual query but conceptually its query needs to be:

  • A helper_invocation
    • with a helper: that is an identifier that equals else.
    • its first argument: is an identifier that equals if.
    • (ideally we ensure it has another argument)

Conceptually this doesn't match what we expect. I don't think of else if as a helper named else with its first argument the identifier if.

If's are still kept as mustache_statement and an identifier as it needs to support many keywords (each, each-in, ...), but the inverse is always else (and else if for ifs.)

TLDR I want my else if's to syntax highlighted correctly:

image

@NullVoxPopuli

Copy link
Copy Markdown
Member

Tyty. Seems ok to me

mogstad added 2 commits July 7, 2026 09:25
As there isn't a native concept of if statements in the grammar, it
makes it especially hard to query `else if` statements, as they've
previously just been exposed as a helper. It makes it cumbersome to
query `else` blocks and not possible—I think at least—to query and
capture `else if` as the same capture.

`else` is supported for all block_statements in Ember, but `else if`
isn't, but it wouldn't be valid syntax that we want to capture any other
way, so I haven't tried to be cleaver about it.
@mogstad
mogstad force-pushed the push-nksopsusrmto branch from 8e0ab76 to cb4b52e Compare July 7, 2026 07:26
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 this pull request may close these issues.

2 participants