Fix printing comment for the last operand of union types (#15409)
This commit is contained in:
1 parent
c041f3fb4f
commit
4169dbb871
6 files changed
+155
No files matched your search
@@ -0,0 +1,36 @@
|
||||
#### Fix printing comment for the last operand of union types (#15409 by @sosukesuzuki)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
type Foo1 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
|
||||
// Prettier stable
|
||||
type Foo1 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2"
|
||||
)[]; // Comment2 // Final comment1
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2"
|
||||
) & // Comment2
|
||||
Bar; // Final comment2
|
||||
|
||||
// Prettier main
|
||||
type Foo1 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
type Foo2 = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) &
|
||||
Bar; // Final comment2
|
||||
```
|
||||
@@ -84,6 +84,7 @@ function handleEndOfLineComment(context) {
|
||||
handleVariableDeclaratorComments,
|
||||
handleBreakAndContinueStatementComments,
|
||||
handleSwitchDefaultCaseComments,
|
||||
handleLastUnionElementInExpression,
|
||||
].some((fn) => fn(context));
|
||||
}
|
||||
|
||||
@@ -887,6 +888,44 @@ function handleSwitchDefaultCaseComments({
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle `Comment2` and `Comment4`.
|
||||
*
|
||||
* type Foo = (
|
||||
* | "thing1" // Comment1
|
||||
* | "thing2" // Comment2
|
||||
* )[];
|
||||
*
|
||||
* type Foo = (
|
||||
* | "thing1" // Comment3
|
||||
* | "thing2" // Comment4
|
||||
* ) & Bar;
|
||||
*
|
||||
* @param {CommentContext} context
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function handleLastUnionElementInExpression({
|
||||
comment,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
followingNode,
|
||||
}) {
|
||||
if (
|
||||
precedingNode &&
|
||||
(precedingNode.type === "TSUnionType" ||
|
||||
precedingNode.type === "UnionTypeAnnotation") &&
|
||||
(((enclosingNode.type === "TSArrayType" ||
|
||||
enclosingNode.type === "ArrayTypeAnnotation") &&
|
||||
followingNode === undefined) ||
|
||||
enclosingNode.type === "TSIntersectionType" ||
|
||||
enclosingNode.type === "IntersectionTypeAnnotation")
|
||||
) {
|
||||
addTrailingComment(precedingNode.types.at(-1), comment);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
|
||||
@@ -22,6 +22,37 @@ const myValue = (callcallcallcallcallcall(
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`comments.js format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
|
||||
=====================================output=====================================
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) &
|
||||
Bar; // Final comment2
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`union.js format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["flow"]
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
@@ -1,5 +1,36 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`comments.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
|
||||
=====================================output=====================================
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) &
|
||||
Bar; // Final comment2
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`inlining.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
)[]; // Final comment1
|
||||
|
||||
type Foo = (
|
||||
| "thing1" // Comment1
|
||||
| "thing2" // Comment2
|
||||
) & Bar; // Final comment2
|
||||
Reference in new issue
Block a user