Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parenthesis around leading multiline comment in return statement #15037

Merged
merged 3 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions changelog_unreleased/javascript/15037.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#### Add parenthesis around leading multiline comment in return statement (#15037 by @auvred)

<!-- prettier-ignore -->
```jsx
// Input
function fn() {
return (
/**
* @type {...}
*/ expresssion
)
}

// Prettier stable
function fn() {
return /**
* @type {...}
*/ expresssion;
}

// Prettier main
function fn() {
return (
/**
* @type {...}
*/ expresssion
);
}
```
15 changes: 14 additions & 1 deletion src/language-js/print/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import {
getFunctionParameters,
hasLeadingOwnLineComment,
isBinaryish,
isJsxElement,
hasComment,
CommentCheckFlags,
isCallExpression,
getCallArguments,
hasNakedLeftSide,
getLeftSide,
} from "../utils/index.js";
import hasNewlineInRange from "../../utils/has-newline-in-range.js";
import { locEnd, locStart } from "../loc.js";
import {
printFunctionParameters,
shouldGroupFunctionParameters,
Expand Down Expand Up @@ -286,7 +289,17 @@ function printThrowStatement(path, options, print) {
// (the leftmost leaf node) and, if it (or its parents) has any
// leadingComments, returns true (so it can be wrapped in parens).
function returnArgumentHasLeadingComment(options, argument) {
if (hasLeadingOwnLineComment(options.originalText, argument)) {
if (
hasLeadingOwnLineComment(options.originalText, argument) ||
(hasComment(argument, CommentCheckFlags.Leading, (comment) =>
hasNewlineInRange(
options.originalText,
locStart(comment),
locEnd(comment),
),
) &&
!isJsxElement(argument))
) {
return true;
}

Expand Down
184 changes: 184 additions & 0 deletions tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4733,6 +4733,53 @@ function inlineComment() {
) || 42
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ 'result'
)
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
'result'
)
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
)
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
)
}

function singleLineBlockSameLine() {
return (
/** Result -> */ 'result'
)
}

function singleLineBlockNextLine() {
return (
/** Result below */
'result'
)
}

=====================================output=====================================
function jsx() {
return (
Expand Down Expand Up @@ -4864,6 +4911,51 @@ function inlineComment() {
return /* hi */ 42 || 42
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ "result"
)
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
"result"
)
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
)
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
)
}

function singleLineBlockSameLine() {
return /** Result -> */ "result"
}

function singleLineBlockNextLine() {
return (
/** Result below */
"result"
)
}

================================================================================
`;

Expand Down Expand Up @@ -4995,6 +5087,53 @@ function inlineComment() {
) || 42
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ 'result'
)
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
'result'
)
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
)
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
)
}

function singleLineBlockSameLine() {
return (
/** Result -> */ 'result'
)
}

function singleLineBlockNextLine() {
return (
/** Result below */
'result'
)
}

=====================================output=====================================
function jsx() {
return (
Expand Down Expand Up @@ -5126,6 +5265,51 @@ function inlineComment() {
return /* hi */ 42 || 42;
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ "result"
);
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
"result"
);
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
);
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
);
}

function singleLineBlockSameLine() {
return /** Result -> */ "result";
}

function singleLineBlockNextLine() {
return (
/** Result below */
"result"
);
}

================================================================================
`;

Expand Down
47 changes: 47 additions & 0 deletions tests/format/js/comments/return-statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,50 @@ function inlineComment() {
/* hi */ 42
) || 42
}

function multilineBlockSameLine() {
return (
/**
* @type {string}
*/ 'result'
)
}

function multilineBlockNextLine() {
return (
/**
* @type {string}
*/
'result'
)
}

function multilineBlockSameLineJsx() {
return (
/**
* JSX Same line
*/ <div></div>
)
}

function multilineBlockNextLineJsx() {
return (
/**
* JSX Next line
*/
<div></div>
)
}

function singleLineBlockSameLine() {
return (
/** Result -> */ 'result'
)
}

function singleLineBlockNextLine() {
return (
/** Result below */
'result'
)
}