Skip to content

Commit

Permalink
fix(glimmer): {{else}}{{#if}} into {{else if}} merging (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcyriller authored and evilebottnawi committed May 2, 2019
1 parent 157b020 commit 84cc273
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -80,3 +80,37 @@ Examples:
var x = (await foo.bar.blah)?.hi;
}
```
- Handlebars: Fix {{else}}{{#if}} into {{else if}} merging ([#6080] by [@dcyriller])
<!-- prettier-ignore -->
```
// Input
{{#if a}}
a
{{else}}
{{#if c}}
c
{{/if}}
e
{{/if}}

// Output (Prettier stable)
{{#if a}}
a
{{else if c}}
c
e
{{/if}}

// Output (Prettier master)
Code Sample
{{#if a}}
a
{{else}}
{{#if c}}
c
{{/if}}
e
{{/if}}
```
3 changes: 2 additions & 1 deletion src/language-handlebars/printer-glimmer.js
Expand Up @@ -106,11 +106,12 @@ function print(path, options, print) {
const isElseIf =
pp &&
pp.inverse &&
pp.inverse.body.length === 1 &&
pp.inverse.body[0] === n &&
pp.inverse.body[0].path.parts[0] === "if";
const hasElseIf =
n.inverse &&
n.inverse.body[0] &&
n.inverse.body.length === 1 &&
n.inverse.body[0].type === "BlockStatement" &&
n.inverse.body[0].path.parts[0] === "if";
const indentElse = hasElseIf ? a => a : indent;
Expand Down
72 changes: 72 additions & 0 deletions tests/glimmer/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -872,6 +872,22 @@ printWidth: 80
ac
{{/if}}
{{/if}}
{{#if a}}
a
<div>b</div>
c
{{else}}
{{#if c}}
a
b
<div>c</div>
{{/if}}
<div>a</div>
b
c
{{/if}}
=====================================output=====================================
{{#if a}}
b
Expand Down Expand Up @@ -936,6 +952,26 @@ printWidth: 80
ac
{{/if}}
{{/if}}
{{#if a}}
a
<div>
b
</div>
c
{{else}}
{{#if c}}
a
b
<div>
c
</div>
{{/if}}
<div>
a
</div>
b
c
{{/if}}
================================================================================
`;

Expand Down Expand Up @@ -1015,6 +1051,22 @@ singleQuote: true
ac
{{/if}}
{{/if}}
{{#if a}}
a
<div>b</div>
c
{{else}}
{{#if c}}
a
b
<div>c</div>
{{/if}}
<div>a</div>
b
c
{{/if}}
=====================================output=====================================
{{#if a}}
b
Expand Down Expand Up @@ -1079,6 +1131,26 @@ singleQuote: true
ac
{{/if}}
{{/if}}
{{#if a}}
a
<div>
b
</div>
c
{{else}}
{{#if c}}
a
b
<div>
c
</div>
{{/if}}
<div>
a
</div>
b
c
{{/if}}
================================================================================
`;

Expand Down
17 changes: 16 additions & 1 deletion tests/glimmer/else-if.hbs
Expand Up @@ -66,4 +66,19 @@
{{else if c}}
ac
{{/if}}
{{/if}}
{{/if}}

{{#if a}}
a
<div>b</div>
c
{{else}}
{{#if c}}
a
b
<div>c</div>
{{/if}}
<div>a</div>
b
c
{{/if}}

0 comments on commit 84cc273

Please sign in to comment.