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

Support Angular inline styles as single template literal #15968

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 32 additions & 0 deletions changelog_unreleased/angular/15968.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#### Support Angular inline styles as single template literal (#15968 by @sosukesuzuki)

[Angular v17](https://blog.angular.io/introducing-angular-v17-4d7033312e4b) supports single string inline styles.

<!-- prettier-ignore -->
```ts
// Input
@Component({
template: `<div>...</div>`,
styles: `h1 { color: blue; }`,
})
export class AppComponent {}

// Prettier stable
@Component({
template: `<div>...</div>`,
styles: `h1 { color: blue; }`,
})
export class AppComponent {}

// Prettier main
@Component({
template: `<div>...</div>`,
styles: `
h1 {
color: blue;
}
`,
})
export class AppComponent {}

```
27 changes: 18 additions & 9 deletions src/language-js/embed/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ const angularComponentObjectExpressionPredicates = [
* })
*/
function isAngularComponentStyles(path) {
return path.match(
(node) => node.type === "TemplateLiteral",
(node, name) => isArrayOrTupleExpression(node) && name === "elements",
(node, name) =>
isObjectProperty(node) &&
node.key.type === "Identifier" &&
node.key.name === "styles" &&
name === "value",
...angularComponentObjectExpressionPredicates,
const isTemplateLiteral = (node) => node.type === "TemplateLiteral";
const isStyleProperty = (node, name) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about isObjectPropertyNamedStyles?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second parameter should be named key

isObjectProperty(node) &&
node.key.type === "Identifier" &&
node.key.name === "styles" &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!node.computed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed this and added tests. Also I've create the new similar issue for template as a good first ( #15969 )

name === "value";
return (
path.match(
isTemplateLiteral,
(node, name) => isArrayOrTupleExpression(node) && name === "elements",
isStyleProperty,
...angularComponentObjectExpressionPredicates,
) ||
path.match(
isTemplateLiteral,
isStyleProperty,
...angularComponentObjectExpressionPredicates,
)
);
}
function isAngularComponentTemplate(path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: `
<h1>My App</h1>
<app-todo-list></app-todo-list>
`,
styles: `h1 { color: blue }`
})
export class AppComponent {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`15934.component.ts - {"trailingComma":"es5"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
trailingComma: "es5"
| printWidth
=====================================input======================================
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: \`
<h1>My App</h1>
<app-todo-list></app-todo-list>
\`,
styles: \`h1 { color: blue }\`
})
export class AppComponent {}

=====================================output=====================================
import { Component } from "@angular/core";

@Component({
selector: "app-root",
template: \`
<h1>My App</h1>
<app-todo-list></app-todo-list>
\`,
styles: \`
h1 {
color: blue;
}
\`,
})
export class AppComponent {}

================================================================================
`;

exports[`15934.component.ts - {"trailingComma":"none"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
trailingComma: "none"
| printWidth
=====================================input======================================
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: \`
<h1>My App</h1>
<app-todo-list></app-todo-list>
\`,
styles: \`h1 { color: blue }\`
})
export class AppComponent {}

=====================================output=====================================
import { Component } from "@angular/core";

@Component({
selector: "app-root",
template: \`
<h1>My App</h1>
<app-todo-list></app-todo-list>
\`,
styles: \`
h1 {
color: blue;
}
\`
})
export class AppComponent {}

================================================================================
`;

exports[`test.component.ts - {"trailingComma":"es5"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down