[Glimmer] improve text/mustache formatting (#6206)

master
Gavin Joyce 2019-06-19 13:53:27 +01:00 committed by Lucas Duailibe
parent 353b2ca064
commit e8037ff250
4 changed files with 122 additions and 2 deletions

View File

@ -104,7 +104,50 @@ Previously, Prettier added line breaks between text and mustaches which resulted
</p>
```
### Handlebars: Improve comment formatting ([#6206] by [@gavinjoyce])
Previously, Prettier would sometimes ignore whitespace when formatting comments.
<!-- prettier-ignore -->
```hbs
// Input
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
// Output (Prettier stable)
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}{{! Bar }}{{#if @bar}}
Bar
{{/if}}
</div>
// Output (Prettier master)
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
```
[#6209]: https://github.com/prettier/prettier/pull/6209
[#6186]: https://github.com/prettier/prettier/pull/6186
[#6186]: https://github.com/prettier/prettier/pull/6206
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce

View File

@ -35,19 +35,22 @@ const voidTags = [
function printChildren(path, options, print) {
return concat(
path.map((childPath, childIndex) => {
const childNode = path.getValue();
const isFirstNode = childIndex === 0;
const isLastNode =
childIndex == path.getParentNode(0).children.length - 1;
const isLastNodeInMultiNodeList = isLastNode && !isFirstNode;
const isWhitespace = isWhitespaceNode(childNode);
if (isLastNodeInMultiNodeList) {
if (isWhitespace && isLastNodeInMultiNodeList) {
return concat([print(childPath, options, print)]);
} else if (
isFirstNode ||
isPreviousNodeOfSomeType(childPath, [
"ElementNode",
"CommentStatement",
"MustacheCommentStatement"
"MustacheCommentStatement",
"BlockStatement"
])
) {
return concat([softline, print(childPath, options, print)]);

View File

@ -293,6 +293,69 @@ singleQuote: true
================================================================================
`;
exports[`comment.hbs 1`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
| printWidth
=====================================input======================================
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
=====================================output=====================================
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
================================================================================
`;
exports[`comment.hbs 2`] = `
====================================options=====================================
parsers: ["glimmer"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
=====================================output=====================================
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>
================================================================================
`;
exports[`component.hbs 1`] = `
====================================options=====================================
parsers: ["glimmer"]

11
tests/glimmer/comment.hbs Normal file
View File

@ -0,0 +1,11 @@
<div>
{{! Foo }}
{{#if @foo}}
Foo
{{/if}}
{{! Bar }}
{{#if @bar}}
Bar
{{/if}}
</div>