Allow decorators to be used with declare on class fields (#19492)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Liamandfisker authored and fisker committed 2026-06-29 19:31:12 +08:00
1 parent bd9e11ab41
commit a74a7b05de
7 files changed
+113

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
#### Allow decorators to be used with declare on class fields (#19492 by @evoactivity)
Extensively used within the Ember ecosystem, decorators with `declare` on class fields will ignore the babel parser error and allow Prettier to format the code without breaking it.
<!-- prettier-ignore -->
```ts
// Input
export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
@service declare server: ServerService;
}
// Prettier stable
// SyntaxError: Decorators can't be used with a declare field. (2:3)
// 1 | export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
//> 2 | @service declare server: ServerService;
// | ^
// 3 | }
// Prettier main
export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
@service declare server: ServerService;
}
```
+7
View File
@@ -222,6 +222,13 @@ const allowedReasonCodesArray = [
```
*/
"DeclarationMissingInitializer",
/*
Allow decorators to be used with declare fields
https://github.com/prettier/prettier/issues/19491
https://github.com/babel/babel/pull/17949
*/
"DecoratorAbstractMethod",
];
const allowedReasonCodes = new Set(allowedReasonCodesArray);
@@ -3,6 +3,14 @@ class Foo {
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
@@ -260,6 +260,14 @@ class Foo {
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
@@ -281,6 +289,14 @@ class Foo {
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
@@ -292,6 +292,12 @@ class Something {
readonly property: Array<string>
}
class Something2 {
@foo()
// comment
abstract property: Array<string>
}
=====================================output=====================================
class Foo1 {
@foo
@@ -323,6 +329,43 @@ class Something {
readonly property: Array<string>;
}
class Something2 {
@foo()
// comment
abstract property: Array<string>;
}
================================================================================
`;
exports[`ember.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
/*
https://github.com/prettier/prettier/issues/19491
decorators with declare is used extensively in Ember ecosystem,
and we want to make sure that Prettier doesn't break them.
*/
export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
@service declare server: ServerService;
}
=====================================output=====================================
/*
https://github.com/prettier/prettier/issues/19491
decorators with declare is used extensively in Ember ecosystem,
and we want to make sure that Prettier doesn't break them.
*/
export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
@service declare server: ServerService;
}
================================================================================
`;
@@ -28,3 +28,9 @@ class Something {
// comment
readonly property: Array<string>
}
class Something2 {
@foo()
// comment
abstract property: Array<string>
}
@@ -0,0 +1,10 @@
/*
https://github.com/prettier/prettier/issues/19491
decorators with declare is used extensively in Ember ecosystem,
and we want to make sure that Prettier doesn't break them.
*/
export default class ProjectStatusComponent extends Component<ProjectStatusSig> {
@service declare server: ServerService;
}