Skip to content

Commit

Permalink
[handlebars] allow custom "else if"-like with block params (prettier#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescdavis authored and medikoo committed Jan 4, 2024
1 parent 96d5dd0 commit e78a2ee
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 6 deletions.
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 @@ -564,16 +564,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}}

0 comments on commit e78a2ee

Please sign in to comment.