Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 543d87a

Browse files
committed
Fix missing parentheses in ConditionalTypeAnnotation
1 parent af6e721 commit 543d87a

4 files changed

Lines changed: 32 additions & 24 deletions

File tree

src/language-js/needs-parens.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import {
1010
isBinaryCastExpression,
1111
isBitwiseOperator,
1212
isCallExpression,
13+
isConditionalType,
14+
isIntersectionType,
1315
isMemberExpression,
1416
isNullishCoalescing,
1517
isObjectOrRecordExpression,
1618
isObjectProperty,
19+
isUnionType,
1720
shouldFlatten,
1821
startsWithNoLookaheadToken,
1922
} from "./utils/index.js";
@@ -525,11 +528,20 @@ function needsParens(path, options) {
525528
// fallthrough
526529
case "TSConditionalType":
527530
case "TSConstructorType":
528-
if (key === "extendsType" && parent.type === "TSConditionalType") {
529-
if (node.type === "TSConditionalType") {
530-
return true;
531-
}
531+
case "ConditionalTypeAnnotation":
532+
if (
533+
key === "extendsType" &&
534+
isConditionalType(node) &&
535+
parent.type === node.type
536+
) {
537+
return true;
538+
}
539+
540+
if (key === "checkType" && isConditionalType(parent)) {
541+
return true;
542+
}
532543

544+
if (key === "extendsType" && parent.type === "TSConditionalType") {
533545
let { typeAnnotation } = node.returnType || node.typeAnnotation;
534546

535547
if (
@@ -547,15 +559,11 @@ function needsParens(path, options) {
547559
}
548560
}
549561

550-
if (key === "checkType" && parent.type === "TSConditionalType") {
551-
return true;
552-
}
553562
// fallthrough
554563
case "TSUnionType":
555564
case "TSIntersectionType":
556565
if (
557-
(parent.type === "TSUnionType" ||
558-
parent.type === "TSIntersectionType") &&
566+
(isUnionType(parent) || isIntersectionType(parent)) &&
559567
parent.types.length > 1 &&
560568
(!node.types || node.types.length > 1)
561569
) {
@@ -706,19 +714,6 @@ function needsParens(path, options) {
706714
);
707715
}
708716

709-
case "ConditionalTypeAnnotation":
710-
if (
711-
key === "extendsType" &&
712-
parent.type === "ConditionalTypeAnnotation" &&
713-
node.type === "ConditionalTypeAnnotation"
714-
) {
715-
return true;
716-
}
717-
718-
if (key === "checkType" && parent.type === "ConditionalTypeAnnotation") {
719-
return true;
720-
}
721-
722717
// fallthrough
723718
case "OptionalIndexedAccessType":
724719
return key === "objectType" && parent.type === "IndexedAccessType";

src/language-js/utils/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,13 +1060,18 @@ const isBinaryCastExpression = createTypeCheckFunction([
10601060
]);
10611061

10621062
const isUnionType = createTypeCheckFunction([
1063-
"UnionTypeAnnotation",
10641063
"TSUnionType",
1064+
"UnionTypeAnnotation",
10651065
]);
10661066

10671067
const isIntersectionType = createTypeCheckFunction([
1068-
"IntersectionTypeAnnotation",
10691068
"TSIntersectionType",
1069+
"IntersectionTypeAnnotation",
1070+
]);
1071+
1072+
const isConditionalType = createTypeCheckFunction([
1073+
"TSConditionalType",
1074+
"ConditionalTypeAnnotation",
10701075
]);
10711076

10721077
export {
@@ -1092,6 +1097,7 @@ export {
10921097
isBitwiseOperator,
10931098
isCallExpression,
10941099
isCallLikeExpression,
1100+
isConditionalType,
10951101
isExportDeclaration,
10961102
isFlowObjectTypePropertyAFunction,
10971103
isFunctionCompositionArgs,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runFormatTest(import.meta, ["flow", "typescript"], {
2+
errors: {
3+
"babel-flow": true,
4+
},
5+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type T<U> = 'a' | ('b' extends U ? 'c' : empty);
2+
type T<U> = 'a' & ('b' extends U ? 'c' : empty);

0 commit comments

Comments
 (0)