Skip to content

Parser: Support ternaries with transform_conditionals option#1594

Merged
marcoroth merged 2 commits intomainfrom
parser-transform_conditionals-ternary
Apr 5, 2026
Merged

Parser: Support ternaries with transform_conditionals option#1594
marcoroth merged 2 commits intomainfrom
parser-transform_conditionals-ternary

Conversation

@marcoroth
Copy link
Copy Markdown
Owner

@marcoroth marcoroth commented Apr 3, 2026

This pull request extends transform_conditionals to handle ternary expressions (condition ? 'yes' : 'no') in ERB output tags, transforming them into ERBIfNode/ERBElseNode structures.

The following:

<%= condition ? 'yes' : 'no' %>

is transformed to the equivalent with transform_conditionals: true:

<% if condition %><%= 'yes' %><% else %><%= 'no' %><% end %>

This also works inside attribute values, open tags, and nested within postfix conditionals:

<%= (condition? ? "true" : "false") if another_condition? %>

becomes:

<% if another_condition? %><% if condition? %><%= "true" %><% else %><%= "false" %><% end %><% end %>

The ternary transform is split into its own file (ternary_conditionals.c) separate from the postfix conditional transform (postfix_conditionals.c). The transform_ternary_expression function is exported so that postfix_conditionals.c can call it when a nested ternary is detected inside a postfix body.

Follows up on #1560 which added postfix support and intentionally named the option transform_conditionals to allow for this extension.

A future optimization could be to extract literal values directly as LiteralNodes or the proper equivalent. So that:

<%= condition ? 'yes' : 'no' %>

could become:

- <% if condition %><%= 'yes' %><% else %><%= 'no' %><% end %>
+ <% if condition %>yes<% else %>no<% end %>

@marcoroth marcoroth changed the title Parser transform conditionals ternary Parser: Update transform_conditionals to support ternaries Apr 3, 2026
@marcoroth marcoroth force-pushed the parser-transform_conditionals-ternary branch from 43a44cf to f0163c1 Compare April 4, 2026 23:25
@marcoroth marcoroth changed the title Parser: Update transform_conditionals to support ternaries Parser: Support ternaries with transform_conditionals option Apr 4, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 5, 2026

npx https://pkg.pr.new/@herb-tools/formatter@1594
npx https://pkg.pr.new/@herb-tools/language-server@1594
npx https://pkg.pr.new/@herb-tools/linter@1594

commit: 6475b9e

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 5, 2026

🌿 Interactive Playground and Documentation Preview

A preview deployment has been built for this pull request. Try out the changes live in the interactive playground:


🌱 Grown from commit 6475b9e


✅ Preview deployment has been cleaned up.

@marcoroth marcoroth merged commit 8150825 into main Apr 5, 2026
32 checks passed
@marcoroth marcoroth deleted the parser-transform_conditionals-ternary branch April 5, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant