Skip to content

Commit f51de5b

Browse files
committed
Merge pull request #5860 from Microsoft/superPropertiesInES6
do not error on 'super' property access in ES6
2 parents 8c6105f + c7a65b5 commit f51de5b

8 files changed

+406
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8386,7 +8386,7 @@ namespace ts {
83868386
// - In a static member function or static member accessor
83878387
// where this references the constructor function object of a derived class,
83888388
// a super property access is permitted and must specify a public static member function of the base class.
8389-
if (getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) {
8389+
if (languageVersion < ScriptTarget.ES6 && getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) {
83908390
// `prop` refers to a *property* declared in the super class
83918391
// rather than a *method*, so it does not satisfy the above criteria.
83928392

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
tests/cases/compiler/superPropertyAccess_ES5.ts(12,22): error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.
2+
tests/cases/compiler/superPropertyAccess_ES5.ts(27,15): error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.
3+
4+
5+
==== tests/cases/compiler/superPropertyAccess_ES5.ts (2 errors) ====
6+
7+
class MyBase {
8+
getValue(): number { return 1; }
9+
get value(): number { return 1; }
10+
}
11+
12+
class MyDerived extends MyBase {
13+
constructor() {
14+
super();
15+
16+
const f1 = super.getValue();
17+
const f2 = super.value;
18+
~~~~~
19+
!!! error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.
20+
}
21+
}
22+
23+
var d = new MyDerived();
24+
var f3 = d.value;
25+
26+
class A {
27+
private _property: string;
28+
get property() { return this._property; }
29+
set property(value: string) { this._property = value }
30+
}
31+
32+
class B extends A {
33+
set property(value: string) {
34+
super.property = value + " addition";
35+
~~~~~~~~
36+
!!! error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.
37+
}
38+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//// [superPropertyAccess_ES5.ts]
2+
3+
class MyBase {
4+
getValue(): number { return 1; }
5+
get value(): number { return 1; }
6+
}
7+
8+
class MyDerived extends MyBase {
9+
constructor() {
10+
super();
11+
12+
const f1 = super.getValue();
13+
const f2 = super.value;
14+
}
15+
}
16+
17+
var d = new MyDerived();
18+
var f3 = d.value;
19+
20+
class A {
21+
private _property: string;
22+
get property() { return this._property; }
23+
set property(value: string) { this._property = value }
24+
}
25+
26+
class B extends A {
27+
set property(value: string) {
28+
super.property = value + " addition";
29+
}
30+
}
31+
32+
//// [superPropertyAccess_ES5.js]
33+
var __extends = (this && this.__extends) || function (d, b) {
34+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
35+
function __() { this.constructor = d; }
36+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37+
};
38+
var MyBase = (function () {
39+
function MyBase() {
40+
}
41+
MyBase.prototype.getValue = function () { return 1; };
42+
Object.defineProperty(MyBase.prototype, "value", {
43+
get: function () { return 1; },
44+
enumerable: true,
45+
configurable: true
46+
});
47+
return MyBase;
48+
})();
49+
var MyDerived = (function (_super) {
50+
__extends(MyDerived, _super);
51+
function MyDerived() {
52+
_super.call(this);
53+
var f1 = _super.prototype.getValue.call(this);
54+
var f2 = _super.prototype.value;
55+
}
56+
return MyDerived;
57+
})(MyBase);
58+
var d = new MyDerived();
59+
var f3 = d.value;
60+
var A = (function () {
61+
function A() {
62+
}
63+
Object.defineProperty(A.prototype, "property", {
64+
get: function () { return this._property; },
65+
set: function (value) { this._property = value; },
66+
enumerable: true,
67+
configurable: true
68+
});
69+
return A;
70+
})();
71+
var B = (function (_super) {
72+
__extends(B, _super);
73+
function B() {
74+
_super.apply(this, arguments);
75+
}
76+
Object.defineProperty(B.prototype, "property", {
77+
set: function (value) {
78+
_super.prototype.property = value + " addition";
79+
},
80+
enumerable: true,
81+
configurable: true
82+
});
83+
return B;
84+
})(A);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//// [superPropertyAccess_ES6.ts]
2+
3+
class MyBase {
4+
getValue(): number { return 1; }
5+
get value(): number { return 1; }
6+
}
7+
8+
class MyDerived extends MyBase {
9+
constructor() {
10+
super();
11+
12+
const f1 = super.getValue();
13+
const f2 = super.value;
14+
}
15+
}
16+
17+
var d = new MyDerived();
18+
var f3 = d.value;
19+
20+
class A {
21+
private _property: string;
22+
get property() { return this._property; }
23+
set property(value: string) { this._property = value }
24+
}
25+
26+
class B extends A {
27+
set property(value: string) {
28+
super.property = value + " addition";
29+
}
30+
}
31+
32+
//// [superPropertyAccess_ES6.js]
33+
class MyBase {
34+
getValue() { return 1; }
35+
get value() { return 1; }
36+
}
37+
class MyDerived extends MyBase {
38+
constructor() {
39+
super();
40+
const f1 = super.getValue();
41+
const f2 = super.value;
42+
}
43+
}
44+
var d = new MyDerived();
45+
var f3 = d.value;
46+
class A {
47+
get property() { return this._property; }
48+
set property(value) { this._property = value; }
49+
}
50+
class B extends A {
51+
set property(value) {
52+
super.property = value + " addition";
53+
}
54+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
=== tests/cases/compiler/superPropertyAccess_ES6.ts ===
2+
3+
class MyBase {
4+
>MyBase : Symbol(MyBase, Decl(superPropertyAccess_ES6.ts, 0, 0))
5+
6+
getValue(): number { return 1; }
7+
>getValue : Symbol(getValue, Decl(superPropertyAccess_ES6.ts, 1, 14))
8+
9+
get value(): number { return 1; }
10+
>value : Symbol(value, Decl(superPropertyAccess_ES6.ts, 2, 34))
11+
}
12+
13+
class MyDerived extends MyBase {
14+
>MyDerived : Symbol(MyDerived, Decl(superPropertyAccess_ES6.ts, 4, 1))
15+
>MyBase : Symbol(MyBase, Decl(superPropertyAccess_ES6.ts, 0, 0))
16+
17+
constructor() {
18+
super();
19+
>super : Symbol(MyBase, Decl(superPropertyAccess_ES6.ts, 0, 0))
20+
21+
const f1 = super.getValue();
22+
>f1 : Symbol(f1, Decl(superPropertyAccess_ES6.ts, 10, 9))
23+
>super.getValue : Symbol(MyBase.getValue, Decl(superPropertyAccess_ES6.ts, 1, 14))
24+
>super : Symbol(MyBase, Decl(superPropertyAccess_ES6.ts, 0, 0))
25+
>getValue : Symbol(MyBase.getValue, Decl(superPropertyAccess_ES6.ts, 1, 14))
26+
27+
const f2 = super.value;
28+
>f2 : Symbol(f2, Decl(superPropertyAccess_ES6.ts, 11, 9))
29+
>super.value : Symbol(MyBase.value, Decl(superPropertyAccess_ES6.ts, 2, 34))
30+
>super : Symbol(MyBase, Decl(superPropertyAccess_ES6.ts, 0, 0))
31+
>value : Symbol(MyBase.value, Decl(superPropertyAccess_ES6.ts, 2, 34))
32+
}
33+
}
34+
35+
var d = new MyDerived();
36+
>d : Symbol(d, Decl(superPropertyAccess_ES6.ts, 15, 3))
37+
>MyDerived : Symbol(MyDerived, Decl(superPropertyAccess_ES6.ts, 4, 1))
38+
39+
var f3 = d.value;
40+
>f3 : Symbol(f3, Decl(superPropertyAccess_ES6.ts, 16, 3))
41+
>d.value : Symbol(MyBase.value, Decl(superPropertyAccess_ES6.ts, 2, 34))
42+
>d : Symbol(d, Decl(superPropertyAccess_ES6.ts, 15, 3))
43+
>value : Symbol(MyBase.value, Decl(superPropertyAccess_ES6.ts, 2, 34))
44+
45+
class A {
46+
>A : Symbol(A, Decl(superPropertyAccess_ES6.ts, 16, 17))
47+
48+
private _property: string;
49+
>_property : Symbol(_property, Decl(superPropertyAccess_ES6.ts, 18, 9))
50+
51+
get property() { return this._property; }
52+
>property : Symbol(property, Decl(superPropertyAccess_ES6.ts, 19, 30), Decl(superPropertyAccess_ES6.ts, 20, 45))
53+
>this._property : Symbol(_property, Decl(superPropertyAccess_ES6.ts, 18, 9))
54+
>this : Symbol(A, Decl(superPropertyAccess_ES6.ts, 16, 17))
55+
>_property : Symbol(_property, Decl(superPropertyAccess_ES6.ts, 18, 9))
56+
57+
set property(value: string) { this._property = value }
58+
>property : Symbol(property, Decl(superPropertyAccess_ES6.ts, 19, 30), Decl(superPropertyAccess_ES6.ts, 20, 45))
59+
>value : Symbol(value, Decl(superPropertyAccess_ES6.ts, 21, 17))
60+
>this._property : Symbol(_property, Decl(superPropertyAccess_ES6.ts, 18, 9))
61+
>this : Symbol(A, Decl(superPropertyAccess_ES6.ts, 16, 17))
62+
>_property : Symbol(_property, Decl(superPropertyAccess_ES6.ts, 18, 9))
63+
>value : Symbol(value, Decl(superPropertyAccess_ES6.ts, 21, 17))
64+
}
65+
66+
class B extends A {
67+
>B : Symbol(B, Decl(superPropertyAccess_ES6.ts, 22, 1))
68+
>A : Symbol(A, Decl(superPropertyAccess_ES6.ts, 16, 17))
69+
70+
set property(value: string) {
71+
>property : Symbol(property, Decl(superPropertyAccess_ES6.ts, 24, 19))
72+
>value : Symbol(value, Decl(superPropertyAccess_ES6.ts, 25, 17))
73+
74+
super.property = value + " addition";
75+
>super.property : Symbol(A.property, Decl(superPropertyAccess_ES6.ts, 19, 30), Decl(superPropertyAccess_ES6.ts, 20, 45))
76+
>super : Symbol(A, Decl(superPropertyAccess_ES6.ts, 16, 17))
77+
>property : Symbol(A.property, Decl(superPropertyAccess_ES6.ts, 19, 30), Decl(superPropertyAccess_ES6.ts, 20, 45))
78+
>value : Symbol(value, Decl(superPropertyAccess_ES6.ts, 25, 17))
79+
}
80+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=== tests/cases/compiler/superPropertyAccess_ES6.ts ===
2+
3+
class MyBase {
4+
>MyBase : MyBase
5+
6+
getValue(): number { return 1; }
7+
>getValue : () => number
8+
>1 : number
9+
10+
get value(): number { return 1; }
11+
>value : number
12+
>1 : number
13+
}
14+
15+
class MyDerived extends MyBase {
16+
>MyDerived : MyDerived
17+
>MyBase : MyBase
18+
19+
constructor() {
20+
super();
21+
>super() : void
22+
>super : typeof MyBase
23+
24+
const f1 = super.getValue();
25+
>f1 : number
26+
>super.getValue() : number
27+
>super.getValue : () => number
28+
>super : MyBase
29+
>getValue : () => number
30+
31+
const f2 = super.value;
32+
>f2 : number
33+
>super.value : number
34+
>super : MyBase
35+
>value : number
36+
}
37+
}
38+
39+
var d = new MyDerived();
40+
>d : MyDerived
41+
>new MyDerived() : MyDerived
42+
>MyDerived : typeof MyDerived
43+
44+
var f3 = d.value;
45+
>f3 : number
46+
>d.value : number
47+
>d : MyDerived
48+
>value : number
49+
50+
class A {
51+
>A : A
52+
53+
private _property: string;
54+
>_property : string
55+
56+
get property() { return this._property; }
57+
>property : string
58+
>this._property : string
59+
>this : this
60+
>_property : string
61+
62+
set property(value: string) { this._property = value }
63+
>property : string
64+
>value : string
65+
>this._property = value : string
66+
>this._property : string
67+
>this : this
68+
>_property : string
69+
>value : string
70+
}
71+
72+
class B extends A {
73+
>B : B
74+
>A : A
75+
76+
set property(value: string) {
77+
>property : string
78+
>value : string
79+
80+
super.property = value + " addition";
81+
>super.property = value + " addition" : string
82+
>super.property : string
83+
>super : A
84+
>property : string
85+
>value + " addition" : string
86+
>value : string
87+
>" addition" : string
88+
}
89+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @target: ES5
2+
3+
class MyBase {
4+
getValue(): number { return 1; }
5+
get value(): number { return 1; }
6+
}
7+
8+
class MyDerived extends MyBase {
9+
constructor() {
10+
super();
11+
12+
const f1 = super.getValue();
13+
const f2 = super.value;
14+
}
15+
}
16+
17+
var d = new MyDerived();
18+
var f3 = d.value;
19+
20+
class A {
21+
private _property: string;
22+
get property() { return this._property; }
23+
set property(value: string) { this._property = value }
24+
}
25+
26+
class B extends A {
27+
set property(value: string) {
28+
super.property = value + " addition";
29+
}
30+
}

0 commit comments

Comments
 (0)