Preserve a comment between modifier and the decorated property name (#16578)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-08-16 13:12:24 +09:00
1 parent 1e15157090
commit f7cceb9be1
4 files changed
+126 -1

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
#### Preserve a comment between modifier and the decorated property name (#16578 by @sosukesuzuki)
There was a bug where block comments between the modifier and the name of a decorated property were being treated as trailing comments of the decorator. This behavior was not only unexpected but also lacked idempotency.
With this change, the position of the block comment between the modifier and the property name is preserved.
<!-- prettier-ignore -->
```tsx
// Input
class Foo {
@decorator
readonly /* comment */ propertyName;
}
// Prettier stable
class Foo {
@decorator /* comment */
readonly propertyName;
}
// Prettier stable (second output)
class Foo {
@decorator /* comment */ readonly propertyName;
}
// Prettier main
class Foo {
@decorator
readonly /* comment */ propertyName;
}
```
+2 -1
View File
@@ -528,7 +528,8 @@ function handleMethodNameComments({
// on the decorator node instead of the method node
if (
precedingNode?.type === "Decorator" &&
propertyLikeNodeTypes.has(enclosingNode?.type)
propertyLikeNodeTypes.has(enclosingNode?.type) &&
isLineComment(comment)
) {
addTrailingComment(precedingNode, comment);
return true;
@@ -0,0 +1,27 @@
class Foo {
// PropertyDefinition
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
// AccessorProperty
@decorator
accessor /* comment */ accessorProperty = 3;
constructor(
// TSParameterProperty
@decorator
readonly /* comment */ parameterProperty,
) {}
}
@@ -156,6 +156,72 @@ class Foo {
================================================================================
`;
exports[`16065-2.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class Foo {
// PropertyDefinition
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
// AccessorProperty
@decorator
accessor /* comment */ accessorProperty = 3;
constructor(
// TSParameterProperty
@decorator
readonly /* comment */ parameterProperty,
) {}
}
=====================================output=====================================
class Foo {
// PropertyDefinition
@decorator
readonly /* comment */ propertyDefinition;
// TSAbstractPropertyDefinition
@decorator
abstract /* comment */ abstractPropertyDefinition;
// TSAbstractMethodDefinition
@decorator
abstract /* comment */ abstractMethodDefinition;
// MethodDefinition
@decorator
private /* comment */ methodDefinition() {}
// AccessorProperty
@decorator
accessor /* comment */ accessorProperty = 3;
constructor(
// TSParameterProperty
@decorator
readonly /* comment */ parameterProperty,
) {}
}
================================================================================
`;
exports[`abstract_class.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]