Fix top level logical assignment leak (#40536)

* Fix leaking top level logical assignment

* Fix leaking top level logical assignment
This commit is contained in:
Wenlu Wang 2022-03-15 01:48:31 +08:00 committed by GitHub
parent 4a58fbce17
commit 5f017df609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 270 additions and 1 deletions

View File

@ -1069,7 +1069,6 @@ namespace ts {
node = node.parent;
}
return !isStatementCondition(node) &&
!isLogicalAssignmentExpression(node.parent) &&
!isLogicalExpression(node.parent) &&
!(isOptionalChain(node.parent) && node.parent.expression === node);
}

View File

@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;
let d: string | undefined;
d ?? (d = x ?? "x")
d.length;
let e: string | undefined;
e ??= x ?? "x"
e.length
//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d !== null && d !== void 0 ? d : (d = x !== null && x !== void 0 ? x : "x");
d.length;
let e;
e !== null && e !== void 0 ? e : (e = x !== null && x !== void 0 ? x : "x");
e.length;

View File

@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

View File

@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined
let d: string | undefined;
>d : string | undefined
d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
d.length;
>d.length : number
>d : string
>length : number
let e: string | undefined;
>e : string | undefined
e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
e.length
>e.length : number
>e : string
>length : number

View File

@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;
let d: string | undefined;
d ?? (d = x ?? "x")
d.length;
let e: string | undefined;
e ??= x ?? "x"
e.length
//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d ?? (d = x ?? "x");
d.length;
let e;
e ?? (e = x ?? "x");
e.length;

View File

@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

View File

@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined
let d: string | undefined;
>d : string | undefined
d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
d.length;
>d.length : number
>d : string
>length : number
let e: string | undefined;
>e : string | undefined
e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
e.length
>e.length : number
>e : string
>length : number

View File

@ -0,0 +1,20 @@
//// [logicalAssignment11.ts]
let x: string | undefined;
let d: string | undefined;
d ?? (d = x ?? "x")
d.length;
let e: string | undefined;
e ??= x ?? "x"
e.length
//// [logicalAssignment11.js]
"use strict";
let x;
let d;
d ?? (d = x ?? "x");
d.length;
let e;
e ??= x ?? "x";
e.length;

View File

@ -0,0 +1,29 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
let d: string | undefined;
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
d ?? (d = x ?? "x")
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
d.length;
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
let e: string | undefined;
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
e ??= x ?? "x"
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
e.length
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))

View File

@ -0,0 +1,37 @@
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
let x: string | undefined;
>x : string | undefined
let d: string | undefined;
>d : string | undefined
d ?? (d = x ?? "x")
>d ?? (d = x ?? "x") : string
>d : string | undefined
>(d = x ?? "x") : string
>d = x ?? "x" : string
>d : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
d.length;
>d.length : number
>d : string
>length : number
let e: string | undefined;
>e : string | undefined
e ??= x ?? "x"
>e ??= x ?? "x" : string
>e : string | undefined
>x ?? "x" : string
>x : string | undefined
>"x" : "x"
e.length
>e.length : number
>e : string
>length : number

View File

@ -0,0 +1,12 @@
// @strict: true
// @target: esnext, es2020, es2015
let x: string | undefined;
let d: string | undefined;
d ?? (d = x ?? "x")
d.length;
let e: string | undefined;
e ??= x ?? "x"
e.length