Normalize union type comment alignment in TypeScript conditional types (#18644)
Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
1 parent
7f455be5af
commit
72a182f594
4 files changed
+231
-1
No files matched your search
@@ -0,0 +1,25 @@
|
||||
#### Fix conditional type comment indentation (#18644 by @lindsaycode05)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
type T = any extends B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? undefined | NonNullable<B>[foo]
|
||||
: B[foo];
|
||||
|
||||
// Prettier stable
|
||||
type T = any extends B
|
||||
? // Comment
|
||||
// Multiline comment
|
||||
undefined | NonNullable<B>[foo]
|
||||
: B[foo];
|
||||
|
||||
// Prettier main
|
||||
type T = any extends B
|
||||
? // Comment
|
||||
// Multiline comment
|
||||
undefined | NonNullable<B>[foo]
|
||||
: B[foo];
|
||||
```
|
||||
@@ -43,7 +43,6 @@ function printUnionType(path, options, print) {
|
||||
// If there's a leading comment, the parent is doing the indentation
|
||||
const shouldIndent =
|
||||
parent.type !== "TypeParameterInstantiation" &&
|
||||
(!isConditionalType(parent) || !options.experimentalTernaries) &&
|
||||
parent.type !== "TSTypeParameterInstantiation" &&
|
||||
parent.type !== "GenericTypeAnnotation" &&
|
||||
parent.type !== "TSTypeReference" &&
|
||||
@@ -54,6 +53,11 @@ function printUnionType(path, options, print) {
|
||||
!parent.name &&
|
||||
path.grandparent.this !== parent
|
||||
) &&
|
||||
!(
|
||||
isConditionalType(parent) &&
|
||||
!options.experimentalTernaries &&
|
||||
hasLeadingOwnLineComment(options.originalText, node)
|
||||
) &&
|
||||
!(
|
||||
(isTypeAlias(parent) || parent.type === "VariableDeclarator") &&
|
||||
hasLeadingOwnLineComment(options.originalText, node)
|
||||
|
||||
@@ -79,6 +79,47 @@ type T = test extends B
|
||||
type T = test extends B ? test extends B /* c
|
||||
c */? foo : bar : bar;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
type T = any extends B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
|
||||
=====================================output=====================================
|
||||
type A =
|
||||
B extends T ?
|
||||
@@ -158,6 +199,46 @@ c */
|
||||
: bar
|
||||
: bar;
|
||||
|
||||
type T =
|
||||
any extends B ?
|
||||
// Comment
|
||||
// Multiline comment
|
||||
B | C
|
||||
: D;
|
||||
T =
|
||||
any instanceof B ?
|
||||
// Comment
|
||||
// Multiline comment
|
||||
B | C
|
||||
: D;
|
||||
|
||||
type T =
|
||||
any extends B ?
|
||||
// Comment
|
||||
B | C
|
||||
: D;
|
||||
T =
|
||||
any instanceof B ?
|
||||
// Comment
|
||||
B | C
|
||||
: D;
|
||||
|
||||
type T =
|
||||
any extends B ?
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
B | C
|
||||
: D;
|
||||
T = any instanceof B ?
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
B | C
|
||||
: D;
|
||||
type T = any extends B ? /* Comment */ B | C : D;
|
||||
T = any instanceof B ? /* Comment */ B | C : D;
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
@@ -239,6 +320,47 @@ type T = test extends B
|
||||
type T = test extends B ? test extends B /* c
|
||||
c */? foo : bar : bar;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
type T = any extends B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
|
||||
=====================================output=====================================
|
||||
type A = B extends T
|
||||
? // comment
|
||||
@@ -310,6 +432,44 @@ c */
|
||||
: bar
|
||||
: bar;
|
||||
|
||||
type T = any extends B
|
||||
? // Comment
|
||||
// Multiline comment
|
||||
B | C
|
||||
: D;
|
||||
T =
|
||||
any instanceof B
|
||||
? // Comment
|
||||
// Multiline comment
|
||||
B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
? // Comment
|
||||
B | C
|
||||
: D;
|
||||
T =
|
||||
any instanceof B
|
||||
? // Comment
|
||||
B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
? /**
|
||||
* Comment
|
||||
*/
|
||||
B | C
|
||||
: D;
|
||||
T =
|
||||
any instanceof B
|
||||
? /**
|
||||
* Comment
|
||||
*/
|
||||
B | C
|
||||
: D;
|
||||
type T = any extends B ? /* Comment */ B | C : D;
|
||||
T = any instanceof B ? /* Comment */ B | C : D;
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
|
||||
@@ -69,3 +69,44 @@ type T = test extends B
|
||||
|
||||
type T = test extends B ? test extends B /* c
|
||||
c */? foo : bar : bar;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
// Multiline comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
// Comment
|
||||
? B | C
|
||||
: D;
|
||||
|
||||
type T = any extends B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
/**
|
||||
* Comment
|
||||
*/
|
||||
? B | C
|
||||
: D;
|
||||
type T = any extends B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
T = any instanceof B
|
||||
?/* Comment */
|
||||
B | C
|
||||
: D;
|
||||
Reference in new issue
Block a user