Remove arrow function return type check (#18589)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-14 06:56:11 +08:00
1 parent c2b2f9ad99
commit 6e85d4a785
4 files changed
+152 -24

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
#### Fix inconsistent formatting of arrow function (#18589 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
a.map(() => ({
name,
}));
a.map(():A => ({
name,
}));
a.map(():{A} => ({
name,
}));
// Prettier stable
a.map(() => ({
name,
}));
a.map(
(): A => ({
name,
}),
);
a.map((): { A } => ({
name,
}));
// Prettier main
a.map(() => ({
name,
}));
a.map((): A => ({
name,
}));
a.map((): { A } => ({
name,
}));
```
-24
View File
@@ -209,22 +209,6 @@ function couldExpandArg(arg, arrowChainRecursion = false) {
(isBinaryCastExpression(arg) && couldExpandArg(arg.expression)) ||
arg.type === "FunctionExpression" ||
(arg.type === "ArrowFunctionExpression" &&
// we want to avoid breaking inside composite return types but not simple keywords
// https://github.com/prettier/prettier/issues/4070
// export class Thing implements OtherThing {
// do: (type: Type) => Provider<Prop> = memoize(
// (type: ObjectType): Provider<Opts> => {}
// );
// }
// https://github.com/prettier/prettier/issues/6099
// app.get("/", (req, res): void => {
// res.send("Hello World!");
// });
(!arg.returnType ||
!arg.returnType.typeAnnotation ||
arg.returnType.typeAnnotation.type !== "TSTypeReference" ||
// https://github.com/prettier/prettier/issues/7542
isNonEmptyBlockStatement(arg.body)) &&
(arg.body.type === "BlockStatement" ||
(arg.body.type === "ArrowFunctionExpression" &&
couldExpandArg(arg.body, true)) ||
@@ -383,14 +367,6 @@ function isValidHookCallbackAndDepsFormat(args, baseIndex) {
);
}
function isNonEmptyBlockStatement(node) {
return (
node.type === "BlockStatement" &&
(node.body.some((node) => node.type !== "EmptyStatement") ||
hasComment(node, CommentCheckFlags.Dangling))
);
}
// `{ type: "module" }` and `{"type": "module"}`
function isTypeModuleObjectExpression(node) {
if (!(node.type === "ObjectExpression" && node.properties.length === 1)) {
@@ -0,0 +1,35 @@
a.map(() => ({
name,
}));
a.map<A>(() => ({
name,
}));
a.map(():A => ({
name,
}));
a.map(():{A} => ({
name,
}));
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/element/src/elbowArrow.ts#L1882C1-L1899C5
_vertical.flatMap((y, row) =>
_horizontal.map(
(x, col): Node => ({
f: 0,
}),
),
);
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/excalidraw/actions/actionProperties.tsx#L1852
updateElbowArrowPoints(newElement, elementsMap, {
points: [startGlobalPoint, endGlobalPoint].map(
(p): LocalPoint => pointFrom(p[0] - newElement.x, p[1] - newElement.y),
),
});
// https://github.com/vuejs/core/blob/aac7e1898907445c8f89b22047a9bfcf0a6e91b8/packages-private/sfc-playground/src/App.vue#L50
sfcOptions = computed(
(): SFCOptions => ({
a,
}),
);
@@ -45,3 +45,81 @@ export function foo3(text: string): { Foo: string; Bar: string } {
================================================================================
`;
exports[`18588.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
a.map(() => ({
name,
}));
a.map<A>(() => ({
name,
}));
a.map(():A => ({
name,
}));
a.map(():{A} => ({
name,
}));
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/element/src/elbowArrow.ts#L1882C1-L1899C5
_vertical.flatMap((y, row) =>
_horizontal.map(
(x, col): Node => ({
f: 0,
}),
),
);
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/excalidraw/actions/actionProperties.tsx#L1852
updateElbowArrowPoints(newElement, elementsMap, {
points: [startGlobalPoint, endGlobalPoint].map(
(p): LocalPoint => pointFrom(p[0] - newElement.x, p[1] - newElement.y),
),
});
// https://github.com/vuejs/core/blob/aac7e1898907445c8f89b22047a9bfcf0a6e91b8/packages-private/sfc-playground/src/App.vue#L50
sfcOptions = computed(
(): SFCOptions => ({
a,
}),
);
=====================================output=====================================
a.map(() => ({
name,
}));
a.map<A>(() => ({
name,
}));
a.map((): A => ({
name,
}));
a.map((): { A } => ({
name,
}));
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/element/src/elbowArrow.ts#L1882C1-L1899C5
_vertical.flatMap((y, row) =>
_horizontal.map((x, col): Node => ({
f: 0,
})),
);
// https://github.com/excalidraw/excalidraw/blob/e95222ed323d45adf2183bae835c9fb6708e7029/packages/excalidraw/actions/actionProperties.tsx#L1852
updateElbowArrowPoints(newElement, elementsMap, {
points: [startGlobalPoint, endGlobalPoint].map((p): LocalPoint =>
pointFrom(p[0] - newElement.x, p[1] - newElement.y),
),
});
// https://github.com/vuejs/core/blob/aac7e1898907445c8f89b22047a9bfcf0a6e91b8/packages-private/sfc-playground/src/App.vue#L50
sfcOptions = computed((): SFCOptions => ({
a,
}));
================================================================================
`;