Skip to content

Commit 85a39c6

Browse files
treat setters like getters in preceding commit; move test accordingly
1 parent a585735 commit 85a39c6

12 files changed

+128
-107
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.SetAccessor:
18271826
case SyntaxKind.CallSignature:
18281827
case SyntaxKind.JSDocSignature:
18291828
case SyntaxKind.JSDocFunctionType:
@@ -1835,6 +1834,7 @@ namespace ts {
18351834
return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike;
18361835

18371836
case SyntaxKind.GetAccessor:
1837+
case SyntaxKind.SetAccessor:
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 && (node.kind === SyntaxKind.GetAccessor || isObjectLiteralOrClassExpressionMethod(node))) {
3375+
if (currentFlow && (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor || 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 || flowContainer.kind === SyntaxKind.GetAccessor || isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
24439+
flowContainer.kind === SyntaxKind.ArrowFunction || flowContainer.kind === SyntaxKind.GetAccessor || flowContainer.kind === SyntaxKind.SetAccessor || isObjectLiteralOrClassExpressionMethod(flowContainer)) &&
2444024440
(isConstVariable(localOrExportSymbol) && type !== autoArrayType || isParameter && !isParameterAssigned(localOrExportSymbol))) {
2444124441
flowContainer = getControlFlowContainer(flowContainer);
2444224442
}

tests/baselines/reference/getterControlFlowStrictNull.errors.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,4 @@ 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-
}
5142
}

tests/baselines/reference/getterControlFlowStrictNull.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ 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-
}
4031
}
4132

4233
//// [getterControlFlowStrictNull.js]
@@ -83,11 +74,3 @@ var C = /** @class */ (function () {
8374
});
8475
return C;
8576
}());
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: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,3 @@ 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: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,42 +69,3 @@ 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/baselines/reference/gettersAndSetters.errors.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ tests/cases/compiler/gettersAndSetters.ts(25,13): error TS2339: Property 'Baz' d
66
tests/cases/compiler/gettersAndSetters.ts(26,3): error TS2339: Property 'Baz' does not exist on type 'C'.
77
tests/cases/compiler/gettersAndSetters.ts(29,30): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
88
tests/cases/compiler/gettersAndSetters.ts(29,53): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
9+
tests/cases/compiler/gettersAndSetters.ts(45,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
10+
tests/cases/compiler/gettersAndSetters.ts(46,9): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
911

1012

11-
==== tests/cases/compiler/gettersAndSetters.ts (8 errors) ====
13+
==== tests/cases/compiler/gettersAndSetters.ts (10 errors) ====
1214
// classes
1315
class C {
1416
public fooBack = "";
@@ -64,4 +66,17 @@ tests/cases/compiler/gettersAndSetters.ts(29,53): error TS1056: Accessors are on
6466
}
6567

6668
var i:I1 = function (n) {return n;}
67-
69+
70+
// Repro from #45006
71+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
72+
if (typeof x === "string") {
73+
let obj = {
74+
set prop(_: any) { x.toUpperCase(); },
75+
~~~~
76+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
77+
get prop() { return x.toUpperCase() },
78+
~~~~
79+
!!! error TS1056: Accessors are only available when targeting ECMAScript 5 and higher.
80+
method() { return x.toUpperCase() }
81+
}
82+
}

tests/baselines/reference/gettersAndSetters.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ interface I1 {
3838
}
3939

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

4352
//// [gettersAndSetters.js]
4453
// classes
@@ -80,3 +89,12 @@ var o = { get Foo() { return 0; }, set Foo(val) { val; } }; // o
8089
var ofg = o.Foo;
8190
o.Foo = 0;
8291
var i = function (n) { return n; };
92+
// Repro from #45006
93+
var x = Math.random() < 0.5 ? "str" : 123;
94+
if (typeof x === "string") {
95+
var obj = {
96+
set prop(_) { x.toUpperCase(); },
97+
get prop() { return x.toUpperCase(); },
98+
method: function () { return x.toUpperCase(); }
99+
};
100+
}

tests/baselines/reference/gettersAndSetters.symbols

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,36 @@ var i:I1 = function (n) {return n;}
114114
>n : Symbol(n, Decl(gettersAndSetters.ts, 38, 21))
115115
>n : Symbol(n, Decl(gettersAndSetters.ts, 38, 21))
116116

117+
// Repro from #45006
118+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
119+
>x : Symbol(x, Decl(gettersAndSetters.ts, 41, 5))
120+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
121+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
122+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
123+
124+
if (typeof x === "string") {
125+
>x : Symbol(x, Decl(gettersAndSetters.ts, 41, 5))
126+
127+
let obj = {
128+
>obj : Symbol(obj, Decl(gettersAndSetters.ts, 43, 5))
129+
130+
set prop(_: any) { x.toUpperCase(); },
131+
>prop : Symbol(prop, Decl(gettersAndSetters.ts, 43, 13), Decl(gettersAndSetters.ts, 44, 42))
132+
>_ : Symbol(_, Decl(gettersAndSetters.ts, 44, 13))
133+
>x.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
134+
>x : Symbol(x, Decl(gettersAndSetters.ts, 41, 5))
135+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
136+
137+
get prop() { return x.toUpperCase() },
138+
>prop : Symbol(prop, Decl(gettersAndSetters.ts, 43, 13), Decl(gettersAndSetters.ts, 44, 42))
139+
>x.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
140+
>x : Symbol(x, Decl(gettersAndSetters.ts, 41, 5))
141+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
142+
143+
method() { return x.toUpperCase() }
144+
>method : Symbol(method, Decl(gettersAndSetters.ts, 45, 42))
145+
>x.toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
146+
>x : Symbol(x, Decl(gettersAndSetters.ts, 41, 5))
147+
>toUpperCase : Symbol(String.toUpperCase, Decl(lib.es5.d.ts, --, --))
148+
}
149+
}

tests/baselines/reference/gettersAndSetters.types

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,49 @@ var i:I1 = function (n) {return n;}
134134
>n : number
135135
>n : number
136136

137+
// Repro from #45006
138+
const x: string | number = Math.random() < 0.5 ? "str" : 123;
139+
>x : string | number
140+
>Math.random() < 0.5 ? "str" : 123 : "str" | 123
141+
>Math.random() < 0.5 : boolean
142+
>Math.random() : number
143+
>Math.random : () => number
144+
>Math : Math
145+
>random : () => number
146+
>0.5 : 0.5
147+
>"str" : "str"
148+
>123 : 123
149+
150+
if (typeof x === "string") {
151+
>typeof x === "string" : boolean
152+
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
153+
>x : string | number
154+
>"string" : "string"
155+
156+
let obj = {
157+
>obj : { prop: any; method(): string; }
158+
>{ set prop(_: any) { x.toUpperCase(); }, get prop() { return x.toUpperCase() }, method() { return x.toUpperCase() } } : { prop: any; method(): string; }
159+
160+
set prop(_: any) { x.toUpperCase(); },
161+
>prop : any
162+
>_ : any
163+
>x.toUpperCase() : string
164+
>x.toUpperCase : () => string
165+
>x : string
166+
>toUpperCase : () => string
167+
168+
get prop() { return x.toUpperCase() },
169+
>prop : any
170+
>x.toUpperCase() : string
171+
>x.toUpperCase : () => string
172+
>x : string
173+
>toUpperCase : () => string
174+
175+
method() { return x.toUpperCase() }
176+
>method : () => string
177+
>x.toUpperCase() : string
178+
>x.toUpperCase : () => string
179+
>x : string
180+
>toUpperCase : () => string
181+
}
182+
}

0 commit comments

Comments
 (0)