Skip to content

Commit 8a88cdc

Browse files
fiskersosukesuzuki
andauthored
Respect trailingComma in angular templates (#15926)
Co-authored-by: Sosuke Suzuki <[email protected]>
1 parent c2e20fb commit 8a88cdc

File tree

10 files changed

+761
-40
lines changed

10 files changed

+761
-40
lines changed

changelog_unreleased/angular/15926.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#### Respect `trailingComma` in Angular templates (#15926 by @fisker)
2+
3+
If you are using an old version of Angular doesn't support trailing comma, make sure add
4+
5+
```yaml
6+
overrides:
7+
- files: "<patterns-of-my-angular-files>"
8+
options:
9+
trailingComma: none
10+
```
11+
12+
to your `.prettierrc` file.
13+
14+
<!-- prettier-ignore -->
15+
```jsx
16+
// Input
17+
<div
18+
[ngClass]="{
19+
'px-lg': ctx.spacing == 'lg',
20+
'px-md': ctx.spacing == 'md',
21+
'px-xs': ctx.spacing == 'xs',
22+
}"
23+
></div>
24+
25+
// Prettier stable
26+
<div
27+
[ngClass]="{
28+
'px-lg': ctx.spacing == 'lg',
29+
'px-md': ctx.spacing == 'md',
30+
'px-xs': ctx.spacing == 'xs'
31+
}"
32+
></div>
33+
34+
// Prettier main
35+
<div
36+
[ngClass]="{
37+
'px-lg': ctx.spacing == 'lg',
38+
'px-md': ctx.spacing == 'md',
39+
'px-xs': ctx.spacing == 'xs',
40+
}"
41+
></div>
42+
```

src/language-html/embed.js

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ function embed(path, options) {
114114
};
115115
if (options.parser === "angular") {
116116
textToDocOptions.parser = "__ng_interpolation";
117-
textToDocOptions.trailingComma = "none";
118117
} else if (options.parser === "vue") {
119118
textToDocOptions.parser = isVueSfcWithTypescriptScript(
120119
path,

src/language-html/embed/angular-attributes.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ function createAngularPrinter({ parser }) {
1818
formatAttributeValue(
1919
getUnescapedAttributeValue(path.node),
2020
textToDoc,
21-
{
22-
parser,
23-
// angular does not allow trailing comma
24-
trailingComma: "none",
25-
},
21+
{ parser },
2622
shouldHugJsExpression,
2723
);
2824
}

src/language-html/embed/angular-control-flow-block-parameters.js

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function printAngularControlFlowBlockParameters(
2424
{
2525
parser: "__ng_directive",
2626
__isInHtmlAttribute: false,
27-
trailingComma: "none",
2827
},
2928
shouldHugJsExpression,
3029
);

src/language-html/embed/angular-interpolation.js

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ async function printAngularInterpolation(text, textToDoc) {
1919
await formatAttributeValue(part, textToDoc, {
2020
parser: "__ng_interpolation",
2121
__isInHtmlInterpolation: true, // to avoid unexpected `}}`
22-
trailingComma: "none",
2322
}),
2423
]),
2524
line,

src/language-js/print/call-arguments.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ function printCallArguments(path, options, print) {
8282
const isDynamicImport =
8383
node.type === "ImportExpression" || node.callee.type === "Import";
8484
const maybeTrailingComma =
85-
!isDynamicImport && shouldPrintComma(options, "all") ? "," : "";
85+
// Angular does not allow trailing comma
86+
!options.parser.startsWith("__ng_") &&
87+
!isDynamicImport &&
88+
shouldPrintComma(options, "all")
89+
? ","
90+
: "";
8691

8792
function allArgsBrokenOut() {
8893
return group(

tests/format/angular/angular/__snapshots__/format.test.js.snap

+31-31
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ printWidth: 80
451451
[target]="{a: 1}"
452452
[target]="{a: 1}"
453453
[target]="{
454-
trailingComma: 'notAllowed'
454+
trailingComma: 'notAllowed',
455455
}"
456456
[target]="[
457-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
457+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
458458
]"
459459
[target]="true"
460460
[target]="undefined"
@@ -540,14 +540,14 @@ printWidth: 80
540540
listRow.NextScheduledSendStatus == 3
541541
"
542542
[target]="{
543-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
543+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
544544
}"
545545
[error]="'We couldn\\\\\\'t find anything with that name.'"
546546
*ngIf="form.controls.details?.controls.amount?.errors.min"
547547
[ngClass]="{
548548
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
549549
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
550-
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
550+
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
551551
}"
552552
[stickout]="+toolAssembly.stickoutMm"
553553
test="{{ 'test' | translate }}"
@@ -730,10 +730,10 @@ printWidth: 80
730730
[target]="{ a: 1 }"
731731
[target]="{ a: 1 }"
732732
[target]="{
733-
trailingComma: 'notAllowed'
733+
trailingComma: 'notAllowed',
734734
}"
735735
[target]="[
736-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
736+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
737737
]"
738738
[target]="true"
739739
[target]="undefined"
@@ -819,14 +819,14 @@ printWidth: 80
819819
listRow.NextScheduledSendStatus == 3
820820
"
821821
[target]="{
822-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
822+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
823823
}"
824824
[error]="'We couldn\\\\\\'t find anything with that name.'"
825825
*ngIf="form.controls.details?.controls.amount?.errors.min"
826826
[ngClass]="{
827827
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
828828
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
829-
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
829+
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
830830
}"
831831
[stickout]="+toolAssembly.stickoutMm"
832832
test="{{ 'test' | translate }}"
@@ -1053,20 +1053,20 @@ printWidth: 1
10531053
]
10541054
"
10551055
[target]="[
1056-
1
1056+
1,
10571057
]"
10581058
[target]="{
1059-
a: 1
1059+
a: 1,
10601060
}"
10611061
[target]="{
1062-
a: 1
1062+
a: 1,
10631063
}"
10641064
[target]="{
10651065
trailingComma:
1066-
'notAllowed'
1066+
'notAllowed',
10671067
}"
10681068
[target]="[
1069-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
1069+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
10701070
]"
10711071
[target]="
10721072
true
@@ -1210,7 +1210,7 @@ printWidth: 1
12101210
1,
12111211
2,
12121212
3,
1213-
666666666666666666666666666666666666
1213+
666666666666666666666666666666666666,
12141214
];
12151215
let i = index
12161216
"
@@ -1320,7 +1320,7 @@ printWidth: 1
13201320
3
13211321
"
13221322
[target]="{
1323-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
1323+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
13241324
}"
13251325
[error]="
13261326
'We couldn\\\\\\'t find anything with that name.'
@@ -1355,7 +1355,7 @@ printWidth: 1
13551355
| async
13561356
)
13571357
.level ===
1358-
dialogLevelEnum.DANGER
1358+
dialogLevelEnum.DANGER,
13591359
}"
13601360
[stickout]="
13611361
+toolAssembly.stickoutMm
@@ -1636,10 +1636,10 @@ trailingComma: "es5"
16361636
[target]="{ a: 1 }"
16371637
[target]="{ a: 1 }"
16381638
[target]="{
1639-
trailingComma: 'notAllowed'
1639+
trailingComma: 'notAllowed',
16401640
}"
16411641
[target]="[
1642-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
1642+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
16431643
]"
16441644
[target]="true"
16451645
[target]="undefined"
@@ -1725,14 +1725,14 @@ trailingComma: "es5"
17251725
listRow.NextScheduledSendStatus == 3
17261726
"
17271727
[target]="{
1728-
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
1728+
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
17291729
}"
17301730
[error]="'We couldn\\\\\\'t find anything with that name.'"
17311731
*ngIf="form.controls.details?.controls.amount?.errors.min"
17321732
[ngClass]="{
17331733
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
17341734
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
1735-
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
1735+
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
17361736
}"
17371737
[stickout]="+toolAssembly.stickoutMm"
17381738
test="{{ 'test' | translate }}"
@@ -3395,21 +3395,21 @@ printWidth: 1
33953395
<div>
33963396
{{
33973397
[
3398-
1
3398+
1,
33993399
]
34003400
}}
34013401
</div>
34023402
<div>
34033403
{{
34043404
{
3405-
a: 1
3405+
a: 1,
34063406
}
34073407
}}
34083408
</div>
34093409
<div>
34103410
{{
34113411
{
3412-
a: 1
3412+
a: 1,
34133413
}
34143414
}}
34153415
</div>
@@ -3599,8 +3599,8 @@ printWidth: 1
35993599
| translate
36003600
: {
36013601
count:
3602-
array.length
3603-
})
3602+
array.length,
3603+
}),
36043604
}
36053605
}}
36063606
</p>
@@ -3609,7 +3609,7 @@ printWidth: 1
36093609
{
36103610
a:
36113611
1 +
3612-
{}
3612+
{},
36133613
}
36143614
}}
36153615
</p>
@@ -3618,14 +3618,14 @@ printWidth: 1
36183618
{
36193619
a:
36203620
a ===
3621-
{}
3621+
{},
36223622
}
36233623
}}
36243624
</p>
36253625
<p>
36263626
{{
36273627
{
3628-
a: !{}
3628+
a: !{},
36293629
}
36303630
}}
36313631
</p>
@@ -3634,7 +3634,7 @@ printWidth: 1
36343634
{
36353635
a: a
36363636
? b
3637-
: {}
3637+
: {},
36383638
}
36393639
}}
36403640
</p>
@@ -8391,7 +8391,7 @@ can be found in the LICENSE file at http://angular.io/license
83918391
<div
83928392
[ngClass]="{
83938393
special:
8394-
isSpecial
8394+
isSpecial,
83958395
}"
83968396
></div>
83978397
</div>
@@ -9371,7 +9371,7 @@ bindon-ngModel
93719371
[ngClass]="{
93729372
bad: false,
93739373
curly: true,
9374-
special: true
9374+
special: true,
93759375
}"
93769376
>
93779377
Curly

0 commit comments

Comments
 (0)