fix(issue-3982): Glimmer printer now properly handles else-if blocks without else block … (#4256)

* fix(glimmer): Now properly handles else-if blocks without else block and block statements as root of else.

* run prettier on printer-glimmer.js
master
Nate Pendleton 2018-04-08 10:36:37 -07:00 committed by Suchipi
parent 0c1d62079e
commit 28e4b07b17
3 changed files with 58 additions and 3 deletions

View File

@ -96,11 +96,16 @@ function print(path, options, print) {
}
case "BlockStatement": {
const pp = path.getParentNode(1);
const isElseIf = pp && pp.inverse && pp.inverse.body[0] === n;
const isElseIf =
pp &&
pp.inverse &&
pp.inverse.body[0] === n &&
pp.inverse.body[0].path.parts[0] === "if";
const hasElseIf =
n.inverse &&
n.inverse.body[0] &&
n.inverse.body[0].type === "BlockStatement";
n.inverse.body[0].type === "BlockStatement" &&
n.inverse.body[0].path.parts[0] === "if";
const indentElse = hasElseIf ? a => a : indent;
if (n.inverse) {
return concat([
@ -114,6 +119,11 @@ function print(path, options, print) {
: "",
isElseIf ? "" : concat([hardline, printCloseBlock(path, print)])
]);
} else if (isElseIf) {
return concat([
concat(["{{else ", printPathParams(path, print), "}}"]),
indent(concat([hardline, path.call(print, "program")]))
]);
}
/**
* I want this boolean to be: if params are going to cause a break,

View File

@ -321,7 +321,22 @@ exports[`else-if.hbs 1`] = `
{{/if}}
</div>
</div>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{{#if a}}
b
{{else}}
{{#each c as |d|}}
e
{{/each}}
{{/if}}
{{#if a}}
{{#if b}}
ab
{{else if c}}
ac
{{/if}}
{{/if}}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{{#if a}}
b
{{else if c}}
@ -371,6 +386,20 @@ exports[`else-if.hbs 1`] = `
{{/if}}
</div>
</div>
{{#if a}}
b
{{else}}
{{#each c as |d|}}
e
{{/each}}
{{/if}}
{{#if a}}
{{#if b}}
ab
{{else if c}}
ac
{{/if}}
{{/if}}
`;
exports[`literals.hbs 1`] = `

View File

@ -51,3 +51,19 @@
{{/if}}
</div>
</div>
{{#if a}}
b
{{else}}
{{#each c as |d|}}
e
{{/each}}
{{/if}}
{{#if a}}
{{#if b}}
ab
{{else if c}}
ac
{{/if}}
{{/if}}