Skip to content

Commit 572bebe

Browse files
authored
Fix comments on class methods with decorators (#16891)
1 parent 359c4f0 commit 572bebe

File tree

4 files changed

+253
-1
lines changed

4 files changed

+253
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#### Fix comments print on class methods with decorators (#16891 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
class A {
7+
@decorator
8+
/**
9+
* The method description
10+
*
11+
*/
12+
async method(foo: Foo, bar: Bar) {
13+
console.log(foo);
14+
}
15+
}
16+
17+
// Prettier stable
18+
class A {
19+
@decorator
20+
async /**
21+
* The method description
22+
*
23+
*/
24+
method(foo: Foo, bar: Bar) {
25+
console.log(foo);
26+
}
27+
}
28+
29+
// Prettier main
30+
class A {
31+
@decorator
32+
/**
33+
* The method description
34+
*
35+
*/
36+
async method(foo: Foo, bar: Bar) {
37+
console.log(foo);
38+
}
39+
}
40+
```

src/language-js/comments/handle-comments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ function handleMethodNameComments({
543543
if (
544544
precedingNode?.type === "Decorator" &&
545545
propertyLikeNodeTypes.has(enclosingNode?.type) &&
546-
isLineComment(comment)
546+
(isLineComment(comment) || comment.placement === "ownLine")
547547
) {
548548
addTrailingComment(precedingNode, comment);
549549
return true;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class A {
2+
@decorator
3+
/**
4+
* The method description
5+
*
6+
*/
7+
async method() {
8+
}
9+
10+
@decorator/**
11+
* The method description
12+
*
13+
*/
14+
async method() {
15+
}
16+
17+
@decorator/**
18+
* The method description
19+
*
20+
*/ async method() {
21+
}
22+
23+
@decorator
24+
async /* comment */ method() {
25+
}
26+
27+
@decorator /* comment */ async method() {
28+
}
29+
30+
@decorator
31+
// line comment
32+
async method() {
33+
}
34+
35+
@decorator // line comment
36+
async method() {
37+
}
38+
39+
40+
@decorator
41+
/* comment */
42+
public async method() {
43+
}
44+
45+
@decorator
46+
/* comment */
47+
static async method() {
48+
}
49+
50+
@decorator
51+
/* comment */
52+
protected async method() {
53+
}
54+
55+
@decorator
56+
/* comment */
57+
protected async method() {
58+
}
59+
60+
@decorator
61+
/* comment */
62+
* method() {}
63+
64+
@decorator
65+
* /* comment */ method() {}
66+
67+
/* comment */
68+
abstract method():void;
69+
70+
}
71+

tests/format/typescript/comments/__snapshots__/format.test.js.snap

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,147 @@ class Foo {
248248
================================================================================
249249
`;
250250

251+
exports[`16889.ts format 1`] = `
252+
====================================options=====================================
253+
parsers: ["typescript"]
254+
printWidth: 80
255+
| printWidth
256+
=====================================input======================================
257+
class A {
258+
@decorator
259+
/**
260+
* The method description
261+
*
262+
*/
263+
async method() {
264+
}
265+
266+
@decorator/**
267+
* The method description
268+
*
269+
*/
270+
async method() {
271+
}
272+
273+
@decorator/**
274+
* The method description
275+
*
276+
*/ async method() {
277+
}
278+
279+
@decorator
280+
async /* comment */ method() {
281+
}
282+
283+
@decorator /* comment */ async method() {
284+
}
285+
286+
@decorator
287+
// line comment
288+
async method() {
289+
}
290+
291+
@decorator // line comment
292+
async method() {
293+
}
294+
295+
296+
@decorator
297+
/* comment */
298+
public async method() {
299+
}
300+
301+
@decorator
302+
/* comment */
303+
static async method() {
304+
}
305+
306+
@decorator
307+
/* comment */
308+
protected async method() {
309+
}
310+
311+
@decorator
312+
/* comment */
313+
protected async method() {
314+
}
315+
316+
@decorator
317+
/* comment */
318+
* method() {}
319+
320+
@decorator
321+
* /* comment */ method() {}
322+
323+
/* comment */
324+
abstract method():void;
325+
326+
}
327+
328+
329+
=====================================output=====================================
330+
class A {
331+
@decorator
332+
/**
333+
* The method description
334+
*
335+
*/
336+
async method() {}
337+
338+
@decorator /**
339+
* The method description
340+
*
341+
*/
342+
async method() {}
343+
344+
@decorator /**
345+
* The method description
346+
*
347+
*/
348+
async method() {}
349+
350+
@decorator
351+
async /* comment */ method() {}
352+
353+
@decorator /* comment */ async method() {}
354+
355+
@decorator
356+
// line comment
357+
async method() {}
358+
359+
@decorator // line comment
360+
async method() {}
361+
362+
@decorator
363+
/* comment */
364+
public async method() {}
365+
366+
@decorator
367+
/* comment */
368+
static async method() {}
369+
370+
@decorator
371+
/* comment */
372+
protected async method() {}
373+
374+
@decorator
375+
/* comment */
376+
protected async method() {}
377+
378+
@decorator
379+
/* comment */
380+
*method() {}
381+
382+
@decorator
383+
*/* comment */ method() {}
384+
385+
/* comment */
386+
abstract method(): void;
387+
}
388+
389+
================================================================================
390+
`;
391+
251392
exports[`abstract_class.ts format 1`] = `
252393
====================================options=====================================
253394
parsers: ["typescript"]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy