Skip to content

Commit 6c11ceb

Browse files
Kingwlorta
andauthored
Assignment operator is definite assignment (microsoft#39087)
Co-authored-by: Orta Therox <[email protected]>
1 parent 25f6232 commit 6c11ceb

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,8 +2594,8 @@ namespace ts {
25942594
switch (parent.kind) {
25952595
case SyntaxKind.BinaryExpression:
25962596
const binaryOperator = (<BinaryExpression>parent).operatorToken.kind;
2597-
return isAssignmentOperator(binaryOperator) && !isLogicalOrCoalescingAssignmentOperator(binaryOperator) && (<BinaryExpression>parent).left === node ?
2598-
binaryOperator === SyntaxKind.EqualsToken ? AssignmentKind.Definite : AssignmentKind.Compound :
2597+
return isAssignmentOperator(binaryOperator) && (<BinaryExpression>parent).left === node ?
2598+
binaryOperator === SyntaxKind.EqualsToken || isLogicalOrCoalescingAssignmentOperator(binaryOperator) ? AssignmentKind.Definite : AssignmentKind.Compound :
25992599
AssignmentKind.None;
26002600
case SyntaxKind.PrefixUnaryExpression:
26012601
case SyntaxKind.PostfixUnaryExpression:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [logicalAssignment9.ts]
2+
declare let x: { a?: boolean };
3+
4+
x.a ??= true;
5+
x.a &&= false;
6+
7+
8+
//// [logicalAssignment9.js]
9+
"use strict";
10+
var _a;
11+
var _b, _c;
12+
(_a = (_b = x).a) !== null && _a !== void 0 ? _a : (_b.a = true);
13+
(_c = x).a && (_c.a = false);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment9.ts ===
2+
declare let x: { a?: boolean };
3+
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
4+
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
5+
6+
x.a ??= true;
7+
>x.a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
8+
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
9+
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
10+
11+
x.a &&= false;
12+
>x.a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
13+
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
14+
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
15+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment9.ts ===
2+
declare let x: { a?: boolean };
3+
>x : { a?: boolean | undefined; }
4+
>a : boolean | undefined
5+
6+
x.a ??= true;
7+
>x.a ??= true : boolean
8+
>x.a : boolean | undefined
9+
>x : { a?: boolean | undefined; }
10+
>a : boolean | undefined
11+
>true : true
12+
13+
x.a &&= false;
14+
>x.a &&= false : false | undefined
15+
>x.a : boolean | undefined
16+
>x : { a?: boolean | undefined; }
17+
>a : boolean | undefined
18+
>false : false
19+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @strict: true
2+
declare let x: { a?: boolean };
3+
4+
x.a ??= true;
5+
x.a &&= false;

0 commit comments

Comments
 (0)