From 507835fbf90949edc11b2bddb4f472f7f4a0ccb4 Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Thu, 1 Dec 2022 10:22:14 -0500 Subject: [PATCH 1/5] add failing test for else if with block params --- .../__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ .../block-statement/custom-else.hbs | 12 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap index c9e5f615504f..4f0d1577a53e 100644 --- a/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap @@ -274,6 +274,18 @@ 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}} + =====================================output=====================================

{{#when isAtWork}} @@ -381,6 +393,18 @@ 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}} ================================================================================ `; diff --git a/tests/format/handlebars/block-statement/custom-else.hbs b/tests/format/handlebars/block-statement/custom-else.hbs index 156d61268cb0..27411da62904 100644 --- a/tests/format/handlebars/block-statement/custom-else.hbs +++ b/tests/format/handlebars/block-statement/custom-else.hbs @@ -106,3 +106,15 @@ {{~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}} From 773eb07fc2d9e08f9175f860c524de08589da4b0 Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Thu, 1 Dec 2022 11:35:58 -0500 Subject: [PATCH 2/5] allow block params in custom "else if"-like blocks --- src/language-handlebars/printer-glimmer.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index 12c6b52d6965..a1c927a41e3a 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -563,6 +563,13 @@ function printElseBlock(node, options) { } function printElseIfLikeBlock(path, print, ifLikeKeyword) { + const node = path.getValue(); + let blockParams = []; + + if (isNonEmptyArray(node.program.blockParams)) { + blockParams = [" ", ...printBlockParams(node.program)]; + } + const parentNode = path.getParentNode(1); return [ @@ -571,6 +578,7 @@ function printElseIfLikeBlock(path, print, ifLikeKeyword) { ifLikeKeyword, " ", printParams(path, print), + ...blockParams, printInverseBlockClosingMustache(parentNode), ]; } From 299bb7be8ee16faced01fcdb8cdf3b14b835e9e2 Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Thu, 1 Dec 2022 11:36:25 -0500 Subject: [PATCH 3/5] add changelog entry --- changelog_unreleased/handlebars/13930.md | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 changelog_unreleased/handlebars/13930.md diff --git a/changelog_unreleased/handlebars/13930.md b/changelog_unreleased/handlebars/13930.md new file mode 100644 index 000000000000..6e824a32e02c --- /dev/null +++ b/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. + + +```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}} +``` From 22a1096edca4a0cf3f0aecf9faf04ed93f79f75a Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Mon, 5 Dec 2022 16:35:57 -0500 Subject: [PATCH 4/5] use group to allow breaking --- src/language-handlebars/printer-glimmer.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index a1c927a41e3a..19249ff52a9a 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -567,20 +567,24 @@ function printElseIfLikeBlock(path, print, ifLikeKeyword) { let blockParams = []; if (isNonEmptyArray(node.program.blockParams)) { - blockParams = [" ", ...printBlockParams(node.program)]; + blockParams = [line, printBlockParams(node.program)]; } const parentNode = path.getParentNode(1); - return [ + return group([ printInverseBlockOpeningMustache(parentNode), - "else ", - ifLikeKeyword, - " ", - printParams(path, print), - ...blockParams, + indent( + group([ + group(["else", line, ifLikeKeyword]), + line, + printParams(path, print), + ]) + ), + indent(blockParams), + softline, printInverseBlockClosingMustache(parentNode), - ]; + ]); } function printCloseBlock(path, print, options) { From 97b47a1c38032ae6a5c0237f14b87b01282c969e Mon Sep 17 00:00:00 2001 From: "James C. Davis" Date: Mon, 12 Dec 2022 10:26:28 -0500 Subject: [PATCH 5/5] add tests for wrapping long custom else --- .../__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++++++++++ .../block-statement/custom-else.hbs | 8 +++++++ 2 files changed, 32 insertions(+) diff --git a/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap index 4f0d1577a53e..8894daaed253 100644 --- a/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/block-statement/__snapshots__/jsfmt.spec.js.snap @@ -286,6 +286,14 @@ printWidth: 80 j {{/when}} +{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}} + {{b}} +{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}} + {{d}} +{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}} + {{f}} +{{/when}} + =====================================output=====================================

{{#when isAtWork}} @@ -405,6 +413,22 @@ printWidth: 80 {{else}} j {{/when}} + +{{#when + abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst + as |b| +}} + {{b}} +{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop + as |d| +}} + {{d}} +{{else when + abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz + as |f| +}} + {{f}} +{{/when}} ================================================================================ `; diff --git a/tests/format/handlebars/block-statement/custom-else.hbs b/tests/format/handlebars/block-statement/custom-else.hbs index 27411da62904..72580285e949 100644 --- a/tests/format/handlebars/block-statement/custom-else.hbs +++ b/tests/format/handlebars/block-statement/custom-else.hbs @@ -118,3 +118,11 @@ {{else}} j {{/when}} + +{{#when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst as |b|}} + {{b}} +{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnop as |d|}} + {{d}} +{{else when abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz as |f|}} + {{f}} +{{/when}}