Don't treat Flow inexact tuples as empty (#18616)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-13 19:15:36 +08:00
1 parent 09ce948ee3
commit 3b9ecca4d9
4 files changed
+127 -18

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
#### Improve comment format in inexact tuples (#18616 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
type A = [// comment
...];
type B = [/* comment */...];
// Prettier stable
type A = [...
// comment
];
type B = [...
/* comment */
];
// Prettier main
type A = [
...// comment
];
type B = [.../* comment */];
```
+19 -18
View File
@@ -30,13 +30,11 @@ import { printTypeAnnotationProperty } from "./type-annotation.js";
function printEmptyArrayElements(path, options, openBracket, closeBracket) {
const { node } = path;
const inexact = node.inexact ? "..." : "";
if (!hasComment(node, CommentCheckFlags.Dangling)) {
return [openBracket, inexact, closeBracket];
return [openBracket, closeBracket];
}
return group([
openBracket,
inexact,
printDanglingComments(path, options, { indent: true }),
softline,
closeBracket,
@@ -58,7 +56,7 @@ function printArray(path, options, print) {
const closeBracket = "]";
const elementsProperty = isTupleType(node) ? "elementTypes" : "elements";
const elements = node[elementsProperty];
if (elements.length === 0) {
if (elements.length === 0 && !node.inexact) {
parts.push(
printEmptyArrayElements(path, options, openBracket, closeBracket),
);
@@ -82,23 +80,26 @@ function printArray(path, options, print) {
const groupId = Symbol("array");
const shouldBreak =
!options.__inJestEach &&
elements.length > 1 &&
elements.every((element, i, elements) => {
const elementType = element?.type;
if (!isArrayExpression(element) && !isObjectExpression(element)) {
return false;
}
(!options.__inJestEach &&
elements.length > 1 &&
elements.every((element, i, elements) => {
const elementType = element?.type;
if (!isArrayExpression(element) && !isObjectExpression(element)) {
return false;
}
const nextElement = elements[i + 1];
if (nextElement && elementType !== nextElement.type) {
return false;
}
const nextElement = elements[i + 1];
if (nextElement && elementType !== nextElement.type) {
return false;
}
const itemsKey = isArrayExpression(element) ? "elements" : "properties";
const itemsKey = isArrayExpression(element)
? "elements"
: "properties";
return element[itemsKey] && element[itemsKey].length > 1;
});
return element[itemsKey] && element[itemsKey].length > 1;
})) ||
hasComment(node, CommentCheckFlags.Dangling | CommentCheckFlags.Line);
const shouldUseConciseFormatting = isConciselyPrintedArray(node, options);
@@ -1,5 +1,63 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
exports[`comments.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
type Empty11 = [
// 11
...];
type Empty12 = [// 12
...];
type Empty13 = [
...// 13
];
type Empty14 = [
...
// 14
];
type Empty21 = [
/* 21 */
...];
type Empty22 = [/* 22 */
...];
type Empty23 = [
.../* 23 */
];
type Empty24 = [
...
/* 24 */
];
type Empty25 = [/* 25 */...];
type Empty26 = [.../* 25 */];
=====================================output=====================================
type Empty11 = [
...// 11
];
type Empty12 = [
...// 12
];
type Empty13 = [
...// 13
];
type Empty14 = [
...// 14
];
type Empty21 = [.../* 21 */];
type Empty22 = [.../* 22 */];
type Empty23 = [.../* 23 */];
type Empty24 = [.../* 24 */];
type Empty25 = [.../* 25 */];
type Empty26 = [.../* 25 */];
================================================================================
`;
exports[`inexact.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
+27
View File
@@ -0,0 +1,27 @@
type Empty11 = [
// 11
...];
type Empty12 = [// 12
...];
type Empty13 = [
...// 13
];
type Empty14 = [
...
// 14
];
type Empty21 = [
/* 21 */
...];
type Empty22 = [/* 22 */
...];
type Empty23 = [
.../* 23 */
];
type Empty24 = [
...
/* 24 */
];
type Empty25 = [/* 25 */...];
type Empty26 = [.../* 25 */];