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

This commit is contained in:
James C. Davis authored and GitHub committed 2022-12-15 14:50:51 +08:00
1 parent df1f6ad1ac
commit 40d0f27055
4 files changed
+128 -6

No files matched your search

+42
View File
@@ -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}}
```
+18 -6
View File
@@ -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) {
@@ -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}}
@@ -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}}
================================================================================
`;
@@ -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}}