Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[handlebars] group params in opening block statements #14067

Merged
57 changes: 57 additions & 0 deletions changelog_unreleased/handlebars/14067.md
@@ -0,0 +1,57 @@
#### Group params in opening block statements (#14067 by @jamescdavis)

This is a follow-up to #13930 to establish wrapping consistency between opening block statements and else blocks by
grouping params in opening blocks. This causes params to break to a new line together and not be split across lines
unless the length of params exceeds the print width. This also updates the else block wrapping to behave exactly the
same as opening blocks.

<!-- prettier-ignore -->
```hbs
{{! Input }}
{{#block param param param param param param param param param param as |blockParam|}}
Hello
{{else block param param param param param param param param param param as |blockParam|}}
There
{{/block}}

{{! Prettier stable }}
{{#block
param
param
param
param
param
param
param
param
param
param
as |blockParam|
}}
Hello
{{else block param
param
param
param
param
param
param
param
param
param}}
There
{{/block}}

{{! Prettier main }}
{{#block
param param param param param param param param param param
as |blockParam|
}}
Hello
{{else block
param param param param param param param param param param
as |blockParam|
}}
There
{{/block}}
```
49 changes: 22 additions & 27 deletions src/language-handlebars/printer-glimmer.js
Expand Up @@ -30,6 +30,10 @@ const {
isWhitespaceNode,
} = require("./utils.js");

/**
* @typedef {import("../document").Doc} Doc
*/

const NEWLINES_TO_PRESERVE_MAX = 2;

// Formatter based on @glimmerjs/syntax's built-in test formatter:
Expand Down Expand Up @@ -529,27 +533,24 @@ function printInverseBlockClosingMustache(node) {

function printOpenBlock(path, print) {
const node = path.getValue();
/** @type {Doc[]} */
const parts = [];

const openingMustache = printOpeningBlockOpeningMustache(node);
const closingMustache = printOpeningBlockClosingMustache(node);

const attributes = [printPath(path, print)];

const params = printParams(path, print);
if (params) {
attributes.push(line, params);
const paramsDoc = printParams(path, print);
if (paramsDoc) {
parts.push(group(paramsDoc));
}

if (isNonEmptyArray(node.program.blockParams)) {
const block = printBlockParams(node.program);
attributes.push(line, block);
parts.push(printBlockParams(node.program));
}

return group([
openingMustache,
indent(attributes),
printOpeningBlockOpeningMustache(node),
printPath(path, print),
parts.length > 0 ? indent([line, join(line, parts)]) : "",
softline,
closingMustache,
printOpeningBlockClosingMustache(node),
]);
}

Expand All @@ -564,24 +565,18 @@ 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 group([
printInverseBlockOpeningMustache(parentNode),
indent(
group([
group(["else", line, ifLikeKeyword]),
line,
printParams(path, print),
])
),
indent(blockParams),
["else", " ", ifLikeKeyword],
indent([
line,
group(printParams(path, print)),
...(isNonEmptyArray(node.program.blockParams)
? [line, printBlockParams(node.program)]
: []),
]),
softline,
printInverseBlockClosingMustache(parentNode),
]);
Expand Down