Skip to content

Commit 178417f

Browse files
ajafffRyanCavanaugh
authored andcommitted
factory: correctly parenthesize conditional head (#34227)
Fixes: #34109
1 parent 3da51df commit 178417f

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4739,7 +4739,7 @@ namespace ts {
47394739
const conditionalPrecedence = getOperatorPrecedence(SyntaxKind.ConditionalExpression, SyntaxKind.QuestionToken);
47404740
const emittedCondition = skipPartiallyEmittedExpressions(condition);
47414741
const conditionPrecedence = getExpressionPrecedence(emittedCondition);
4742-
if (compareValues(conditionPrecedence, conditionalPrecedence) === Comparison.LessThan) {
4742+
if (compareValues(conditionPrecedence, conditionalPrecedence) !== Comparison.GreaterThan) {
47434743
return createParen(condition);
47444744
}
47454745
return condition;

tests/baselines/reference/propertyAccessChain.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ o5.b?.().c.d?.e;
1616

1717
// GH#33744
1818
declare const o6: <T>() => undefined | ({ x: number });
19-
o6<number>()?.x;
19+
o6<number>()?.x;
20+
21+
// GH#34109
22+
o1?.b ? 1 : 0;
2023

2124
//// [propertyAccessChain.js]
2225
"use strict";
23-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
26+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
2427
(_a = o1) === null || _a === void 0 ? void 0 : _a.b;
2528
(_b = o2) === null || _b === void 0 ? void 0 : _b.b.c;
2629
(_c = o3.b) === null || _c === void 0 ? void 0 : _c.c;
2730
(_e = (_d = o4.b) === null || _d === void 0 ? void 0 : _d.c.d) === null || _e === void 0 ? void 0 : _e.e;
2831
(_h = (_g = (_f = o5).b) === null || _g === void 0 ? void 0 : _g.call(_f).c.d) === null || _h === void 0 ? void 0 : _h.e;
2932
(_j = o6()) === null || _j === void 0 ? void 0 : _j.x;
33+
// GH#34109
34+
((_k = o1) === null || _k === void 0 ? void 0 : _k.b) ? 1 : 0;

tests/baselines/reference/propertyAccessChain.symbols

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ o6<number>()?.x;
7979
>o6 : Symbol(o6, Decl(propertyAccessChain.ts, 16, 13))
8080
>x : Symbol(x, Decl(propertyAccessChain.ts, 16, 41))
8181

82+
// GH#34109
83+
o1?.b ? 1 : 0;
84+
>o1?.b : Symbol(b, Decl(propertyAccessChain.ts, 0, 31))
85+
>o1 : Symbol(o1, Decl(propertyAccessChain.ts, 0, 13))
86+
>b : Symbol(b, Decl(propertyAccessChain.ts, 0, 31))
87+

tests/baselines/reference/propertyAccessChain.types

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,12 @@ o6<number>()?.x;
8080
>o6 : <T>() => { x: number; } | undefined
8181
>x : number | undefined
8282

83+
// GH#34109
84+
o1?.b ? 1 : 0;
85+
>o1?.b ? 1 : 0 : 0 | 1
86+
>o1?.b : string | undefined
87+
>o1 : { b: string; } | undefined
88+
>b : string | undefined
89+
>1 : 1
90+
>0 : 0
91+

tests/cases/conformance/expressions/optionalChaining/propertyAccessChain/propertyAccessChain.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ o5.b?.().c.d?.e;
1717

1818
// GH#33744
1919
declare const o6: <T>() => undefined | ({ x: number });
20-
o6<number>()?.x;
20+
o6<number>()?.x;
21+
22+
// GH#34109
23+
o1?.b ? 1 : 0;

0 commit comments

Comments
 (0)