Skip to content

Commit 842a5b7

Browse files
committed
Merge pull request #3880 from Microsoft/superElementAccess
Allow super element access
2 parents cc3e97a + 8e5f34f commit 842a5b7

23 files changed

+521
-1
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3295,7 +3295,7 @@ namespace ts {
32953295

32963296
function parseSuperExpression(): MemberExpression {
32973297
let expression = parseTokenNode<PrimaryExpression>();
3298-
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.DotToken) {
3298+
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.DotToken || token === SyntaxKind.OpenBracketToken) {
32993299
return expression;
33003300
}
33013301

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [superSymbolIndexedAccess1.ts]
2+
var symbol = Symbol.for('myThing');
3+
4+
class Foo {
5+
[symbol]() {
6+
return 0;
7+
}
8+
}
9+
10+
class Bar extends Foo {
11+
[symbol]() {
12+
return super[symbol]();
13+
}
14+
}
15+
16+
//// [superSymbolIndexedAccess1.js]
17+
var symbol = Symbol.for('myThing');
18+
class Foo {
19+
[symbol]() {
20+
return 0;
21+
}
22+
}
23+
class Bar extends Foo {
24+
[symbol]() {
25+
return super[symbol]();
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess1.ts ===
2+
var symbol = Symbol.for('myThing');
3+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess1.ts, 0, 3))
4+
>Symbol.for : Symbol(SymbolConstructor.for, Decl(lib.d.ts, 1221, 42))
5+
>Symbol : Symbol(Symbol, Decl(lib.d.ts, 1199, 52), Decl(lib.d.ts, 1305, 11))
6+
>for : Symbol(SymbolConstructor.for, Decl(lib.d.ts, 1221, 42))
7+
8+
class Foo {
9+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess1.ts, 0, 35))
10+
11+
[symbol]() {
12+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess1.ts, 0, 3))
13+
14+
return 0;
15+
}
16+
}
17+
18+
class Bar extends Foo {
19+
>Bar : Symbol(Bar, Decl(superSymbolIndexedAccess1.ts, 6, 1))
20+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess1.ts, 0, 35))
21+
22+
[symbol]() {
23+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess1.ts, 0, 3))
24+
25+
return super[symbol]();
26+
>super : Symbol(Foo, Decl(superSymbolIndexedAccess1.ts, 0, 35))
27+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess1.ts, 0, 3))
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess1.ts ===
2+
var symbol = Symbol.for('myThing');
3+
>symbol : symbol
4+
>Symbol.for('myThing') : symbol
5+
>Symbol.for : (key: string) => symbol
6+
>Symbol : SymbolConstructor
7+
>for : (key: string) => symbol
8+
>'myThing' : string
9+
10+
class Foo {
11+
>Foo : Foo
12+
13+
[symbol]() {
14+
>symbol : symbol
15+
16+
return 0;
17+
>0 : number
18+
}
19+
}
20+
21+
class Bar extends Foo {
22+
>Bar : Bar
23+
>Foo : Foo
24+
25+
[symbol]() {
26+
>symbol : symbol
27+
28+
return super[symbol]();
29+
>super[symbol]() : any
30+
>super[symbol] : any
31+
>super : Foo
32+
>symbol : symbol
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [superSymbolIndexedAccess2.ts]
2+
3+
class Foo {
4+
[Symbol.isConcatSpreadable]() {
5+
return 0;
6+
}
7+
}
8+
9+
class Bar extends Foo {
10+
[Symbol.isConcatSpreadable]() {
11+
return super[Symbol.isConcatSpreadable]();
12+
}
13+
}
14+
15+
//// [superSymbolIndexedAccess2.js]
16+
class Foo {
17+
[Symbol.isConcatSpreadable]() {
18+
return 0;
19+
}
20+
}
21+
class Bar extends Foo {
22+
[Symbol.isConcatSpreadable]() {
23+
return super[Symbol.isConcatSpreadable]();
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess2.ts ===
2+
3+
class Foo {
4+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess2.ts, 0, 0))
5+
6+
[Symbol.isConcatSpreadable]() {
7+
>Symbol.isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
8+
>Symbol : Symbol(Symbol, Decl(lib.d.ts, 1199, 52), Decl(lib.d.ts, 1305, 11))
9+
>isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
10+
11+
return 0;
12+
}
13+
}
14+
15+
class Bar extends Foo {
16+
>Bar : Symbol(Bar, Decl(superSymbolIndexedAccess2.ts, 5, 1))
17+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess2.ts, 0, 0))
18+
19+
[Symbol.isConcatSpreadable]() {
20+
>Symbol.isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
21+
>Symbol : Symbol(Symbol, Decl(lib.d.ts, 1199, 52), Decl(lib.d.ts, 1305, 11))
22+
>isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
23+
24+
return super[Symbol.isConcatSpreadable]();
25+
>super : Symbol(Foo, Decl(superSymbolIndexedAccess2.ts, 0, 0))
26+
>Symbol.isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
27+
>Symbol : Symbol(Symbol, Decl(lib.d.ts, 1199, 52), Decl(lib.d.ts, 1305, 11))
28+
>isConcatSpreadable : Symbol(SymbolConstructor.isConcatSpreadable, Decl(lib.d.ts, 1243, 24))
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess2.ts ===
2+
3+
class Foo {
4+
>Foo : Foo
5+
6+
[Symbol.isConcatSpreadable]() {
7+
>Symbol.isConcatSpreadable : symbol
8+
>Symbol : SymbolConstructor
9+
>isConcatSpreadable : symbol
10+
11+
return 0;
12+
>0 : number
13+
}
14+
}
15+
16+
class Bar extends Foo {
17+
>Bar : Bar
18+
>Foo : Foo
19+
20+
[Symbol.isConcatSpreadable]() {
21+
>Symbol.isConcatSpreadable : symbol
22+
>Symbol : SymbolConstructor
23+
>isConcatSpreadable : symbol
24+
25+
return super[Symbol.isConcatSpreadable]();
26+
>super[Symbol.isConcatSpreadable]() : number
27+
>super[Symbol.isConcatSpreadable] : () => number
28+
>super : Foo
29+
>Symbol.isConcatSpreadable : symbol
30+
>Symbol : SymbolConstructor
31+
>isConcatSpreadable : symbol
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess3.ts(11,16): error TS2342: An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.
2+
3+
4+
==== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess3.ts (1 errors) ====
5+
var symbol = Symbol.for('myThing');
6+
7+
class Foo {
8+
[symbol]() {
9+
return 0;
10+
}
11+
}
12+
13+
class Bar extends Foo {
14+
[symbol]() {
15+
return super[Bar]();
16+
~~~~~~~~~~
17+
!!! error TS2342: An index expression argument must be of type 'string', 'number', 'symbol, or 'any'.
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [superSymbolIndexedAccess3.ts]
2+
var symbol = Symbol.for('myThing');
3+
4+
class Foo {
5+
[symbol]() {
6+
return 0;
7+
}
8+
}
9+
10+
class Bar extends Foo {
11+
[symbol]() {
12+
return super[Bar]();
13+
}
14+
}
15+
16+
//// [superSymbolIndexedAccess3.js]
17+
var symbol = Symbol.for('myThing');
18+
class Foo {
19+
[symbol]() {
20+
return 0;
21+
}
22+
}
23+
class Bar extends Foo {
24+
[symbol]() {
25+
return super[Bar]();
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess4.ts(5,16): error TS2335: 'super' can only be referenced in a derived class.
2+
3+
4+
==== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess4.ts (1 errors) ====
5+
var symbol = Symbol.for('myThing');
6+
7+
class Bar {
8+
[symbol]() {
9+
return super[symbol]();
10+
~~~~~
11+
!!! error TS2335: 'super' can only be referenced in a derived class.
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [superSymbolIndexedAccess4.ts]
2+
var symbol = Symbol.for('myThing');
3+
4+
class Bar {
5+
[symbol]() {
6+
return super[symbol]();
7+
}
8+
}
9+
10+
//// [superSymbolIndexedAccess4.js]
11+
var symbol = Symbol.for('myThing');
12+
class Bar {
13+
[symbol]() {
14+
return super[symbol]();
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [superSymbolIndexedAccess5.ts]
2+
var symbol: any;
3+
4+
class Foo {
5+
[symbol]() {
6+
return 0;
7+
}
8+
}
9+
10+
class Bar extends Foo {
11+
[symbol]() {
12+
return super[symbol]();
13+
}
14+
}
15+
16+
//// [superSymbolIndexedAccess5.js]
17+
var __extends = (this && this.__extends) || function (d, b) {
18+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
19+
function __() { this.constructor = d; }
20+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
21+
};
22+
var symbol;
23+
var Foo = (function () {
24+
function Foo() {
25+
}
26+
Foo.prototype[symbol] = function () {
27+
return 0;
28+
};
29+
return Foo;
30+
})();
31+
var Bar = (function (_super) {
32+
__extends(Bar, _super);
33+
function Bar() {
34+
_super.apply(this, arguments);
35+
}
36+
Bar.prototype[symbol] = function () {
37+
return _super.prototype[symbol]();
38+
};
39+
return Bar;
40+
})(Foo);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess5.ts ===
2+
var symbol: any;
3+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess5.ts, 0, 3))
4+
5+
class Foo {
6+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess5.ts, 0, 16))
7+
8+
[symbol]() {
9+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess5.ts, 0, 3))
10+
11+
return 0;
12+
}
13+
}
14+
15+
class Bar extends Foo {
16+
>Bar : Symbol(Bar, Decl(superSymbolIndexedAccess5.ts, 6, 1))
17+
>Foo : Symbol(Foo, Decl(superSymbolIndexedAccess5.ts, 0, 16))
18+
19+
[symbol]() {
20+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess5.ts, 0, 3))
21+
22+
return super[symbol]();
23+
>super : Symbol(Foo, Decl(superSymbolIndexedAccess5.ts, 0, 16))
24+
>symbol : Symbol(symbol, Decl(superSymbolIndexedAccess5.ts, 0, 3))
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/expressions/superPropertyAccess/superSymbolIndexedAccess5.ts ===
2+
var symbol: any;
3+
>symbol : any
4+
5+
class Foo {
6+
>Foo : Foo
7+
8+
[symbol]() {
9+
>symbol : any
10+
11+
return 0;
12+
>0 : number
13+
}
14+
}
15+
16+
class Bar extends Foo {
17+
>Bar : Bar
18+
>Foo : Foo
19+
20+
[symbol]() {
21+
>symbol : any
22+
23+
return super[symbol]();
24+
>super[symbol]() : any
25+
>super[symbol] : any
26+
>super : Foo
27+
>symbol : any
28+
}
29+
}

0 commit comments

Comments
 (0)