handle empty object param patterns with type annotations (#6438)

* handle object patterns in function parameters with type annotations

* fix lint

* update changelog
master
Kevin Gibbons 2019-09-05 12:56:30 -07:00 committed by Simon Lydell
parent ff9b2b6cb5
commit fd6ad2a623
5 changed files with 66 additions and 3 deletions

View File

@ -531,6 +531,25 @@ class Class {
}
```
#### JavaScript: Handle empty object patterns with type annotations in function parameters ([#6438] by [@bakkot])
<!-- prettier-ignore -->
```js
// Input
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
// Output (Prettier stable)
const f = ({
,
}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({ }: Foo) {}
// Output (Prettier master)
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
```
[#5910]: https://github.com/prettier/prettier/pull/5910
[#6186]: https://github.com/prettier/prettier/pull/6186
[#6206]: https://github.com/prettier/prettier/pull/6206
@ -549,6 +568,7 @@ class Class {
[#6423]: https://github.com/prettier/prettier/pull/6423
[#6420]: https://github.com/prettier/prettier/pull/6420
[#6411]: https://github.com/prettier/prettier/pull/6411
[#6438]: https://github.com/prettier/prettier/pull/6411
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce
[@sosukesuzuki]: https://github.com/sosukesuzuki

View File

@ -1379,9 +1379,13 @@ function printPathNoParens(path, options, print, args) {
);
let content;
if (props.length === 0 && !n.typeAnnotation) {
if (props.length === 0) {
if (!hasDanglingComments(n)) {
return concat([leftBrace, rightBrace]);
return concat([
leftBrace,
rightBrace,
printTypeAnnotation(path, options, print)
]);
}
content = group(
@ -1390,7 +1394,8 @@ function printPathNoParens(path, options, print, args) {
comments.printDanglingComments(path, options),
softline,
rightBrace,
printOptionalToken(path)
printOptionalToken(path),
printTypeAnnotation(path, options, print)
])
);
} else {

View File

@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`param.js 1`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
| printWidth
=====================================input======================================
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
=====================================output=====================================
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
================================================================================
`;
exports[`param.js 2`] = `
====================================options=====================================
parsers: ["flow", "babel"]
printWidth: 80
trailingComma: "all"
| printWidth
=====================================input======================================
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
=====================================output=====================================
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}
================================================================================
`;

View File

@ -0,0 +1,2 @@
run_spec(__dirname, ["flow", "babel"]);
run_spec(__dirname, ["flow", "babel"], { trailingComma: "all" });

View File

@ -0,0 +1,2 @@
const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {};
function g({}: Foo) {}