[Glimmer] improve text/mustache formatting (#6206)
parent
353b2ca064
commit
e8037ff250
|
@ -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
|
||||
|
|
|
@ -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)]);
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<div>
|
||||
{{! Foo }}
|
||||
{{#if @foo}}
|
||||
Foo
|
||||
{{/if}}
|
||||
|
||||
{{! Bar }}
|
||||
{{#if @bar}}
|
||||
Bar
|
||||
{{/if}}
|
||||
</div>
|
Loading…
Reference in New Issue