[go: nahoru, domu]

Skip to content

Commit

Permalink
Respect trailingComma in angular templates (#15926)
Browse files Browse the repository at this point in the history
Co-authored-by: Sosuke Suzuki <aosukeke@gmail.com>
  • Loading branch information
fisker and sosukesuzuki committed May 28, 2024
1 parent c2e20fb commit 8a88cdc
Show file tree
Hide file tree
Showing 10 changed files with 761 additions and 40 deletions.
42 changes: 42 additions & 0 deletions changelog_unreleased/angular/15926.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#### Respect `trailingComma` in Angular templates (#15926 by @fisker)

If you are using an old version of Angular doesn't support trailing comma, make sure add

```yaml
overrides:
- files: "<patterns-of-my-angular-files>"
options:
trailingComma: none
```

to your `.prettierrc` file.

<!-- prettier-ignore -->
```jsx
// Input
<div
[ngClass]="{
'px-lg': ctx.spacing == 'lg',
'px-md': ctx.spacing == 'md',
'px-xs': ctx.spacing == 'xs',
}"
></div>

// Prettier stable
<div
[ngClass]="{
'px-lg': ctx.spacing == 'lg',
'px-md': ctx.spacing == 'md',
'px-xs': ctx.spacing == 'xs'
}"
></div>

// Prettier main
<div
[ngClass]="{
'px-lg': ctx.spacing == 'lg',
'px-md': ctx.spacing == 'md',
'px-xs': ctx.spacing == 'xs',
}"
></div>
```
1 change: 0 additions & 1 deletion src/language-html/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function embed(path, options) {
};
if (options.parser === "angular") {
textToDocOptions.parser = "__ng_interpolation";
textToDocOptions.trailingComma = "none";
} else if (options.parser === "vue") {
textToDocOptions.parser = isVueSfcWithTypescriptScript(
path,
Expand Down
6 changes: 1 addition & 5 deletions src/language-html/embed/angular-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ function createAngularPrinter({ parser }) {
formatAttributeValue(
getUnescapedAttributeValue(path.node),
textToDoc,
{
parser,
// angular does not allow trailing comma
trailingComma: "none",
},
{ parser },
shouldHugJsExpression,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ function printAngularControlFlowBlockParameters(
{
parser: "__ng_directive",
__isInHtmlAttribute: false,
trailingComma: "none",
},
shouldHugJsExpression,
);
Expand Down
1 change: 0 additions & 1 deletion src/language-html/embed/angular-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async function printAngularInterpolation(text, textToDoc) {
await formatAttributeValue(part, textToDoc, {
parser: "__ng_interpolation",
__isInHtmlInterpolation: true, // to avoid unexpected `}}`
trailingComma: "none",
}),
]),
line,
Expand Down
7 changes: 6 additions & 1 deletion src/language-js/print/call-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ function printCallArguments(path, options, print) {
const isDynamicImport =
node.type === "ImportExpression" || node.callee.type === "Import";
const maybeTrailingComma =
!isDynamicImport && shouldPrintComma(options, "all") ? "," : "";
// Angular does not allow trailing comma
!options.parser.startsWith("__ng_") &&
!isDynamicImport &&
shouldPrintComma(options, "all")
? ","
: "";

function allArgsBrokenOut() {
return group(
Expand Down
62 changes: 31 additions & 31 deletions tests/format/angular/angular/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ printWidth: 80
[target]="{a: 1}"
[target]="{a: 1}"
[target]="{
trailingComma: 'notAllowed'
trailingComma: 'notAllowed',
}"
[target]="[
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
]"
[target]="true"
[target]="undefined"
Expand Down Expand Up @@ -540,14 +540,14 @@ printWidth: 80
listRow.NextScheduledSendStatus == 3
"
[target]="{
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
}"
[error]="'We couldn\\\\\\'t find anything with that name.'"
*ngIf="form.controls.details?.controls.amount?.errors.min"
[ngClass]="{
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
}"
[stickout]="+toolAssembly.stickoutMm"
test="{{ 'test' | translate }}"
Expand Down Expand Up @@ -730,10 +730,10 @@ printWidth: 80
[target]="{ a: 1 }"
[target]="{ a: 1 }"
[target]="{
trailingComma: 'notAllowed'
trailingComma: 'notAllowed',
}"
[target]="[
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
]"
[target]="true"
[target]="undefined"
Expand Down Expand Up @@ -819,14 +819,14 @@ printWidth: 80
listRow.NextScheduledSendStatus == 3
"
[target]="{
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
}"
[error]="'We couldn\\\\\\'t find anything with that name.'"
*ngIf="form.controls.details?.controls.amount?.errors.min"
[ngClass]="{
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
}"
[stickout]="+toolAssembly.stickoutMm"
test="{{ 'test' | translate }}"
Expand Down Expand Up @@ -1053,20 +1053,20 @@ printWidth: 1
]
"
[target]="[
1
1,
]"
[target]="{
a: 1
a: 1,
}"
[target]="{
a: 1
a: 1,
}"
[target]="{
trailingComma:
'notAllowed'
'notAllowed',
}"
[target]="[
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
]"
[target]="
true
Expand Down Expand Up @@ -1210,7 +1210,7 @@ printWidth: 1
1,
2,
3,
666666666666666666666666666666666666
666666666666666666666666666666666666,
];
let i = index
"
Expand Down Expand Up @@ -1320,7 +1320,7 @@ printWidth: 1
3
"
[target]="{
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
}"
[error]="
'We couldn\\\\\\'t find anything with that name.'
Expand Down Expand Up @@ -1355,7 +1355,7 @@ printWidth: 1
| async
)
.level ===
dialogLevelEnum.DANGER
dialogLevelEnum.DANGER,
}"
[stickout]="
+toolAssembly.stickoutMm
Expand Down Expand Up @@ -1636,10 +1636,10 @@ trailingComma: "es5"
[target]="{ a: 1 }"
[target]="{ a: 1 }"
[target]="{
trailingComma: 'notAllowed'
trailingComma: 'notAllowed',
}"
[target]="[
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong,
]"
[target]="true"
[target]="undefined"
Expand Down Expand Up @@ -1725,14 +1725,14 @@ trailingComma: "es5"
listRow.NextScheduledSendStatus == 3
"
[target]="{
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true
longlonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglonglong: true,
}"
[error]="'We couldn\\\\\\'t find anything with that name.'"
*ngIf="form.controls.details?.controls.amount?.errors.min"
[ngClass]="{
'btn-success': (dialog$ | async).level === dialogLevelEnum.SUCCESS,
'btn-warning': (dialog$ | async).level === dialogLevelEnum.WARNING,
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER
'btn-svg': (dialog$ | async).level === dialogLevelEnum.DANGER,
}"
[stickout]="+toolAssembly.stickoutMm"
test="{{ 'test' | translate }}"
Expand Down Expand Up @@ -3395,21 +3395,21 @@ printWidth: 1
<div>
{{
[
1
1,
]
}}
</div>
<div>
{{
{
a: 1
a: 1,
}
}}
</div>
<div>
{{
{
a: 1
a: 1,
}
}}
</div>
Expand Down Expand Up @@ -3599,8 +3599,8 @@ printWidth: 1
| translate
: {
count:
array.length
})
array.length,
}),
}
}}
</p>
Expand All @@ -3609,7 +3609,7 @@ printWidth: 1
{
a:
1 +
{}
{},
}
}}
</p>
Expand All @@ -3618,14 +3618,14 @@ printWidth: 1
{
a:
a ===
{}
{},
}
}}
</p>
<p>
{{
{
a: !{}
a: !{},
}
}}
</p>
Expand All @@ -3634,7 +3634,7 @@ printWidth: 1
{
a: a
? b
: {}
: {},
}
}}
</p>
Expand Down Expand Up @@ -8391,7 +8391,7 @@ can be found in the LICENSE file at http://angular.io/license
<div
[ngClass]="{
special:
isSpecial
isSpecial,
}"
></div>
</div>
Expand Down Expand Up @@ -9371,7 +9371,7 @@ bindon-ngModel
[ngClass]="{
bad: false,
curly: true,
special: true
special: true,
}"
>
Curly
Expand Down
Loading

0 comments on commit 8a88cdc

Please sign in to comment.