Skip to content

Commit

Permalink
Flow: fix function type param (#8365)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 25, 2020
1 parent d048aa3 commit 82a3198
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
19 changes: 19 additions & 0 deletions changelog_unreleased/flow/pr-8365.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#### Fix function type param contains nullable param ([#8365](https://github.com/prettier/prettier/pull/8365) by [@fisker](https://github.com/fisker))

<!-- prettier-ignore -->
```flow
// Input
let f: <A>(
((?A) => B),
) => B;
// Prettier stable
let f: <A>((?A) => B) => B;
// Prettier stable (Second format)
SyntaxError: Unexpected token (1:12)
> 1 | let f: <A>((?A) => B) => B;
// Prettier master
let f: <A>(((?A) => B)) => B;
```
11 changes: 10 additions & 1 deletion src/language-js/needs-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,16 @@ function needsParens(path, options) {
// We should check ancestor's parent to know whether the parentheses
// are really needed, but since ??T doesn't make sense this check
// will almost never be true.
ancestor.type === "NullableTypeAnnotation"
ancestor.type === "NullableTypeAnnotation" ||
// See #5283
(parent.type === "FunctionTypeParam" &&
parent.name === null &&
node.params &&
node.params.some(
(param) =>
param.typeAnnotation &&
param.typeAnnotation.type === "NullableTypeAnnotation"
))
);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/flow/function-type-param/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`issue-5283.js format 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
| printWidth
=====================================input======================================
let f: <A>((
(?A) => B
)) => B;
export const testFunctionOnOptionsAsArgument: <T1,a>(?a, ((?a) => T1)) => T1 = function _(Arg1, Arg2) { const result = TypesBS.testFunctionOnOptionsAsArgument((Arg1 == null ? undefined : Arg1), Arg2); return result };
=====================================output=====================================
let f: <A>(((?A) => B)) => B;
export const testFunctionOnOptionsAsArgument: <T1, a>(
?a,
((?a) => T1)
) => T1 = function _(Arg1, Arg2) {
const result = TypesBS.testFunctionOnOptionsAsArgument(
Arg1 == null ? undefined : Arg1,
Arg2
);
return result;
};
================================================================================
`;
7 changes: 7 additions & 0 deletions tests/flow/function-type-param/issue-5283.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

let f: <A>((
(?A) => B
)) => B;


export const testFunctionOnOptionsAsArgument: <T1,a>(?a, ((?a) => T1)) => T1 = function _(Arg1, Arg2) { const result = TypesBS.testFunctionOnOptionsAsArgument((Arg1 == null ? undefined : Arg1), Arg2); return result };
1 change: 1 addition & 0 deletions tests/flow/function-type-param/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["flow", "babel"]);

0 comments on commit 82a3198

Please sign in to comment.