diff --git a/changelog_unreleased/handlebars/11899.md b/changelog_unreleased/handlebars/11899.md new file mode 100644 index 000000000000..2d49571a9f2c --- /dev/null +++ b/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). + + +```hbs +// Input + + <:named> + + +// Prettier stable + + <:named /> + + +// Prettier main + + <:named> + +``` diff --git a/src/language-handlebars/utils.js b/src/language-handlebars/utils.js index d4831d6f1c70..abcf1a968cb2 100644 --- a/src/language-handlebars/utils.js +++ b/src/language-handlebars/utils.js @@ -32,6 +32,7 @@ function isGlimmerComponent(node) { return ( isNodeOfSomeType(node, ["ElementNode"]) && typeof node.tag === "string" && + node.tag[0] !== ":" && (isUppercase(node.tag[0]) || node.tag.includes(".")) ); } diff --git a/tests/format/handlebars/basics/__snapshots__/jsfmt.spec.js.snap b/tests/format/handlebars/basics/__snapshots__/jsfmt.spec.js.snap index 5cad2bbce4e0..987bfa99cf08 100644 --- a/tests/format/handlebars/basics/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/handlebars/basics/__snapshots__/jsfmt.spec.js.snap @@ -93,6 +93,30 @@ printWidth: 80 ================================================================================ `; +exports[`named-block.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] +printWidth: 80 + | printWidth +=====================================input====================================== + + <:first-named-block> + <:second-named-block> + <:named-block-with-comment>{{! Do not convert to an empty element}} + <:named-block-with-content>Hello + + +=====================================output===================================== + + <:first-named-block> + <:second-named-block> + <:named-block-with-comment + >{{! Do not convert to an empty element}} + <:named-block-with-content>Hello + +================================================================================ +`; + exports[`nested-path.hbs format 1`] = ` ====================================options===================================== parsers: ["glimmer"] diff --git a/tests/format/handlebars/basics/named-block.hbs b/tests/format/handlebars/basics/named-block.hbs new file mode 100644 index 000000000000..442a5a5875e5 --- /dev/null +++ b/tests/format/handlebars/basics/named-block.hbs @@ -0,0 +1,6 @@ + + <:first-named-block> + <:second-named-block> + <:named-block-with-comment>{{! Do not convert to an empty element}} + <:named-block-with-content>Hello +