Skip to content

Commit 5f017df

Browse files
authored
Fix top level logical assignment leak (microsoft#40536)
* Fix leaking top level logical assignment * Fix leaking top level logical assignment
1 parent 4a58fbc commit 5f017df

11 files changed

+270
-1
lines changed

src/compiler/binder.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,6 @@ namespace ts {
10691069
node = node.parent;
10701070
}
10711071
return !isStatementCondition(node) &&
1072-
!isLogicalAssignmentExpression(node.parent) &&
10731072
!isLogicalExpression(node.parent) &&
10741073
!(isOptionalChain(node.parent) && node.parent.expression === node);
10751074
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [logicalAssignment11.ts]
2+
let x: string | undefined;
3+
4+
let d: string | undefined;
5+
d ?? (d = x ?? "x")
6+
d.length;
7+
8+
let e: string | undefined;
9+
e ??= x ?? "x"
10+
e.length
11+
12+
//// [logicalAssignment11.js]
13+
"use strict";
14+
let x;
15+
let d;
16+
d !== null && d !== void 0 ? d : (d = x !== null && x !== void 0 ? x : "x");
17+
d.length;
18+
let e;
19+
e !== null && e !== void 0 ? e : (e = x !== null && x !== void 0 ? x : "x");
20+
e.length;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
4+
5+
let d: string | undefined;
6+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
7+
8+
d ?? (d = x ?? "x")
9+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
10+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
11+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
12+
13+
d.length;
14+
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
15+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
16+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
17+
18+
let e: string | undefined;
19+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
20+
21+
e ??= x ?? "x"
22+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
23+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
24+
25+
e.length
26+
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
27+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
28+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : string | undefined
4+
5+
let d: string | undefined;
6+
>d : string | undefined
7+
8+
d ?? (d = x ?? "x")
9+
>d ?? (d = x ?? "x") : string
10+
>d : string | undefined
11+
>(d = x ?? "x") : string
12+
>d = x ?? "x" : string
13+
>d : string | undefined
14+
>x ?? "x" : string
15+
>x : string | undefined
16+
>"x" : "x"
17+
18+
d.length;
19+
>d.length : number
20+
>d : string
21+
>length : number
22+
23+
let e: string | undefined;
24+
>e : string | undefined
25+
26+
e ??= x ?? "x"
27+
>e ??= x ?? "x" : string
28+
>e : string | undefined
29+
>x ?? "x" : string
30+
>x : string | undefined
31+
>"x" : "x"
32+
33+
e.length
34+
>e.length : number
35+
>e : string
36+
>length : number
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [logicalAssignment11.ts]
2+
let x: string | undefined;
3+
4+
let d: string | undefined;
5+
d ?? (d = x ?? "x")
6+
d.length;
7+
8+
let e: string | undefined;
9+
e ??= x ?? "x"
10+
e.length
11+
12+
//// [logicalAssignment11.js]
13+
"use strict";
14+
let x;
15+
let d;
16+
d ?? (d = x ?? "x");
17+
d.length;
18+
let e;
19+
e ?? (e = x ?? "x");
20+
e.length;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
4+
5+
let d: string | undefined;
6+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
7+
8+
d ?? (d = x ?? "x")
9+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
10+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
11+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
12+
13+
d.length;
14+
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
15+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
16+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
17+
18+
let e: string | undefined;
19+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
20+
21+
e ??= x ?? "x"
22+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
23+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
24+
25+
e.length
26+
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
27+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
28+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : string | undefined
4+
5+
let d: string | undefined;
6+
>d : string | undefined
7+
8+
d ?? (d = x ?? "x")
9+
>d ?? (d = x ?? "x") : string
10+
>d : string | undefined
11+
>(d = x ?? "x") : string
12+
>d = x ?? "x" : string
13+
>d : string | undefined
14+
>x ?? "x" : string
15+
>x : string | undefined
16+
>"x" : "x"
17+
18+
d.length;
19+
>d.length : number
20+
>d : string
21+
>length : number
22+
23+
let e: string | undefined;
24+
>e : string | undefined
25+
26+
e ??= x ?? "x"
27+
>e ??= x ?? "x" : string
28+
>e : string | undefined
29+
>x ?? "x" : string
30+
>x : string | undefined
31+
>"x" : "x"
32+
33+
e.length
34+
>e.length : number
35+
>e : string
36+
>length : number
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [logicalAssignment11.ts]
2+
let x: string | undefined;
3+
4+
let d: string | undefined;
5+
d ?? (d = x ?? "x")
6+
d.length;
7+
8+
let e: string | undefined;
9+
e ??= x ?? "x"
10+
e.length
11+
12+
//// [logicalAssignment11.js]
13+
"use strict";
14+
let x;
15+
let d;
16+
d ?? (d = x ?? "x");
17+
d.length;
18+
let e;
19+
e ??= x ?? "x";
20+
e.length;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
4+
5+
let d: string | undefined;
6+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
7+
8+
d ?? (d = x ?? "x")
9+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
10+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
11+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
12+
13+
d.length;
14+
>d.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
15+
>d : Symbol(d, Decl(logicalAssignment11.ts, 2, 3))
16+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
17+
18+
let e: string | undefined;
19+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
20+
21+
e ??= x ?? "x"
22+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
23+
>x : Symbol(x, Decl(logicalAssignment11.ts, 0, 3))
24+
25+
e.length
26+
>e.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
27+
>e : Symbol(e, Decl(logicalAssignment11.ts, 6, 3))
28+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/conformance/esnext/logicalAssignment/logicalAssignment11.ts ===
2+
let x: string | undefined;
3+
>x : string | undefined
4+
5+
let d: string | undefined;
6+
>d : string | undefined
7+
8+
d ?? (d = x ?? "x")
9+
>d ?? (d = x ?? "x") : string
10+
>d : string | undefined
11+
>(d = x ?? "x") : string
12+
>d = x ?? "x" : string
13+
>d : string | undefined
14+
>x ?? "x" : string
15+
>x : string | undefined
16+
>"x" : "x"
17+
18+
d.length;
19+
>d.length : number
20+
>d : string
21+
>length : number
22+
23+
let e: string | undefined;
24+
>e : string | undefined
25+
26+
e ??= x ?? "x"
27+
>e ??= x ?? "x" : string
28+
>e : string | undefined
29+
>x ?? "x" : string
30+
>x : string | undefined
31+
>"x" : "x"
32+
33+
e.length
34+
>e.length : number
35+
>e : string
36+
>length : number
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @strict: true
2+
// @target: esnext, es2020, es2015
3+
4+
let x: string | undefined;
5+
6+
let d: string | undefined;
7+
d ?? (d = x ?? "x")
8+
d.length;
9+
10+
let e: string | undefined;
11+
e ??= x ?? "x"
12+
e.length

0 commit comments

Comments
 (0)