Fix parenthesis for optional function as return type (#11004)
Co-authored-by: fisker Cheung <lionkay@gmail.com>
This commit is contained in:
1 parent
cc90434c80
commit
599bad1415
5 files changed
+75
-2
No files matched your search
@@ -0,0 +1,19 @@
|
|||||||
|
#### Fix parenthesis for optional function as return type (#11004 by @vjeux, @fisker)
|
||||||
|
|
||||||
|
<!-- prettier-ignore -->
|
||||||
|
```jsx
|
||||||
|
// Input
|
||||||
|
const fun = (a: number): (?(string => string)) => {
|
||||||
|
return a > 0 ? s => `${s}: ${a}` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prettier stable
|
||||||
|
const fun = (a: number): ?(string) => string => {
|
||||||
|
return a > 0 ? (s) => `${s}: ${a}` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Prettier main
|
||||||
|
const fun = (a: number): (?(string) => string) => {
|
||||||
|
return a > 0 ? (s) => `${s}: ${a}` : null;
|
||||||
|
};
|
||||||
|
```
|
||||||
@@ -558,12 +558,31 @@ function needsParentheses(path, options) {
|
|||||||
);
|
);
|
||||||
case "InferTypeAnnotation":
|
case "InferTypeAnnotation":
|
||||||
case "NullableTypeAnnotation":
|
case "NullableTypeAnnotation":
|
||||||
return (
|
if (
|
||||||
parent.type === "ArrayTypeAnnotation" ||
|
parent.type === "ArrayTypeAnnotation" ||
|
||||||
(key === "objectType" &&
|
(key === "objectType" &&
|
||||||
(parent.type === "IndexedAccessType" ||
|
(parent.type === "IndexedAccessType" ||
|
||||||
parent.type === "OptionalIndexedAccessType"))
|
parent.type === "OptionalIndexedAccessType"))
|
||||||
);
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the return type is a nullable arrow function, then we need a paren
|
||||||
|
// otherwise the inner => can be assumed to be for the outer one.
|
||||||
|
if (
|
||||||
|
node.type === "NullableTypeAnnotation" &&
|
||||||
|
path.match(
|
||||||
|
undefined,
|
||||||
|
(node, key) =>
|
||||||
|
key === "typeAnnotation" && node.type === "TypeAnnotation",
|
||||||
|
(node, key) =>
|
||||||
|
key === "returnType" && node.type === "ArrowFunctionExpression",
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case "ComponentTypeAnnotation":
|
case "ComponentTypeAnnotation":
|
||||||
case "FunctionTypeAnnotation": {
|
case "FunctionTypeAnnotation": {
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||||
|
|
||||||
|
exports[`optional.js format 1`] = `
|
||||||
|
====================================options=====================================
|
||||||
|
parsers: ["flow"]
|
||||||
|
printWidth: 80
|
||||||
|
| printWidth
|
||||||
|
=====================================input======================================
|
||||||
|
fun = (a: number): (?(string => string)) => {
|
||||||
|
return a > 0 ? s => \`\${s}: \${a}\` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
fun = (a: number): ?((string => string)) => {
|
||||||
|
return a > 0 ? s => \`\${s}: \${a}\` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
=====================================output=====================================
|
||||||
|
fun = (a: number): (?(string) => string) => {
|
||||||
|
return a > 0 ? (s) => \`\${s}: \${a}\` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
fun = (a: number): (?(string) => string) => {
|
||||||
|
return a > 0 ? (s) => \`\${s}: \${a}\` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
================================================================================
|
||||||
|
`;
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
runFormatTest(import.meta, ["flow"]);
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun = (a: number): (?(string => string)) => {
|
||||||
|
return a > 0 ? s => `${s}: ${a}` : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
fun = (a: number): ?((string => string)) => {
|
||||||
|
return a > 0 ? s => `${s}: ${a}` : null;
|
||||||
|
};
|
||||||
Reference in new issue
Block a user