Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow: add support for inexact tuple types #16271

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions changelog_unreleased/flow/16271.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Empty = [...];

type One = [number, ...];