Skip to content

Commit

Permalink
Flow: add support for inexact tuple types (#16271)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkz committed May 11, 2024
1 parent 26f1cd9 commit 5a51577
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
15 changes: 15 additions & 0 deletions changelog_unreleased/flow/16271.md
@@ -0,0 +1,15 @@
#### Support Flow's inexact tuple types (#16271 by @gkz)

Adds support for Flow's inexact tuple types.

<!-- prettier-ignore -->
```jsx
// Input
type T = [number, ...];

// Prettier stable
type T = [number];

// Prettier main
type T = [number, ...];
```
23 changes: 18 additions & 5 deletions src/language-js/print/array.js
Expand Up @@ -29,11 +29,13 @@ 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, closeBracket];
return [openBracket, inexact, closeBracket];
}
return group([
openBracket,
inexact,
printDanglingComments(path, options, { indent: true }),
softline,
closeBracket,
Expand Down Expand Up @@ -68,7 +70,8 @@ function printArray(path, options, print) {
);
} else {
const lastElem = elements.at(-1);
const canHaveTrailingComma = lastElem?.type !== "RestElement";
const canHaveTrailingComma =
lastElem?.type !== "RestElement" && !node.inexact;

// JavaScript allows you to have empty elements in an array which
// changes its length based on the number of commas. The algorithm
Expand Down Expand Up @@ -129,7 +132,13 @@ function printArray(path, options, print) {
shouldUseConciseFormatting
? printArrayElementsConcisely(path, options, print, trailingComma)
: [
printArrayElements(path, options, elementsProperty, print),
printArrayElements(
path,
options,
elementsProperty,
node.inexact,
print,
),
trailingComma,
],
printDanglingComments(path, options),
Expand Down Expand Up @@ -183,13 +192,13 @@ function isLineAfterElementEmpty({ node }, { originalText: text }) {
return isNextLineEmptyAfterIndex(text, skipToComma(locEnd(node)));
}

function printArrayElements(path, options, elementsProperty, print) {
function printArrayElements(path, options, elementsProperty, inexact, print) {
const parts = [];

path.each(({ node, isLast }) => {
parts.push(node ? group(print()) : "");

if (!isLast) {
if (!isLast || inexact) {
parts.push([
",",
line,
Expand All @@ -198,6 +207,10 @@ function printArrayElements(path, options, elementsProperty, print) {
}
}, elementsProperty);

if (inexact) {
parts.push("...");
}

return parts;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/format/flow/tuples/__snapshots__/format.test.js.snap
@@ -1,5 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`inexact.js [babel-flow] format 1`] = `
"Unexpected token (1:15)
> 1 | type Empty = [...];
| ^
2 |
3 | type One = [number, ...];
4 |
Cause: Unexpected token (1:14)"
`;

exports[`inexact.js format 1`] = `
====================================options=====================================
parsers: ["flow"]
printWidth: 80
| printWidth
=====================================input======================================
type Empty = [...];
type One = [number, ...];
=====================================output=====================================
type Empty = [...];
type One = [number, ...];
================================================================================
`;

exports[`labeled.js [babel-flow] format 1`] = `
"Unexpected token, expected "," (1:12)
> 1 | type T = [a: string, b: number];
Expand Down
3 changes: 3 additions & 0 deletions tests/format/flow/tuples/inexact.js
@@ -0,0 +1,3 @@
type Empty = [...];

type One = [number, ...];

0 comments on commit 5a51577

Please sign in to comment.