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

[handlebars] allow custom "else if"-like with block params #13930

Merged
merged 5 commits into from Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions changelog_unreleased/handlebars/13930.md
@@ -0,0 +1,42 @@
#### Allow custom "else if"-like blocks with block params (#13930 by @jamescdavis)

#13507 added support for custom block keywords used with `else`, but failed to allow block params. This updates printer-glimmer to allow block params with custom "else if"-like blocks.

<!-- prettier-ignore -->
```hbs
{{! Input }}
{#when isAtWork as |work|}}
Ship that
{{work}}!
{{else when isReading as |book|}}
You can finish
{{book}}
eventually...
{{else}}
Go to bed!
{{/when}}

{{! Prettier stable }}
{{#when isAtWork as |work|}}
Ship that
{{work}}!
{{else when isReading}}
You can finish
{{book}}
eventually...
{{else}}
Go to bed!
{{/when}}

{{! Prettier main }}
{#when isAtWork as |work|}}
Ship that
{{work}}!
{{else when isReading as |book|}}
You can finish
{{book}}
eventually...
{{else}}
Go to bed!
{{/when}}
```
24 changes: 18 additions & 6 deletions src/language-handlebars/printer-glimmer.js
Expand Up @@ -563,16 +563,28 @@ function printElseBlock(node, options) {
}

function printElseIfLikeBlock(path, print, ifLikeKeyword) {
const node = path.getValue();
let blockParams = [];

if (isNonEmptyArray(node.program.blockParams)) {
blockParams = [line, printBlockParams(node.program)];
}

const parentNode = path.getParentNode(1);

return [
return group([
printInverseBlockOpeningMustache(parentNode),
"else ",
ifLikeKeyword,
" ",
printParams(path, print),
indent(
group([
group(["else", line, ifLikeKeyword]),
line,
printParams(path, print),
])
),
indent(blockParams),
softline,
printInverseBlockClosingMustache(parentNode),
];
]);
}

function printCloseBlock(path, print, options) {
Expand Down
Expand Up @@ -274,6 +274,26 @@ printWidth: 80
Another thing
{{~/when~}}

{{#when a as |b|}}
{{b}}
{{else when c as |d|}}
{{d}}
{{else when e as |f|}}
{{f}}
{{else when g as |h|}}
{{h}}
{{else}}
j
{{/when}}

{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}}
{{b}}
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}}
{{d}}
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}}
{{f}}
{{/when}}

=====================================output=====================================
<h1>
{{#when isAtWork}}
Expand Down Expand Up @@ -381,6 +401,34 @@ printWidth: 80
{{~else when anotherCondition~}}
Another thing
{{~/when~}}

{{#when a as |b|}}
{{b}}
{{else when c as |d|}}
{{d}}
{{else when e as |f|}}
{{f}}
{{else when g as |h|}}
{{h}}
{{else}}
j
{{/when}}

{{#when
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst
as |b|
}}
{{b}}
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop
as |d|
}}
{{d}}
{{else when
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
as |f|
}}
{{f}}
{{/when}}
================================================================================
`;

Expand Down
20 changes: 20 additions & 0 deletions tests/format/handlebars/block-statement/custom-else.hbs
Expand Up @@ -106,3 +106,23 @@
{{~else when anotherCondition~}}
Another thing
{{~/when~}}

{{#when a as |b|}}
{{b}}
{{else when c as |d|}}
{{d}}
{{else when e as |f|}}
{{f}}
{{else when g as |h|}}
{{h}}
{{else}}
j
{{/when}}

{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}}
{{b}}
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}}
{{d}}
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}}
{{f}}
{{/when}}