Support Angular inline styles as single template literal (#15968)
This commit is contained in:
1 parent
4b1965ae45
commit
2fe7f4193a
5 files changed
+231
-9
No files matched your search
@@ -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 {}
|
||||||
|
|
||||||
|
```
|
||||||
@@ -30,15 +30,25 @@ const angularComponentObjectExpressionPredicates = [
|
|||||||
* })
|
* })
|
||||||
*/
|
*/
|
||||||
function isAngularComponentStyles(path) {
|
function isAngularComponentStyles(path) {
|
||||||
return path.match(
|
const isTemplateLiteral = (node) => node.type === "TemplateLiteral";
|
||||||
(node) => node.type === "TemplateLiteral",
|
const isObjectPropertyNamedStyles = (node, key) =>
|
||||||
(node, name) => isArrayOrTupleExpression(node) && name === "elements",
|
isObjectProperty(node) &&
|
||||||
(node, name) =>
|
!node.computed &&
|
||||||
isObjectProperty(node) &&
|
node.key.type === "Identifier" &&
|
||||||
node.key.type === "Identifier" &&
|
node.key.name === "styles" &&
|
||||||
node.key.name === "styles" &&
|
key === "value";
|
||||||
name === "value",
|
return (
|
||||||
...angularComponentObjectExpressionPredicates,
|
path.match(
|
||||||
|
isTemplateLiteral,
|
||||||
|
(node, name) => isArrayOrTupleExpression(node) && name === "elements",
|
||||||
|
isObjectPropertyNamedStyles,
|
||||||
|
...angularComponentObjectExpressionPredicates,
|
||||||
|
) ||
|
||||||
|
path.match(
|
||||||
|
isTemplateLiteral,
|
||||||
|
isObjectPropertyNamedStyles,
|
||||||
|
...angularComponentObjectExpressionPredicates,
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
function isAngularComponentTemplate(path) {
|
function isAngularComponentTemplate(path) {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
|
const styles = "foobar";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-root',
|
||||||
|
template: `
|
||||||
|
<h1>My App</h1>
|
||||||
|
<app-todo-list></app-todo-list>
|
||||||
|
`,
|
||||||
|
[styles]: `h1 { color: blue }`
|
||||||
|
})
|
||||||
|
export class AppComponent {}
|
||||||
@@ -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 {}
|
||||||
@@ -1,5 +1,161 @@
|
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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[`15934-computed.component.ts - {"trailingComma":"es5"} format 1`] = `
|
||||||
|
====================================options=====================================
|
||||||
|
parsers: ["typescript"]
|
||||||
|
printWidth: 80
|
||||||
|
trailingComma: "es5"
|
||||||
|
| printWidth
|
||||||
|
=====================================input======================================
|
||||||
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
|
const styles = "foobar";
|
||||||
|
|
||||||
|
@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";
|
||||||
|
|
||||||
|
const styles = "foobar";
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: "app-root",
|
||||||
|
template: \`
|
||||||
|
<h1>My App</h1>
|
||||||
|
<app-todo-list></app-todo-list>
|
||||||
|
\`,
|
||||||
|
[styles]: \`h1 { color: blue }\`,
|
||||||
|
})
|
||||||
|
export class AppComponent {}
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`15934-computed.component.ts - {"trailingComma":"none"} format 1`] = `
|
||||||
|
====================================options=====================================
|
||||||
|
parsers: ["typescript"]
|
||||||
|
printWidth: 80
|
||||||
|
trailingComma: "none"
|
||||||
|
| printWidth
|
||||||
|
=====================================input======================================
|
||||||
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
|
const styles = "foobar";
|
||||||
|
|
||||||
|
@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";
|
||||||
|
|
||||||
|
const styles = "foobar";
|
||||||
|
|
||||||
|
@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`] = `
|
exports[`test.component.ts - {"trailingComma":"es5"} format 1`] = `
|
||||||
====================================options=====================================
|
====================================options=====================================
|
||||||
parsers: ["typescript"]
|
parsers: ["typescript"]
|
||||||
|
|||||||
Reference in new issue
Block a user