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:
1 parent
33b0865f40
commit
f675f9f3c0
4 files changed
+267
-2
No files matched your search
@@ -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;
|
||||
```
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import getNextNonSpaceNonCommentCharacterIndex from "../../utils/get-next-non-space-non-comment-character-index.js";
|
||||
import { locEnd } from "../loc.js";
|
||||
import {
|
||||
CommentCheckFlags,
|
||||
getFunctionParameters,
|
||||
hasComment,
|
||||
hasLeadingOwnLineComment,
|
||||
@@ -119,8 +120,9 @@ function printArrowFunction(path, options, print, args = {}) {
|
||||
signatureDocs,
|
||||
shouldBreak: shouldBreakChain,
|
||||
});
|
||||
let shouldBreakSignatures;
|
||||
let shouldBreakSignatures = false;
|
||||
let shouldIndentSignatures = false;
|
||||
let shouldPrintSoftlineInIndent = false;
|
||||
if (
|
||||
shouldPrintAsChain &&
|
||||
(isCallee ||
|
||||
@@ -128,6 +130,13 @@ function printArrowFunction(path, options, print, args = {}) {
|
||||
args.assignmentLayout)
|
||||
) {
|
||||
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 =
|
||||
args.assignmentLayout === "chain-tail-arrow-chain" ||
|
||||
(isCallee && !shouldPutBodyOnSameLine);
|
||||
@@ -143,7 +152,7 @@ function printArrowFunction(path, options, print, args = {}) {
|
||||
return group([
|
||||
group(
|
||||
shouldIndentSignatures
|
||||
? indent([softline, signaturesDoc])
|
||||
? indent([shouldPrintSoftlineInIndent ? softline : "", signaturesDoc])
|
||||
: signaturesDoc,
|
||||
{ shouldBreak: shouldBreakSignatures, id: chainGroupId },
|
||||
),
|
||||
|
||||
@@ -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
|
||||
|
||||
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`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
|
||||
Reference in new issue
Block a user