Skip to content

Commit a585735

Browse files
fix for 45006
1 parent 40ec839 commit a585735

File tree

7 files changed

+104
-3
lines changed

7 files changed

+104
-3
lines changed

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,7 +1823,6 @@ namespace ts {
18231823
case SyntaxKind.Constructor:
18241824
case SyntaxKind.FunctionDeclaration:
18251825
case SyntaxKind.MethodSignature:
1826-
case SyntaxKind.GetAccessor:
18271826
case SyntaxKind.SetAccessor:
18281827
case SyntaxKind.CallSignature:
18291828
case SyntaxKind.JSDocSignature:
@@ -1835,6 +1834,7 @@ namespace ts {
18351834
case SyntaxKind.ClassStaticBlockDeclaration:
18361835
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike;
18371836

1837+
case SyntaxKind.GetAccessor:
18381838
case SyntaxKind.FunctionExpression:
18391839
case SyntaxKind.ArrowFunction:
18401840
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike | ContainerFlags.IsFunctionExpression;
@@ -3372,7 +3372,7 @@ namespace ts {
33723372
emitFlags |= NodeFlags.HasAsyncFunctions;
33733373
}
33743374

3375-
if (currentFlow && isObjectLiteralOrClassExpressionMethod(node)) {
3375+
if (currentFlow && (node.kind === SyntaxKind.GetAccessor || isObjectLiteralOrClassExpressionMethod(node))) {
33763376
node.flowNode = currentFlow;
33773377
}
33783378

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24436,7 +24436,7 @@ namespace ts {
2443624436
// a const variable or parameter from an outer function, we extend the origin of the control flow
2443724437
// analysis to include the immediately enclosing function.
2443824438
while (flowContainer !== declarationContainer && (flowContainer.kind === SyntaxKind.FunctionExpression ||
24439-
flowContainer.kind === SyntaxKind.ArrowFunction || isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
24439+
flowContainer.kind === SyntaxKind.ArrowFunction || flowContainer.kind === SyntaxKind.GetAccessor || isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
2444024440
(isConstVariable(localOrExportSymbol) && type !== autoArrayType || isParameter && !isParameterAssigned(localOrExportSymbol))) {
2444124441
flowContainer = getControlFlowContainer(flowContainer);
2444224442
}

tests/baselines/reference/getterControlFlowStrictNull.errors.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ tests/cases/compiler/getterControlFlowStrictNull.ts(20,9): error TS2366: Functio
3939

4040
set a(value: number) {
4141
}
42+
}
43+
44+
// Repro from #45006
45+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
46+
if (typeof x === "string") {
47+
let obj = {
48+
get prop() { return x.toUpperCase() },
49+
method() { return x.toUpperCase() }
50+
}
4251
}

tests/baselines/reference/getterControlFlowStrictNull.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class C {
2828

2929
set a(value: number) {
3030
}
31+
}
32+
33+
// Repro from #45006
34+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
35+
if (typeof x === "string") {
36+
let obj = {
37+
get prop() { return x.toUpperCase() },
38+
method() { return x.toUpperCase() }
39+
}
3140
}
3241

3342
//// [getterControlFlowStrictNull.js]
@@ -74,3 +83,11 @@ var C = /** @class */ (function () {
7483
});
7584
return C;
7685
}());
86+
// Repro from #45006
87+
var x = Math.random() < 0.5 ? "str" : 123;
88+
if (typeof x === "string") {
89+
var obj = {
90+
get prop() { return x.toUpperCase(); },
91+
method: function () { return x.toUpperCase(); }
92+
};
93+
}

tests/baselines/reference/getterControlFlowStrictNull.symbols

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@ class C {
5555
>value : Symbol(value, Decl(getterControlFlowStrictNull.ts, 27, 10))
5656
}
5757
}
58+
59+
// Repro from #45006
60+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
61+
>x : Symbol(x, Decl(getterControlFlowStrictNull.ts, 32, 5))
62+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
63+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
64+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
65+
66+
if (typeof x === "string") {
67+
>x : Symbol(x, Decl(getterControlFlowStrictNull.ts, 32, 5))
68+
69+
let obj = {
70+
>obj : Symbol(obj, Decl(getterControlFlowStrictNull.ts, 34, 5))
71+
72+
get prop() { return x.toUpperCase() },
73+
>prop : Symbol(prop, Decl(getterControlFlowStrictNull.ts, 34, 13))
74+
>x.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
75+
>x : Symbol(x, Decl(getterControlFlowStrictNull.ts, 32, 5))
76+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
77+
78+
method() { return x.toUpperCase() }
79+
>method : Symbol(method, Decl(getterControlFlowStrictNull.ts, 35, 42))
80+
>x.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
81+
>x : Symbol(x, Decl(getterControlFlowStrictNull.ts, 32, 5))
82+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
83+
}
84+
}

tests/baselines/reference/getterControlFlowStrictNull.types

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,42 @@ class C {
6969
>value : number
7070
}
7171
}
72+
73+
// Repro from #45006
74+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
75+
>x : string | number
76+
>Math.random() < 0.5 ? "str" : 123 : "str" | 123
77+
>Math.random() < 0.5 : boolean
78+
>Math.random() : number
79+
>Math.random : () => number
80+
>Math : Math
81+
>random : () => number
82+
>0.5 : 0.5
83+
>"str" : "str"
84+
>123 : 123
85+
86+
if (typeof x === "string") {
87+
>typeof x === "string" : boolean
88+
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
89+
>x : string | number
90+
>"string" : "string"
91+
92+
let obj = {
93+
>obj : { readonly prop: string; method(): string; }
94+
>{ get prop() { return x.toUpperCase() }, method() { return x.toUpperCase() } } : { readonly prop: string; method(): string; }
95+
96+
get prop() { return x.toUpperCase() },
97+
>prop : string
98+
>x.toUpperCase() : string
99+
>x.toUpperCase : () => string
100+
>x : string
101+
>toUpperCase : () => string
102+
103+
method() { return x.toUpperCase() }
104+
>method : () => string
105+
>x.toUpperCase() : string
106+
>x.toUpperCase : () => string
107+
>x : string
108+
>toUpperCase : () => string
109+
}
110+
}

tests/cases/compiler/getterControlFlowStrictNull.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ class C {
2929

3030
set a(value: number) {
3131
}
32+
}
33+
34+
// Repro from #45006
35+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
36+
if (typeof x === "string") {
37+
let obj = {
38+
get prop() { return x.toUpperCase() },
39+
method() { return x.toUpperCase() }
40+
}
3241
}

0 commit comments

Comments
 (0)