Don't print an extra line break for arrow function with type parameter in assignment (#16586)

* Dont print extra softline

* Update snapshot

* Add changelog
This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-08-19 11:05:32 +09:00
1 parent 33b0865f40
commit f675f9f3c0
4 files changed
+267 -2

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
#### Don't print an extra line break for arrow function with type parameter in assignment (#16586 by @sosukesuzuki)
There was a bug where an extra line was inserted when assigning a chained arrow function with type parameters to a variable if there was a line comment above it.
This change ensures that the extra line is no longer inserted.
<!-- prettier-ignore -->
```tsx
// Input
const foo1 =
// comment
<T,>() => () => 1;
// Prettier stable
const foo1 =
// comment
<T,>() =>
() =>
1;
// Prettier main
const foo1 =
// comment
<T,>() =>
() =>
1;
```
+11 -2
View File
@@ -16,6 +16,7 @@ import {
import getNextNonSpaceNonCommentCharacterIndex from "../../utils/get-next-non-space-non-comment-character-index.js"; import getNextNonSpaceNonCommentCharacterIndex from "../../utils/get-next-non-space-non-comment-character-index.js";
import { locEnd } from "../loc.js"; import { locEnd } from "../loc.js";
import { import {
CommentCheckFlags,
getFunctionParameters, getFunctionParameters,
hasComment, hasComment,
hasLeadingOwnLineComment, hasLeadingOwnLineComment,
@@ -119,8 +120,9 @@ function printArrowFunction(path, options, print, args = {}) {
signatureDocs, signatureDocs,
shouldBreak: shouldBreakChain, shouldBreak: shouldBreakChain,
}); });
let shouldBreakSignatures; let shouldBreakSignatures = false;
let shouldIndentSignatures = false; let shouldIndentSignatures = false;
let shouldPrintSoftlineInIndent = false;
if ( if (
shouldPrintAsChain && shouldPrintAsChain &&
(isCallee || (isCallee ||
@@ -128,6 +130,13 @@ function printArrowFunction(path, options, print, args = {}) {
args.assignmentLayout) args.assignmentLayout)
) { ) {
shouldIndentSignatures = true; shouldIndentSignatures = true;
// If the arrow function has a leading line comment, there should be a hardline above it
// so we should not print a softline in indent call
// https://github.com/prettier/prettier/issues/16067
shouldPrintSoftlineInIndent = !hasComment(
path.node,
CommentCheckFlags.Leading & CommentCheckFlags.Line,
);
shouldBreakSignatures = shouldBreakSignatures =
args.assignmentLayout === "chain-tail-arrow-chain" || args.assignmentLayout === "chain-tail-arrow-chain" ||
(isCallee && !shouldPutBodyOnSameLine); (isCallee && !shouldPutBodyOnSameLine);
@@ -143,7 +152,7 @@ function printArrowFunction(path, options, print, args = {}) {
return group([ return group([
group( group(
shouldIndentSignatures shouldIndentSignatures
? indent([softline, signaturesDoc]) ? indent([shouldPrintSoftlineInIndent ? softline : "", signaturesDoc])
: signaturesDoc, : signaturesDoc,
{ shouldBreak: shouldBreakSignatures, id: chainGroupId }, { shouldBreak: shouldBreakSignatures, id: chainGroupId },
), ),
+35
View File
@@ -0,0 +1,35 @@
const foo1 =
// comment
<T>() => () => 1;
const foo2 =
// comment
() => () => 1;
const foo3 =
// comment
<T>() => 1;
foo(
// comment
<T>() => () => 1,
);
a ||
// comment
(<T>() => () => 1);
void
// comment
(<T>() => () => 1);
cond ?
// comment
<T>() => () => 1
:
// comment
<T>() => () => 1;
foo4 =
// comment
<T>() => () => 1;
@@ -1,5 +1,198 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`16067.ts - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
const foo1 =
// comment
<T>() => () => 1;
const foo2 =
// comment
() => () => 1;
const foo3 =
// comment
<T>() => 1;
foo(
// comment
<T>() => () => 1,
);
a ||
// comment
(<T>() => () => 1);
void
// comment
(<T>() => () => 1);
cond ?
// comment
<T>() => () => 1
:
// comment
<T>() => () => 1;
foo4 =
// comment
<T>() => () => 1;
=====================================output=====================================
const foo1 =
// comment
<T>() =>
() =>
1
const foo2 =
// comment
() => () => 1
const foo3 =
// comment
<T>() => 1
foo(
// comment
<T>() =>
() =>
1,
)
a ||
// comment
(<T>() =>
() =>
1)
void (
// comment
(<T>() =>
() =>
1)
)
cond
? // comment
<T>() =>
() =>
1
: // comment
<T>() =>
() =>
1
foo4 =
// comment
<T>() =>
() =>
1
================================================================================
`;
exports[`16067.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
const foo1 =
// comment
<T>() => () => 1;
const foo2 =
// comment
() => () => 1;
const foo3 =
// comment
<T>() => 1;
foo(
// comment
<T>() => () => 1,
);
a ||
// comment
(<T>() => () => 1);
void
// comment
(<T>() => () => 1);
cond ?
// comment
<T>() => () => 1
:
// comment
<T>() => () => 1;
foo4 =
// comment
<T>() => () => 1;
=====================================output=====================================
const foo1 =
// comment
<T>() =>
() =>
1;
const foo2 =
// comment
() => () => 1;
const foo3 =
// comment
<T>() => 1;
foo(
// comment
<T>() =>
() =>
1,
);
a ||
// comment
(<T>() =>
() =>
1);
void (
// comment
(<T>() =>
() =>
1)
);
cond
? // comment
<T>() =>
() =>
1
: // comment
<T>() =>
() =>
1;
foo4 =
// comment
<T>() =>
() =>
1;
================================================================================
`;
exports[`arrow_regression.ts - {"semi":false} format 1`] = ` exports[`arrow_regression.ts - {"semi":false} format 1`] = `
====================================options===================================== ====================================options=====================================
parsers: ["typescript"] parsers: ["typescript"]