Skip to content

Commit

Permalink
[handlebars] Named blocks can't be self closing
Browse files Browse the repository at this point in the history
  • Loading branch information
duailibe committed Dec 1, 2021
1 parent d095459 commit 65751bf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions changelog_unreleased/handlebars/11899.md
@@ -0,0 +1,23 @@
#### Support Glimmer's named blocks (#11899 by @duailibe)

Prettier already supported this feature, but it converted empty named blocks to self-closing, which is not supported by the Glimmer compiler.

See: [Glimmer's named blocks](https://emberjs.github.io/rfcs/0460-yieldable-named-blocks.html).

<!-- prettier-ignore -->
```jsx
// Input
<Component>
<:named></:named>
</Component>

// Prettier stable
<Component>
<:named />
</Component>

// Prettier main
<Component>
<:named></:named>
</Component>
```
5 changes: 5 additions & 0 deletions src/language-handlebars/utils.js
Expand Up @@ -36,10 +36,15 @@ function isGlimmerComponent(node) {
);
}

function isNamedBlock(node) {
return node.tag[0] === ":";
}

const voidTags = new Set(htmlVoidElements);
function isVoid(node) {
return (
(isGlimmerComponent(node) &&
!isNamedBlock(node) &&
node.children.every((node) => isWhitespaceNode(node))) ||
voidTags.has(node.tag)
);
Expand Down
24 changes: 24 additions & 0 deletions tests/format/handlebars/basics/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -93,6 +93,30 @@ printWidth: 80
================================================================================
`;

exports[`named-block.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<ComponentWithNamedBlocks>
<:first-named-block></:first-named-block>
<:second-named-block> </:second-named-block>
<:named-block-with-comment>{{! Do not convert to an empty element}}</:named-block-with-comment>
<:named-block-with-content>Hello</:named-block-with-content>
</ComponentWithNamedBlocks>
=====================================output=====================================
<ComponentWithNamedBlocks>
<:first-named-block></:first-named-block>
<:second-named-block> </:second-named-block>
<:named-block-with-comment
>{{! Do not convert to an empty element}}</:named-block-with-comment>
<:named-block-with-content>Hello</:named-block-with-content>
</ComponentWithNamedBlocks>
================================================================================
`;

exports[`nested-path.hbs format 1`] = `
====================================options=====================================
parsers: ["glimmer"]
Expand Down
6 changes: 6 additions & 0 deletions tests/format/handlebars/basics/named-block.hbs
@@ -0,0 +1,6 @@
<ComponentWithNamedBlocks>
<:first-named-block></:first-named-block>
<:second-named-block> </:second-named-block>
<:named-block-with-comment>{{! Do not convert to an empty element}}</:named-block-with-comment>
<:named-block-with-content>Hello</:named-block-with-content>
</ComponentWithNamedBlocks>

0 comments on commit 65751bf

Please sign in to comment.