Skip to content

Commit 40d0f27

Browse files
authored
[handlebars] allow custom "else if"-like with block params (#13930)
1 parent df1f6ad commit 40d0f27

4 files changed

Lines changed: 128 additions & 6 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#### Allow custom "else if"-like blocks with block params (#13930 by @jamescdavis)
2+
3+
#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.
4+
5+
<!-- prettier-ignore -->
6+
```hbs
7+
{{! Input }}
8+
{#when isAtWork as |work|}}
9+
Ship that
10+
{{work}}!
11+
{{else when isReading as |book|}}
12+
You can finish
13+
{{book}}
14+
eventually...
15+
{{else}}
16+
Go to bed!
17+
{{/when}}
18+
19+
{{! Prettier stable }}
20+
{{#when isAtWork as |work|}}
21+
Ship that
22+
{{work}}!
23+
{{else when isReading}}
24+
You can finish
25+
{{book}}
26+
eventually...
27+
{{else}}
28+
Go to bed!
29+
{{/when}}
30+
31+
{{! Prettier main }}
32+
{#when isAtWork as |work|}}
33+
Ship that
34+
{{work}}!
35+
{{else when isReading as |book|}}
36+
You can finish
37+
{{book}}
38+
eventually...
39+
{{else}}
40+
Go to bed!
41+
{{/when}}
42+
```

src/language-handlebars/printer-glimmer.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,28 @@ function printElseBlock(node, options) {
563563
}
564564

565565
function printElseIfLikeBlock(path, print, ifLikeKeyword) {
566+
const node = path.getValue();
567+
let blockParams = [];
568+
569+
if (isNonEmptyArray(node.program.blockParams)) {
570+
blockParams = [line, printBlockParams(node.program)];
571+
}
572+
566573
const parentNode = path.getParentNode(1);
567574

568-
return [
575+
return group([
569576
printInverseBlockOpeningMustache(parentNode),
570-
"else ",
571-
ifLikeKeyword,
572-
" ",
573-
printParams(path, print),
577+
indent(
578+
group([
579+
group(["else", line, ifLikeKeyword]),
580+
line,
581+
printParams(path, print),
582+
])
583+
),
584+
indent(blockParams),
585+
softline,
574586
printInverseBlockClosingMustache(parentNode),
575-
];
587+
]);
576588
}
577589

578590
function printCloseBlock(path, print, options) {

tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ printWidth: 80
274274
Another thing
275275
{{~/when~}}
276276
277+
{{#when a as |b|}}
278+
{{b}}
279+
{{else when c as |d|}}
280+
{{d}}
281+
{{else when e as |f|}}
282+
{{f}}
283+
{{else when g as |h|}}
284+
{{h}}
285+
{{else}}
286+
j
287+
{{/when}}
288+
289+
{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}}
290+
{{b}}
291+
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}}
292+
{{d}}
293+
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}}
294+
{{f}}
295+
{{/when}}
296+
277297
=====================================output=====================================
278298
<h1>
279299
{{#when isAtWork}}
@@ -381,6 +401,34 @@ printWidth: 80
381401
{{~else when anotherCondition~}}
382402
Another thing
383403
{{~/when~}}
404+
405+
{{#when a as |b|}}
406+
{{b}}
407+
{{else when c as |d|}}
408+
{{d}}
409+
{{else when e as |f|}}
410+
{{f}}
411+
{{else when g as |h|}}
412+
{{h}}
413+
{{else}}
414+
j
415+
{{/when}}
416+
417+
{{#when
418+
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst
419+
as |b|
420+
}}
421+
{{b}}
422+
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop
423+
as |d|
424+
}}
425+
{{d}}
426+
{{else when
427+
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
428+
as |f|
429+
}}
430+
{{f}}
431+
{{/when}}
384432
================================================================================
385433
`;
386434

tests/format/handlebars/block-statement/custom-else.hbs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,23 @@
106106
{{~else when anotherCondition~}}
107107
Another thing
108108
{{~/when~}}
109+
110+
{{#when a as |b|}}
111+
{{b}}
112+
{{else when c as |d|}}
113+
{{d}}
114+
{{else when e as |f|}}
115+
{{f}}
116+
{{else when g as |h|}}
117+
{{h}}
118+
{{else}}
119+
j
120+
{{/when}}
121+
122+
{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}}
123+
{{b}}
124+
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}}
125+
{{d}}
126+
{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}}
127+
{{f}}
128+
{{/when}}

0 commit comments

Comments
 (0)