fix(typescript): stable parens for function type in arrow return type (#5790)
parent
1061be0702
commit
153d2d0570
|
@ -121,8 +121,34 @@ Examples:
|
|||
_foo <InlineJSX /> bar_
|
||||
```
|
||||
|
||||
- TypeScript: Stable parentheses for function type in the return type of arrow function ([#5790] by [@ikatyang])
|
||||
|
||||
There's a regression introduced in 1.16 that
|
||||
parentheses for function type in the return type of arrow function were kept adding/removing.
|
||||
Their parentheses are always printed now.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```ts
|
||||
// Input
|
||||
const foo = (): (() => void) => (): void => null;
|
||||
const bar = (): () => void => (): void => null;
|
||||
|
||||
// First Output (Prettier stable)
|
||||
const foo = (): () => void => (): void => null;
|
||||
const bar = (): (() => void) => (): void => null;
|
||||
|
||||
// Second Output (Prettier stable)
|
||||
const foo = (): (() => void) => (): void => null;
|
||||
const bar = (): () => void => (): void => null;
|
||||
|
||||
// Output (Prettier master)
|
||||
const foo = (): (() => void) => (): void => null;
|
||||
const bar = (): (() => void) => (): void => null;
|
||||
```
|
||||
|
||||
[@ikatyang]: https://github.com/ikatyang
|
||||
[@simenb]: https://github.com/SimenB
|
||||
[#5778]: https://github.com/prettier/prettier/pull/5778
|
||||
[#5783]: https://github.com/prettier/prettier/pull/5783
|
||||
[#5785]: https://github.com/prettier/prettier/pull/5785
|
||||
[#5790]: https://github.com/prettier/prettier/pull/5790
|
||||
|
|
|
@ -353,6 +353,20 @@ function needsParens(path, options) {
|
|||
|
||||
case "TSParenthesizedType": {
|
||||
const grandParent = path.getParentNode(1);
|
||||
|
||||
/**
|
||||
* const foo = (): (() => void) => (): void => null;
|
||||
* ^ ^
|
||||
*/
|
||||
if (
|
||||
getUnparenthesizedNode(node).type === "TSFunctionType" &&
|
||||
parent.type === "TSTypeAnnotation" &&
|
||||
grandParent.type === "ArrowFunctionExpression" &&
|
||||
grandParent.returnType === parent
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
(parent.type === "TSTypeParameter" ||
|
||||
parent.type === "TypeParameter" ||
|
||||
|
@ -719,6 +733,12 @@ function isStatement(node) {
|
|||
);
|
||||
}
|
||||
|
||||
function getUnparenthesizedNode(node) {
|
||||
return node.type === "TSParenthesizedType"
|
||||
? getUnparenthesizedNode(node.typeAnnotation)
|
||||
: node;
|
||||
}
|
||||
|
||||
function endsWithRightBracket(node) {
|
||||
switch (node.type) {
|
||||
case "ObjectExpression":
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`type-annotation.ts 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
const foo = (): () => void => (): void => null;
|
||||
const bar = (): (() => void) => (): void => null;
|
||||
const baz = (): ((() => void)) => (): void => null;
|
||||
|
||||
=====================================output=====================================
|
||||
const foo = (): (() => void) => (): void => null;
|
||||
const bar = (): (() => void) => (): void => null;
|
||||
const baz = (): (() => void) => (): void => null;
|
||||
|
||||
================================================================================
|
||||
`;
|
|
@ -0,0 +1 @@
|
|||
run_spec(__dirname, ["typescript"]);
|
|
@ -0,0 +1,3 @@
|
|||
const foo = (): () => void => (): void => null;
|
||||
const bar = (): (() => void) => (): void => null;
|
||||
const baz = (): ((() => void)) => (): void => null;
|
Loading…
Reference in New Issue