Skip to content

Commit

Permalink
Add parenthesis around leading multiline comment in return statement (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
auvred committed Jul 16, 2023
1 parent a5a556f commit 8da37cc
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 1 deletion.
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'
)
}

0 comments on commit 8da37cc

Please sign in to comment.