File tree Expand file tree Collapse file tree
changelog_unreleased/handlebars
tests/format/handlebars/block-statement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -563,16 +563,28 @@ function printElseBlock(node, options) {
563563}
564564
565565function 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
578590function printCloseBlock ( path , print , options ) {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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 }}
You can’t perform that action at this time.
0 commit comments