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

feat(angular): do not print colon for ngIf #5542

Merged
merged 2 commits into from Nov 25, 2018
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
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