Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(glimmer): {{else}}{{#if}} into {{else if}} merging #6080

Merged
merged 4 commits into from May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}}

j-f1 marked this conversation as resolved.
Show resolved Hide resolved
=====================================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}}