Preserve a comment on between decorator and modified parameter property (#16574)

This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-08-15 16:03:53 +09:00
1 parent 1dfe28f857
commit bd3ad84c29
4 files changed
+81

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
#### Preserve a comment on between decorator and modified parameter property (#16574 by @sosukesuzuki)
The current version of Prettier unexpectedly moves a line comment between a parameter property modified by `readonly`, `private`, `public`, etc and a decorator. This output results in invalid TypeScript code.
This change ensures that the original format is preserved.
<!-- prettier-ignore -->
```ts
// Input
class Foo {
constructor(
@decorator
// comment
readonly foo,
) {}
}
// Output (stable)
class Foo {
constructor(
@decorator
readonly // comment
foo,
) {}
}
// Output (main)
class Foo {
constructor(
@decorator
// comment
readonly foo,
) {}
}
```
@@ -495,6 +495,7 @@ const propertyLikeNodeTypes = new Set([
"ClassAccessorProperty",
"AccessorProperty",
"TSAbstractAccessorProperty",
"TSParameterProperty",
]);
function handleMethodNameComments({
comment,
+11
View File
@@ -0,0 +1,11 @@
class Foo {
constructor(
@decorator1
// comment1
readonly baz1: string,
@decorator2
// comment2
private baz2: string,
) {}
}
@@ -122,6 +122,40 @@ const [
================================================================================
`;
exports[`16065.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
class Foo {
constructor(
@decorator1
// comment1
readonly baz1: string,
@decorator2
// comment2
private baz2: string,
) {}
}
=====================================output=====================================
class Foo {
constructor(
@decorator1
// comment1
readonly baz1: string,
@decorator2
// comment2
private baz2: string,
) {}
}
================================================================================
`;
exports[`abstract_class.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]