Skip to content

Parser: Add transform_conditionals option for postfix conditionals#1560

Merged
marcoroth merged 4 commits intomainfrom
parser-transform_conditionals
Apr 4, 2026
Merged

Parser: Add transform_conditionals option for postfix conditionals#1560
marcoroth merged 4 commits intomainfrom
parser-transform_conditionals

Conversation

@marcoroth
Copy link
Copy Markdown
Owner

@marcoroth marcoroth commented Mar 31, 2026

This pull request adds a new transform_conditionals parser option that transforms ERB output tags with postfix if/unless conditionals into proper ERBIfNode/ERBUnlessNode structures wrapping the body expression.

The following:

<%= 'aria-current=page' if selected %>

is transformed to the equivalent with transform_conditionals: true:

<% if selected %><%= 'aria-current=page' %><% end %>

The option is named transform_conditionals (rather than transform_postfix) to allow extending it to also handle ternary transformations in the future.

Builds on the postfix conditional support added for Action View helpers in #1494, extending it to all ERB output expressions.

The motivation for this is improve the detection of conditionals since they previously weren't surfaced as ERBIfNodes if they were fully self contained in an ERBContentNode. This will also make the static analysis easier when we want get to analyze template dependencies and conditionals for reactivity.

@marcoroth marcoroth force-pushed the parser-transform_conditionals branch from a260c29 to 668c77d Compare March 31, 2026 23:50
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 1, 2026

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

commit: f810990

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 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 f810990


✅ Preview deployment has been cleaned up.

@marcoroth marcoroth changed the title Parser: Implement transform_conditionals parser option Parser: Add transform_conditionals parser option for postfix conditionals Apr 4, 2026
@marcoroth marcoroth changed the title Parser: Add transform_conditionals parser option for postfix conditionals Parser: Add transform_conditionals option for postfix conditionals Apr 4, 2026
@marcoroth marcoroth merged commit 166fad6 into main Apr 4, 2026
32 checks passed
@marcoroth marcoroth deleted the parser-transform_conditionals branch April 4, 2026 22:57
@marcoroth marcoroth added this to the v1.0.0 milestone Apr 4, 2026
marcoroth added a commit that referenced this pull request Apr 5, 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:
```erb
<%= condition ? 'yes' : 'no' %>
```

is transformed to the equivalent with `transform_conditionals: true`:
```erb
<% if condition %><%= 'yes' %><% else %><%= 'no' %><% end %>
```

This also works inside attribute values, open tags, and nested within
postfix conditionals:
```erb
<%= (condition? ? "true" : "false") if another_condition? %>
```

becomes:
```erb
<% 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
`LiteralNode`s or the proper equivalent. So that:
```erb
<%= condition ? 'yes' : 'no' %>
```

could become:
```diff
- <% if condition %><%= 'yes' %><% else %><%= 'no' %><% end %>
+ <% if condition %>yes<% else %>no<% end %>
```
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