From fd6ad2a623be5aed12b4718a6d08afbbdd4d84f6 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 5 Sep 2019 12:56:30 -0700 Subject: [PATCH] handle empty object param patterns with type annotations (#6438) * handle object patterns in function parameters with type annotations * fix lint * update changelog --- CHANGELOG.unreleased.md | 20 +++++++++++ src/language-js/printer-estree.js | 11 ++++-- .../__snapshots__/jsfmt.spec.js.snap | 34 +++++++++++++++++++ tests/flow_parameter_with_type/jsfmt.spec.js | 2 ++ tests/flow_parameter_with_type/param.js | 2 ++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/flow_parameter_with_type/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/flow_parameter_with_type/jsfmt.spec.js create mode 100644 tests/flow_parameter_with_type/param.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index ed73e5fb..f920c485 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -531,6 +531,25 @@ class Class { } ``` +#### JavaScript: Handle empty object patterns with type annotations in function parameters ([#6438] by [@bakkot]) + + +```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 diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index dad577e6..e010dc7e 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -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 { diff --git a/tests/flow_parameter_with_type/__snapshots__/jsfmt.spec.js.snap b/tests/flow_parameter_with_type/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..42006eb9 --- /dev/null +++ b/tests/flow_parameter_with_type/__snapshots__/jsfmt.spec.js.snap @@ -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) {} + +================================================================================ +`; diff --git a/tests/flow_parameter_with_type/jsfmt.spec.js b/tests/flow_parameter_with_type/jsfmt.spec.js new file mode 100644 index 00000000..381dfaee --- /dev/null +++ b/tests/flow_parameter_with_type/jsfmt.spec.js @@ -0,0 +1,2 @@ +run_spec(__dirname, ["flow", "babel"]); +run_spec(__dirname, ["flow", "babel"], { trailingComma: "all" }); diff --git a/tests/flow_parameter_with_type/param.js b/tests/flow_parameter_with_type/param.js new file mode 100644 index 00000000..45e4f4c6 --- /dev/null +++ b/tests/flow_parameter_with_type/param.js @@ -0,0 +1,2 @@ +const f = ({}: MyVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongType) => {}; +function g({}: Foo) {}