Fix comment between parentheses and function body (#15326)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-09-02 09:18:15 +08:00
1 parent 958b355ccb
commit 411ef345a2
5 files changed
+195 -21

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
#### Fix comment between parentheses and function body (#15326 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
function function_declaration()
// this is a function
{
return 42
}
(function function_expression()
// this is a function
{
return 42
})();
// Prettier stable
function function_declaration() {
// this is a function
return 42;
}
(function function_expression() // this is a function
{
return 42;
})();
// Prettier main
function function_declaration() {
// this is a function
return 42;
}
(function function_expression() {
// this is a function
return 42;
})();
```
<!-- prettier-ignore -->
```jsx
// Input
function function_declaration()
// this is a function
{
return 42
}
export default function()
// this is a function
{
return 42
}
// Prettier stable
TypeError: Cannot read properties of null (reading 'range')
// Prettier main
function function_declaration() {
// this is a function
return 42;
}
export default function () {
// this is a function
return 42;
}
```
+12 -21
View File
@@ -585,29 +585,20 @@ function handleLastFunctionArgComments({
return true;
}
// Comment between function parameters parentheses and function body
if (
enclosingNode?.type === "FunctionDeclaration" &&
followingNode?.type === "BlockStatement"
!isBlockComment(comment) &&
(enclosingNode?.type === "FunctionDeclaration" ||
enclosingNode?.type === "FunctionExpression" ||
enclosingNode?.type === "ObjectMethod") &&
followingNode?.type === "BlockStatement" &&
enclosingNode.body === followingNode
) {
const functionParamRightParenIndex = (() => {
const parameters = getFunctionParameters(enclosingNode);
if (parameters.length > 0) {
return getNextNonSpaceNonCommentCharacterIndex(
text,
locEnd(parameters.at(-1)),
);
}
const functionParamLeftParenIndex =
getNextNonSpaceNonCommentCharacterIndex(text, locEnd(enclosingNode.id));
return (
functionParamLeftParenIndex !== false &&
getNextNonSpaceNonCommentCharacterIndex(
text,
functionParamLeftParenIndex + 1,
)
);
})();
if (locStart(comment) > functionParamRightParenIndex) {
const characterAfterCommentIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
locEnd(comment),
);
if (characterAfterCommentIndex === locStart(followingNode)) {
addBlockStatementFirstComment(followingNode, comment);
return true;
}
@@ -0,0 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`between-parentheses-and-function-body.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
function function_declaration()
// this is a function
{
return 42
}
(function named()
// this is a function
{
return 42
})();
(function ()
// this is a function
{
return 42
})();
/* anonymous declaration */
export default function ()
// this is a function
{
return 42
}
a = {
foo()
// this is a function
{},
bar: function()
// this is a function
{},
}
=====================================output=====================================
function function_declaration() {
// this is a function
return 42;
}
(function named() {
// this is a function
return 42;
})();
(function () {
// this is a function
return 42;
})();
/* anonymous declaration */
export default function () {
// this is a function
return 42;
}
a = {
foo() {
// this is a function
},
bar: function () {
// this is a function
},
};
================================================================================
`;
@@ -0,0 +1,35 @@
function function_declaration()
// this is a function
{
return 42
}
(function named()
// this is a function
{
return 42
})();
(function ()
// this is a function
{
return 42
})();
/* anonymous declaration */
export default function ()
// this is a function
{
return 42
}
a = {
foo()
// this is a function
{},
bar: function()
// this is a function
{},
}
@@ -0,0 +1 @@
run_spec(import.meta, ["babel", "flow", "typescript"]);