Skip to content

Commit

Permalink
fix: do not format computed template which could be unexpected (#15972)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jan 23, 2024
1 parent 91a2e25 commit 678de02
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
30 changes: 30 additions & 0 deletions changelog_unreleased/angular/15969.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#### Unexpected embedded formatting for Angular template (#15969 by @JounQin)

Computed template should not be considered as Angular component template

<!-- prettier-ignore -->
```ts
// Input
const template = "foobar";

@Component({
[template]: `<h1>{{ hello }}</h1>`,
})
export class AppComponent {}

// Prettier stable
const template = "foobar";

@Component({
[template]: `<h1>{{ hello }}</h1>`,
})
export class AppComponent {}

// Prettier main
const template = "foobar";

@Component({
[template]: `<h1>{{ hello }}</h1>`,
})
export class AppComponent {}
```
1 change: 1 addition & 0 deletions src/language-js/embed/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function isAngularComponentTemplate(path) {
(node) => node.type === "TemplateLiteral",
(node, name) =>
isObjectProperty(node) &&
!node.computed &&
node.key.type === "Identifier" &&
node.key.name === "template" &&
name === "value",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

const template = "foobar";

@Component({
[template]: `<h1>{{ hello }}</h1>`,
})
export class AppComponent {}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,64 @@ export class AppComponent {}
================================================================================
`;
exports[`15969-computed.component.ts - {"trailingComma":"es5"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
trailingComma: "es5"
| printWidth
=====================================input======================================
import { Component } from '@angular/core';
const template = "foobar";
@Component({
[template]: \`<h1>{{ hello }}</h1>\`,
})
export class AppComponent {}
=====================================output=====================================
import { Component } from "@angular/core";
const template = "foobar";
@Component({
[template]: \`<h1>{{ hello }}</h1>\`,
})
export class AppComponent {}
================================================================================
`;
exports[`15969-computed.component.ts - {"trailingComma":"none"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
trailingComma: "none"
| printWidth
=====================================input======================================
import { Component } from '@angular/core';
const template = "foobar";
@Component({
[template]: \`<h1>{{ hello }}</h1>\`,
})
export class AppComponent {}
=====================================output=====================================
import { Component } from "@angular/core";
const template = "foobar";
@Component({
[template]: \`<h1>{{ hello }}</h1>\`
})
export class AppComponent {}
================================================================================
`;
exports[`test.component.ts - {"trailingComma":"es5"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down

0 comments on commit 678de02

Please sign in to comment.