From 7a76e5d08ed9a54c79a8687aed46b7ac44a73857 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 15 May 2019 18:46:12 +0300 Subject: [PATCH] fix(javascript): correctly handle comments in empty arrow function expressions (#6086) --- CHANGELOG.unreleased.md | 15 +++++++++++++++ src/language-js/comments.js | 4 +--- .../comments/__snapshots__/jsfmt.spec.js.snap | 18 ++++++++++++++++++ tests/comments/arrow.js | 3 +++ 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/comments/arrow.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index bc4e886e..c54e4edf 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -95,6 +95,20 @@ console.log( ); ``` +### JavaScript: Correctly handle comments in empty arrow function expressions ([#6086] by [@evilebottnawi]) + + +```js +// Input +const fn = (/*event, data*/) => doSomething(anything); + +// Output (Prettier stable) +const fn = () => /*event, data*/ doSomething(anything); + +// Output (Prettier master) +const fn = (/*event, data*/) => doSomething(anything); +``` + ### TypeScript: Keep trailing comma in tsx type parameters ([#6115] by [@sosukesuzuki]) Previously, a trailing comma after single type parameter in arrow function was cleaned up. The formatted result is valid as ts, but is invalid as tsx. Prettier master fixes this issue. @@ -203,6 +217,7 @@ This changes the method of finding the required count of backticks from using 2 ```` [#5979]: https://github.com/prettier/prettier/pull/5979 +[#6086]: https://github.com/prettier/prettier/pull/6086 [#6088]: https://github.com/prettier/prettier/pull/6088 [#6115]: https://github.com/prettier/prettier/pull/6115 [#6106]: https://github.com/prettier/prettier/pull/6106 diff --git a/src/language-js/comments.js b/src/language-js/comments.js index be1394e0..8048bb82 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -558,9 +558,7 @@ function handleCommentInEmptyParens(text, enclosingNode, comment, options) { enclosingNode && (((enclosingNode.type === "FunctionDeclaration" || enclosingNode.type === "FunctionExpression" || - (enclosingNode.type === "ArrowFunctionExpression" && - (enclosingNode.body.type !== "CallExpression" || - enclosingNode.body.arguments.length === 0)) || + enclosingNode.type === "ArrowFunctionExpression" || enclosingNode.type === "ClassMethod" || enclosingNode.type === "ObjectMethod") && enclosingNode.params.length === 0) || diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 1cc4bce1..f40d5959 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,23 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`arrow.js 1`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +const fn = (/*event, data*/) => doSomething(); + +const fn2 = (/*event, data*/) => doSomething(anything); + +=====================================output===================================== +const fn = (/*event, data*/) => doSomething(); + +const fn2 = (/*event, data*/) => doSomething(anything); + +================================================================================ +`; + exports[`assignment-pattern.js 1`] = ` ====================================options===================================== parsers: ["flow", "babel"] diff --git a/tests/comments/arrow.js b/tests/comments/arrow.js new file mode 100644 index 00000000..699f4d36 --- /dev/null +++ b/tests/comments/arrow.js @@ -0,0 +1,3 @@ +const fn = (/*event, data*/) => doSomething(); + +const fn2 = (/*event, data*/) => doSomething(anything);