Skip to content

Commit

Permalink
feat(angular): do not print colon for ngIf (#5542)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 25, 2018
1 parent b6a04ab commit 66ada24
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
24 changes: 16 additions & 8 deletions src/language-js/printer-estree.js
Expand Up @@ -3505,7 +3505,7 @@ function printPathNoParens(path, options, print, args) {
concat([
index === 0
? ""
: isNgForOf(childPath)
: isNgForOf(childPath.getValue(), index, n)
? " "
: concat([";", line]),
print(childPath)
Expand All @@ -3522,12 +3522,24 @@ function printPathNoParens(path, options, print, args) {
path.call(print, "expression"),
n.alias === null ? "" : concat([" as ", path.call(print, "alias")])
]);
case "NGMicrosyntaxKeyedExpression":
case "NGMicrosyntaxKeyedExpression": {
const index = path.getName();
const parentNode = path.getParentNode();
const shouldNotPrintColon =
isNgForOf(n, index, parentNode) ||
(((index === 1 && (n.key.name === "then" || n.key.name === "else")) ||
(index === 2 &&
(n.key.name === "else" &&
parentNode.body[index - 1].type ===
"NGMicrosyntaxKeyedExpression" &&
parentNode.body[index - 1].key.name === "then"))) &&
parentNode.body[0].type === "NGMicrosyntaxExpression");
return concat([
path.call(print, "key"),
isNgForOf(path) ? " " : ": ",
shouldNotPrintColon ? " " : ": ",
path.call(print, "expression")
]);
}
case "NGMicrosyntaxLet":
return concat([
"let ",
Expand All @@ -3546,11 +3558,7 @@ function printPathNoParens(path, options, print, args) {
}
}

/** prefer `let hero of heros` over `let hero; of: heros` */
function isNgForOf(path) {
const node = path.getValue();
const index = path.getName();
const parentNode = path.getParentNode();
function isNgForOf(node, index, parentNode) {
return (
node.type === "NGMicrosyntaxKeyedExpression" &&
node.key.name === "of" &&
Expand Down
37 changes: 24 additions & 13 deletions tests/html_angular/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -60,6 +60,7 @@ exports[`attributes.component.html - angular-verify 1`] = `
*ngFor=" let item of items ; index as i ; trackBy : trackByFn"
*ngFor=" let hero of heroes; let i = index"
*ngFor=" let hero of heroes; value myValue"
*ngIf=" condition ; else elseBlock "
*ngIf=" condition ; then thenBlock else elseBlock "
*ngIf=" condition as value ; else elseBlock "
*directive=" let hero "
Expand Down Expand Up @@ -150,8 +151,9 @@ exports[`attributes.component.html - angular-verify 1`] = `
*ngFor="let item of items; index as i; trackBy: trackByFn"
*ngFor="let hero of heroes; let i = index"
*ngFor="let hero of heroes; value: myValue"
*ngIf="condition; then: thenBlock; else: elseBlock"
*ngIf="condition as value; else: elseBlock"
*ngIf="condition; else elseBlock"
*ngIf="condition; then thenBlock; else elseBlock"
*ngIf="condition as value; else elseBlock"
*directive="let hero"
*directive="let hero = hello"
*directive="let hero of heroes"
Expand All @@ -173,7 +175,7 @@ exports[`attributes.component.html - angular-verify 1`] = `
listRow.NextScheduledSendStatus == 1 ||
listRow.NextScheduledSendStatus == 2 ||
listRow.NextScheduledSendStatus == 3;
else: hello
else hello
"
(target)="
(listRow.NextScheduledSendStatus == 1 ||
Expand Down Expand Up @@ -260,6 +262,7 @@ exports[`attributes.component.html - angular-verify 2`] = `
*ngFor=" let item of items ; index as i ; trackBy : trackByFn"
*ngFor=" let hero of heroes; let i = index"
*ngFor=" let hero of heroes; value myValue"
*ngIf=" condition ; else elseBlock "
*ngIf=" condition ; then thenBlock else elseBlock "
*ngIf=" condition as value ; else elseBlock "
*directive=" let hero "
Expand Down Expand Up @@ -350,8 +353,9 @@ exports[`attributes.component.html - angular-verify 2`] = `
*ngFor="let item of items; index as i; trackBy: trackByFn"
*ngFor="let hero of heroes; let i = index"
*ngFor="let hero of heroes; value: myValue"
*ngIf="condition; then: thenBlock; else: elseBlock"
*ngIf="condition as value; else: elseBlock"
*ngIf="condition; else elseBlock"
*ngIf="condition; then thenBlock; else elseBlock"
*ngIf="condition as value; else elseBlock"
*directive="let hero"
*directive="let hero = hello"
*directive="let hero of heroes"
Expand All @@ -373,7 +377,7 @@ exports[`attributes.component.html - angular-verify 2`] = `
listRow.NextScheduledSendStatus == 1 ||
listRow.NextScheduledSendStatus == 2 ||
listRow.NextScheduledSendStatus == 3;
else: hello
else hello
"
(target)="
(listRow.NextScheduledSendStatus == 1 ||
Expand Down Expand Up @@ -460,6 +464,7 @@ exports[`attributes.component.html - angular-verify 3`] = `
*ngFor=" let item of items ; index as i ; trackBy : trackByFn"
*ngFor=" let hero of heroes; let i = index"
*ngFor=" let hero of heroes; value myValue"
*ngIf=" condition ; else elseBlock "
*ngIf=" condition ; then thenBlock else elseBlock "
*ngIf=" condition as value ; else elseBlock "
*directive=" let hero "
Expand Down Expand Up @@ -697,12 +702,16 @@ exports[`attributes.component.html - angular-verify 3`] = `
"
*ngIf="
condition;
then: thenBlock;
else: elseBlock
else elseBlock
"
*ngIf="
condition;
then thenBlock;
else elseBlock
"
*ngIf="
condition as value;
else: elseBlock
else elseBlock
"
*directive="
let hero
Expand Down Expand Up @@ -761,7 +770,7 @@ exports[`attributes.component.html - angular-verify 3`] = `
2 ||
listRow.NextScheduledSendStatus ==
3;
else: hello
else hello
"
(target)="
(listRow.NextScheduledSendStatus ==
Expand Down Expand Up @@ -884,6 +893,7 @@ exports[`attributes.component.html - angular-verify 4`] = `
*ngFor=" let item of items ; index as i ; trackBy : trackByFn"
*ngFor=" let hero of heroes; let i = index"
*ngFor=" let hero of heroes; value myValue"
*ngIf=" condition ; else elseBlock "
*ngIf=" condition ; then thenBlock else elseBlock "
*ngIf=" condition as value ; else elseBlock "
*directive=" let hero "
Expand Down Expand Up @@ -974,8 +984,9 @@ exports[`attributes.component.html - angular-verify 4`] = `
*ngFor="let item of items; index as i; trackBy: trackByFn"
*ngFor="let hero of heroes; let i = index"
*ngFor="let hero of heroes; value: myValue"
*ngIf="condition; then: thenBlock; else: elseBlock"
*ngIf="condition as value; else: elseBlock"
*ngIf="condition; else elseBlock"
*ngIf="condition; then thenBlock; else elseBlock"
*ngIf="condition as value; else elseBlock"
*directive="let hero"
*directive="let hero = hello"
*directive="let hero of heroes"
Expand All @@ -997,7 +1008,7 @@ exports[`attributes.component.html - angular-verify 4`] = `
listRow.NextScheduledSendStatus == 1 ||
listRow.NextScheduledSendStatus == 2 ||
listRow.NextScheduledSendStatus == 3;
else: hello
else hello
"
(target)="
(listRow.NextScheduledSendStatus == 1 ||
Expand Down
1 change: 1 addition & 0 deletions tests/html_angular/attributes.component.html
Expand Up @@ -57,6 +57,7 @@
*ngFor=" let item of items ; index as i ; trackBy : trackByFn"
*ngFor=" let hero of heroes; let i = index"
*ngFor=" let hero of heroes; value myValue"
*ngIf=" condition ; else elseBlock "
*ngIf=" condition ; then thenBlock else elseBlock "
*ngIf=" condition as value ; else elseBlock "
*directive=" let hero "
Expand Down

0 comments on commit 66ada24

Please sign in to comment.