Fix comments on class methods with decorators (#16891)

This commit is contained in:
fisker Cheung authored and GitHub committed 2024-12-02 10:03:56 +08:00
1 parent 359c4f02d0
commit 572bebe2d4
4 files changed
+253 -1

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
#### Fix comments print on class methods with decorators (#16891 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
class A {
@decorator
/**
* The method description
*
*/
async method(foo: Foo, bar: Bar) {
console.log(foo);
}
}
// Prettier stable
class A {
@decorator
async /**
* The method description
*
*/
method(foo: Foo, bar: Bar) {
console.log(foo);
}
}
// Prettier main
class A {
@decorator
/**
* The method description
*
*/
async method(foo: Foo, bar: Bar) {
console.log(foo);
}
}
```
+1 -1
View File
@@ -543,7 +543,7 @@ function handleMethodNameComments({
if ( if (
precedingNode?.type === "Decorator" && precedingNode?.type === "Decorator" &&
propertyLikeNodeTypes.has(enclosingNode?.type) && propertyLikeNodeTypes.has(enclosingNode?.type) &&
isLineComment(comment) (isLineComment(comment) || comment.placement === "ownLine")
) { ) {
addTrailingComment(precedingNode, comment); addTrailingComment(precedingNode, comment);
return true; return true;
+71
View File
@@ -0,0 +1,71 @@
class A {
@decorator
/**
* The method description
*
*/
async method() {
}
@decorator/**
* The method description
*
*/
async method() {
}
@decorator/**
* The method description
*
*/ async method() {
}
@decorator
async /* comment */ method() {
}
@decorator /* comment */ async method() {
}
@decorator
// line comment
async method() {
}
@decorator // line comment
async method() {
}
@decorator
/* comment */
public async method() {
}
@decorator
/* comment */
static async method() {
}
@decorator
/* comment */
protected async method() {
}
@decorator
/* comment */
protected async method() {
}
@decorator
/* comment */
* method() {}
@decorator
* /* comment */ method() {}
/* comment */
abstract method():void;
}
@@ -248,6 +248,147 @@ class Foo {
================================================================================ ================================================================================
`; `;
exports[`16889.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class A {
@decorator
/**
* The method description
*
*/
async method() {
}
@decorator/**
* The method description
*
*/
async method() {
}
@decorator/**
* The method description
*
*/ async method() {
}
@decorator
async /* comment */ method() {
}
@decorator /* comment */ async method() {
}
@decorator
// line comment
async method() {
}
@decorator // line comment
async method() {
}
@decorator
/* comment */
public async method() {
}
@decorator
/* comment */
static async method() {
}
@decorator
/* comment */
protected async method() {
}
@decorator
/* comment */
protected async method() {
}
@decorator
/* comment */
* method() {}
@decorator
* /* comment */ method() {}
/* comment */
abstract method():void;
}
=====================================output=====================================
class A {
@decorator
/**
* The method description
*
*/
async method() {}
@decorator /**
* The method description
*
*/
async method() {}
@decorator /**
* The method description
*
*/
async method() {}
@decorator
async /* comment */ method() {}
@decorator /* comment */ async method() {}
@decorator
// line comment
async method() {}
@decorator // line comment
async method() {}
@decorator
/* comment */
public async method() {}
@decorator
/* comment */
static async method() {}
@decorator
/* comment */
protected async method() {}
@decorator
/* comment */
protected async method() {}
@decorator
/* comment */
*method() {}
@decorator
*/* comment */ method() {}
/* comment */
abstract method(): void;
}
================================================================================
`;
exports[`abstract_class.ts format 1`] = ` exports[`abstract_class.ts format 1`] = `
====================================options===================================== ====================================options=====================================
parsers: ["typescript"] parsers: ["typescript"]