Skip to content

Assignment operator is definite assignment #39087

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 2 commits into from
Jun 16, 2020
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
4 changes: 2 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2594,8 +2594,8 @@ namespace ts {
switch (parent.kind) {
case SyntaxKind.BinaryExpression:
const binaryOperator = (<BinaryExpression>parent).operatorToken.kind;
return isAssignmentOperator(binaryOperator) && !isLogicalOrCoalescingAssignmentOperator(binaryOperator) && (<BinaryExpression>parent).left === node ?
binaryOperator === SyntaxKind.EqualsToken ? AssignmentKind.Definite : AssignmentKind.Compound :
return isAssignmentOperator(binaryOperator) && (<BinaryExpression>parent).left === node ?
binaryOperator === SyntaxKind.EqualsToken || isLogicalOrCoalescingAssignmentOperator(binaryOperator) ? AssignmentKind.Definite : AssignmentKind.Compound :
AssignmentKind.None;
case SyntaxKind.PrefixUnaryExpression:
case SyntaxKind.PostfixUnaryExpression:
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/logicalAssignment9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [logicalAssignment9.ts]
declare let x: { a?: boolean };

x.a ??= true;
x.a &&= false;


//// [logicalAssignment9.js]
"use strict";
var _a;
var _b, _c;
(_a = (_b = x).a) !== null && _a !== void 0 ? _a : (_b.a = true);
(_c = x).a && (_c.a = false);
15 changes: 15 additions & 0 deletions tests/baselines/reference/logicalAssignment9.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment9.ts ===
declare let x: { a?: boolean };
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))

x.a ??= true;
>x.a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))

x.a &&= false;
>x.a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))
>x : Symbol(x, Decl(logicalAssignment9.ts, 0, 11))
>a : Symbol(a, Decl(logicalAssignment9.ts, 0, 16))

19 changes: 19 additions & 0 deletions tests/baselines/reference/logicalAssignment9.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment9.ts ===
declare let x: { a?: boolean };
>x : { a?: boolean | undefined; }
>a : boolean | undefined

x.a ??= true;
>x.a ??= true : boolean
>x.a : boolean | undefined
>x : { a?: boolean | undefined; }
>a : boolean | undefined
>true : true

x.a &&= false;
>x.a &&= false : false | undefined
>x.a : boolean | undefined
>x : { a?: boolean | undefined; }
>a : boolean | undefined
>false : false

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @strict: true
declare let x: { a?: boolean };

x.a ??= true;
x.a &&= false;