Don't treat Flow inexact tuples as empty (#18616)
This commit is contained in:
1 parent
09ce948ee3
commit
3b9ecca4d9
4 files changed
+127
-18
No files matched your search
@@ -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 */];
|
||||||
|
```
|
||||||
@@ -30,13 +30,11 @@ import { printTypeAnnotationProperty } from "./type-annotation.js";
|
|||||||
|
|
||||||
function printEmptyArrayElements(path, options, openBracket, closeBracket) {
|
function printEmptyArrayElements(path, options, openBracket, closeBracket) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
const inexact = node.inexact ? "..." : "";
|
|
||||||
if (!hasComment(node, CommentCheckFlags.Dangling)) {
|
if (!hasComment(node, CommentCheckFlags.Dangling)) {
|
||||||
return [openBracket, inexact, closeBracket];
|
return [openBracket, closeBracket];
|
||||||
}
|
}
|
||||||
return group([
|
return group([
|
||||||
openBracket,
|
openBracket,
|
||||||
inexact,
|
|
||||||
printDanglingComments(path, options, { indent: true }),
|
printDanglingComments(path, options, { indent: true }),
|
||||||
softline,
|
softline,
|
||||||
closeBracket,
|
closeBracket,
|
||||||
@@ -58,7 +56,7 @@ function printArray(path, options, print) {
|
|||||||
const closeBracket = "]";
|
const closeBracket = "]";
|
||||||
const elementsProperty = isTupleType(node) ? "elementTypes" : "elements";
|
const elementsProperty = isTupleType(node) ? "elementTypes" : "elements";
|
||||||
const elements = node[elementsProperty];
|
const elements = node[elementsProperty];
|
||||||
if (elements.length === 0) {
|
if (elements.length === 0 && !node.inexact) {
|
||||||
parts.push(
|
parts.push(
|
||||||
printEmptyArrayElements(path, options, openBracket, closeBracket),
|
printEmptyArrayElements(path, options, openBracket, closeBracket),
|
||||||
);
|
);
|
||||||
@@ -82,23 +80,26 @@ function printArray(path, options, print) {
|
|||||||
const groupId = Symbol("array");
|
const groupId = Symbol("array");
|
||||||
|
|
||||||
const shouldBreak =
|
const shouldBreak =
|
||||||
!options.__inJestEach &&
|
(!options.__inJestEach &&
|
||||||
elements.length > 1 &&
|
elements.length > 1 &&
|
||||||
elements.every((element, i, elements) => {
|
elements.every((element, i, elements) => {
|
||||||
const elementType = element?.type;
|
const elementType = element?.type;
|
||||||
if (!isArrayExpression(element) && !isObjectExpression(element)) {
|
if (!isArrayExpression(element) && !isObjectExpression(element)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextElement = elements[i + 1];
|
const nextElement = elements[i + 1];
|
||||||
if (nextElement && elementType !== nextElement.type) {
|
if (nextElement && elementType !== nextElement.type) {
|
||||||
return false;
|
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);
|
const shouldUseConciseFormatting = isConciselyPrintedArray(node, options);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,63 @@
|
|||||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
// 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`] = `
|
exports[`inexact.js format 1`] = `
|
||||||
====================================options=====================================
|
====================================options=====================================
|
||||||
parsers: ["flow"]
|
parsers: ["flow"]
|
||||||
|
|||||||
@@ -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 */];
|
||||||
Reference in new issue
Block a user