Skip to content

factory: correctly parenthesize conditional head #34227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4739,7 +4739,7 @@ namespace ts {
const conditionalPrecedence = getOperatorPrecedence(SyntaxKind.ConditionalExpression, SyntaxKind.QuestionToken);
const emittedCondition = skipPartiallyEmittedExpressions(condition);
const conditionPrecedence = getExpressionPrecedence(emittedCondition);
if (compareValues(conditionPrecedence, conditionalPrecedence) === Comparison.LessThan) {
if (compareValues(conditionPrecedence, conditionalPrecedence) !== Comparison.GreaterThan) {
return createParen(condition);
}
return condition;
Expand Down
9 changes: 7 additions & 2 deletions tests/baselines/reference/propertyAccessChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ o5.b?.().c.d?.e;

// GH#33744
declare const o6: <T>() => undefined | ({ x: number });
o6<number>()?.x;
o6<number>()?.x;

// GH#34109
o1?.b ? 1 : 0;

//// [propertyAccessChain.js]
"use strict";
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
(_a = o1) === null || _a === void 0 ? void 0 : _a.b;
(_b = o2) === null || _b === void 0 ? void 0 : _b.b.c;
(_c = o3.b) === null || _c === void 0 ? void 0 : _c.c;
(_e = (_d = o4.b) === null || _d === void 0 ? void 0 : _d.c.d) === null || _e === void 0 ? void 0 : _e.e;
(_h = (_g = (_f = o5).b) === null || _g === void 0 ? void 0 : _g.call(_f).c.d) === null || _h === void 0 ? void 0 : _h.e;
(_j = o6()) === null || _j === void 0 ? void 0 : _j.x;
// GH#34109
((_k = o1) === null || _k === void 0 ? void 0 : _k.b) ? 1 : 0;
6 changes: 6 additions & 0 deletions tests/baselines/reference/propertyAccessChain.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ o6<number>()?.x;
>o6 : Symbol(o6, Decl(propertyAccessChain.ts, 16, 13))
>x : Symbol(x, Decl(propertyAccessChain.ts, 16, 41))

// GH#34109
o1?.b ? 1 : 0;
>o1?.b : Symbol(b, Decl(propertyAccessChain.ts, 0, 31))
>o1 : Symbol(o1, Decl(propertyAccessChain.ts, 0, 13))
>b : Symbol(b, Decl(propertyAccessChain.ts, 0, 31))

9 changes: 9 additions & 0 deletions tests/baselines/reference/propertyAccessChain.types
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,12 @@ o6<number>()?.x;
>o6 : <T>() => { x: number; } | undefined
>x : number | undefined

// GH#34109
o1?.b ? 1 : 0;
>o1?.b ? 1 : 0 : 0 | 1
>o1?.b : string | undefined
>o1 : { b: string; } | undefined
>b : string | undefined
>1 : 1
>0 : 0

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ o5.b?.().c.d?.e;

// GH#33744
declare const o6: <T>() => undefined | ({ x: number });
o6<number>()?.x;
o6<number>()?.x;

// GH#34109
o1?.b ? 1 : 0;