TypeScript/tests/baselines/reference/gettersAndSetters.js
David Rogers d18e82b380
fix for 45006 (#45020)
* fix for 45006

* treat setters like getters in preceding commit; move test accordingly

* fix test baselines

* changes per code review

in `getContainerFlags`, move cases for get/set accessors
to fallthrough into the block that currently handles MethodDeclaration;
so get/set accessors and method declarations all get the same container flags,
such that during `bindContainer`, `startFlow.node` is assigned to
getters/accessors
(this changes a public api in tsserverlibrary.d.ts and typescript.d.ts
by adding `GetAccessorDeclaration` and `SetAccessorDeclaration` to the type
of `FlowStart.node`)

consolidate predicates checking whether a node is either a get or set
accessor, into `isObjectLiteralOrClassExpressionMethodOrAccessor`
(formerly `isObjectLiteralOrClassExpressionMethod`)

annotate updated test with `@target: es2020`

* fix `isObjectLiteralOrClassExpressionMethodOrAccessor`

require that Getter/Setters are parented by an ObjectLiteralExpression or ClassExpression
2021-08-20 17:05:19 -07:00

89 lines
2.1 KiB
TypeScript

//// [gettersAndSetters.ts]
// classes
class C {
public fooBack = "";
static barBack:string = "";
public bazBack = "";
public get Foo() { return this.fooBack;} // ok
public set Foo(foo:string) {this.fooBack = foo;} // ok
static get Bar() {return C.barBack;} // ok
static set Bar(bar:string) {C.barBack = bar;} // ok
public get = function() {} // ok
public set = function() {} // ok
}
var c = new C();
var foo = c.Foo;
c.Foo = "foov";
var bar = C.Bar;
C.Bar = "barv";
var baz = c.Baz;
c.Baz = "bazv";
// The Foo accessors' return and param types should be contextually typed to the Foo field
var o : {Foo:number;} = {get Foo() {return 0;}, set Foo(val:number){val}}; // o
var ofg = o.Foo;
o.Foo = 0;
interface I1 {
(n:number):number;
}
var i:I1 = function (n) {return n;}
// Repro from #45006
const x: string | number = Math.random() < 0.5 ? "str" : 123;
if (typeof x === "string") {
let obj = {
set prop(_: any) { x.toUpperCase(); },
get prop() { return x.toUpperCase() },
method() { return x.toUpperCase() }
}
}
//// [gettersAndSetters.js]
// classes
class C {
constructor() {
this.fooBack = "";
this.bazBack = "";
this.get = function () { }; // ok
this.set = function () { }; // ok
}
get Foo() { return this.fooBack; } // ok
set Foo(foo) { this.fooBack = foo; } // ok
static get Bar() { return C.barBack; } // ok
static set Bar(bar) { C.barBack = bar; } // ok
}
C.barBack = "";
var c = new C();
var foo = c.Foo;
c.Foo = "foov";
var bar = C.Bar;
C.Bar = "barv";
var baz = c.Baz;
c.Baz = "bazv";
// The Foo accessors' return and param types should be contextually typed to the Foo field
var o = { get Foo() { return 0; }, set Foo(val) { val; } }; // o
var ofg = o.Foo;
o.Foo = 0;
var i = function (n) { return n; };
// Repro from #45006
const x = Math.random() < 0.5 ? "str" : 123;
if (typeof x === "string") {
let obj = {
set prop(_) { x.toUpperCase(); },
get prop() { return x.toUpperCase(); },
method() { return x.toUpperCase(); }
};
}