Keep an own-line comment before a satisfies or as type on its own line (#19939)

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Miodrag Obradovicandfisker authored and GitHub committed 2026-08-31 06:48:06 +08:00
1 parent 5a0fdd9740
commit 5fd5b6908b
9 files changed
+163 -62

No files matched your search

@@ -0,0 +1,33 @@
#### Fix misaligned jsdoc comment before a `satisfies` or `as` type (#19939 by @Kjubikstronk)
<!-- prettier-ignore -->
```tsx
// Input
const foo = {
bar: 1,
} as
/**
* comment line 1
* comment line 2
*/
Foo;
// Prettier stable
const foo = {
bar: 1,
} as /**
* comment line 1
* comment line 2
*/
Foo;
// Prettier main
const foo = {
bar: 1,
} as
/**
* comment line 1
* comment line 2
*/
Foo;
```
+29
View File
@@ -0,0 +1,29 @@
#### Keep an own-line comment before a `satisfies` or `as` type on its own line (#19939 by @Kjubikstronk)
<!-- prettier-ignore -->
```tsx
// Input
const foo = {
bar: 1,
} as
// Comment
Record<string, number>;
// Prettier stable (first format)
const foo = {
bar: 1,
} as // Comment
Record<string, number>;
// Prettier stable (second format)
const foo = {
bar: 1,
} as Record<string, number>; // Comment
// Prettier main
const foo = {
bar: 1,
} as
// Comment
Record<string, number>;
```
@@ -1,8 +1,10 @@
import { group, indent, softline } from "../../document/index.js";
import { group, hardline, indent, softline } from "../../document/index.js";
import { hasLeadingOwnLineComment } from "../utilities/has-leading-own-line-comment.js";
import {
isCallOrNewExpression,
isMemberExpression,
isSatisfiesExpression,
isUnionType,
} from "../utilities/node-types.js";
function printBinaryCastExpression(path, options, print) {
@@ -16,10 +18,18 @@ function printBinaryCastExpression(path, options, print) {
print("expression"),
" ",
isSatisfiesExpression(node) ? "satisfies" : "as",
" ",
typeAnnotationDoc,
];
if (
// Union type already indented
!isUnionType(node.typeAnnotation) &&
hasLeadingOwnLineComment(options.originalText, node.typeAnnotation)
) {
parts.push(indent([hardline, typeAnnotationDoc]));
} else {
parts.push(" ", typeAnnotationDoc);
}
if (
(key === "callee" && isCallOrNewExpression(parent)) ||
(key === "object" && isMemberExpression(parent))
@@ -20,7 +20,6 @@ const unstableTests = new Map(
"typescript/prettier-ignore/mapped-types.ts",
"typescript/prettier-ignore/issue-14238.ts",
"js/for-of/comments.js",
"typescript/satisfies-operators/comments-unstable.ts",
"jsx/comments/in-attributes.js",
"typescript/import-type/long-module-name/long-module-name4.ts",
[
@@ -56,3 +56,19 @@ const;
Foo;
1 satisfies // comment
Foo;
1 as
/**
* jsdoc
*/
const;
1 as
/**
* jsdoc
*/
Foo;
1 satisfies
/**
* jsdoc
*/
Foo;
@@ -91,6 +91,22 @@ Foo;
1 satisfies // comment
Foo;
1 as
/**
* jsdoc
*/
const;
1 as
/**
* jsdoc
*/
Foo;
1 satisfies
/**
* jsdoc
*/
Foo;
=====================================output=====================================
1 as const /*
comment
@@ -116,32 +132,51 @@ comment
1 as const /*
comment
*/;
1 as /*
1 as
/*
comment
*/
Foo;
1 satisfies /*
Foo;
1 satisfies
/*
comment
*/
Foo;
Foo;
1 as const;
/*
comment
*/
1 as /*
1 as
/*
comment
*/
Foo;
1 satisfies /*
Foo;
1 satisfies
/*
comment
*/
Foo;
Foo;
1 as const; // comment
1 as Foo; // comment
1 satisfies Foo; // comment
1 as const;
/**
* jsdoc
*/
1 as
/**
* jsdoc
*/
Foo;
1 satisfies
/**
* jsdoc
*/
Foo;
================================================================================
`;
@@ -381,11 +381,25 @@ parsers: ["typescript"]
semi: false
printWidth: 80 (default) |
=====================================input======================================
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3
} satisfies
// Comment
Record<string, number>;
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};
=====================================output=====================================
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3,
} satisfies
// Comment
Record<string, number>
const t2 = {} /* comment */ satisfies {}
const t3 = {} satisfies /* comment */ {}
const t4 = {} /* comment1 */ satisfies /* comment2 */ {}
@@ -398,24 +412,6 @@ exports[`comments.ts format 1`] = `
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};
=====================================output=====================================
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};
================================================================================
`;
exports[`comments-unstable.ts - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
semi: false
printWidth: 80 (default) |
=====================================input======================================
const t1 = {
prop1: 1,
prop2: 2,
@@ -423,38 +419,21 @@ const t1 = {
} satisfies
// Comment
Record<string, number>;
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};
=====================================output=====================================
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3,
} satisfies // Comment
Record<string, number>
================================================================================
`;
exports[`comments-unstable.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3
} satisfies
// Comment
Record<string, number>;
=====================================output=====================================
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3,
} satisfies // Comment
Record<string, number>;
// Comment
Record<string, number>;
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};
================================================================================
`;
@@ -1,7 +0,0 @@
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3
} satisfies
// Comment
Record<string, number>;
@@ -1,3 +1,10 @@
const t1 = {
prop1: 1,
prop2: 2,
prop3: 3
} satisfies
// Comment
Record<string, number>;
const t2 = {} /* comment */ satisfies {};
const t3 = {} satisfies /* comment */ {};
const t4 = {} /* comment1 */ satisfies /* comment2 */ {};