Skip to content

Commit

Permalink
Gracefully parse 'super' with type arguments (#10677) (#30913)
Browse files Browse the repository at this point in the history
  • Loading branch information
soon authored and RyanCavanaugh committed Apr 15, 2019
1 parent 72f6656 commit 13d9f08
Show file tree
Hide file tree
Showing 23 changed files with 58 additions and 91 deletions.
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -2605,6 +2605,10 @@
"category": "Error",
"code": 2753
},
"'super' may not use type arguments.": {
"category": "Error",
"code": 2754
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/parser.ts
Expand Up @@ -4189,6 +4189,14 @@ namespace ts {

function parseSuperExpression(): MemberExpression {
const expression = parseTokenNode<PrimaryExpression>();
if (token() === SyntaxKind.LessThanToken) {
const startPos = getNodePos();
const typeArguments = tryParse(parseTypeArgumentsInExpression);
if (typeArguments !== undefined) {
parseErrorAt(startPos, getNodePos(), Diagnostics.super_may_not_use_type_arguments);
}
}

if (token() === SyntaxKind.OpenParenToken || token() === SyntaxKind.DotToken || token() === SyntaxKind.OpenBracketToken) {
return expression;
}
Expand Down
11 changes: 4 additions & 7 deletions tests/baselines/reference/errorSuperCalls.errors.txt
Expand Up @@ -7,15 +7,14 @@ tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(26,9): error T
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(30,16): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(34,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(38,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(46,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(46,14): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(46,14): error TS2754: 'super' may not use type arguments.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(58,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(62,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(67,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(71,9): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.


==== tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts (15 errors) ====
==== tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts (14 errors) ====
//super call in class constructor with no base type
class NoBase {
constructor() {
Expand Down Expand Up @@ -80,10 +79,8 @@ tests/cases/conformance/expressions/superCalls/errorSuperCalls.ts(71,9): error T
//super call with type arguments
constructor() {
super<string>();
~~~~~
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
~
!!! error TS1034: 'super' must be followed by an argument list or member access.
~~~~~~~~
!!! error TS2754: 'super' may not use type arguments.
super();
}
}
Expand Down
3 changes: 1 addition & 2 deletions tests/baselines/reference/errorSuperCalls.js
Expand Up @@ -140,8 +140,7 @@ var Derived = /** @class */ (function (_super) {
__extends(Derived, _super);
//super call with type arguments
function Derived() {
var _this = this;
_super.prototype..call(_this);
var _this = _super.call(this) || this;
_this = _super.call(this) || this;
return _this;
}
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/errorSuperCalls.types
Expand Up @@ -91,10 +91,8 @@ class Derived<T> extends Base<T> {
//super call with type arguments
constructor() {
super<string>();
>super<string>() : any
>super : any
>super : Base<T>
> : any
>super<string>() : void
>super : typeof Base

super();
>super() : void
Expand Down
15 changes: 6 additions & 9 deletions tests/baselines/reference/parserSuperExpression2.errors.txt
@@ -1,17 +1,14 @@
tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts(3,5): error TS2335: 'super' can only be referenced in a derived class.
tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts(3,10): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts(3,11): error TS2304: Cannot find name 'T'.
tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts(3,5): error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts(3,10): error TS2754: 'super' may not use type arguments.


==== tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/SuperExpressions/parserSuperExpression2.ts (2 errors) ====
class C {
M() {
super<T>(0);
~~~~~
!!! error TS2335: 'super' can only be referenced in a derived class.
~
!!! error TS1034: 'super' must be followed by an argument list or member access.
~
!!! error TS2304: Cannot find name 'T'.
!!! error TS2337: Super calls are not permitted outside constructors or in nested functions inside constructors.
~~~
!!! error TS2754: 'super' may not use type arguments.
}
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/parserSuperExpression2.js
Expand Up @@ -10,7 +10,7 @@ var C = /** @class */ (function () {
function C() {
}
C.prototype.M = function () {
_super.prototype..call(this, 0);
_this = _super.call(this, 0) || this;
};
return C;
}());
4 changes: 1 addition & 3 deletions tests/baselines/reference/parserSuperExpression2.types
Expand Up @@ -6,10 +6,8 @@ class C {
>M : () => void

super<T>(0);
>super<T>(0) : any
>super<T>(0) : void
>super : any
>super : any
> : any
>0 : 0
}
}
16 changes: 4 additions & 12 deletions tests/baselines/reference/superWithTypeArgument.errors.txt
@@ -1,23 +1,15 @@
tests/cases/compiler/superWithTypeArgument.ts(6,5): error TS2377: Constructors for derived classes must contain a 'super' call.
tests/cases/compiler/superWithTypeArgument.ts(7,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
tests/cases/compiler/superWithTypeArgument.ts(7,14): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/compiler/superWithTypeArgument.ts(7,14): error TS2754: 'super' may not use type arguments.


==== tests/cases/compiler/superWithTypeArgument.ts (3 errors) ====
==== tests/cases/compiler/superWithTypeArgument.ts (1 errors) ====
class C {

}

class D<T> extends C {
constructor() {
~~~~~~~~~~~~~~~
super<T>();
~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
~
!!! error TS1034: 'super' must be followed by an argument list or member access.
~~~
!!! error TS2754: 'super' may not use type arguments.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}
4 changes: 1 addition & 3 deletions tests/baselines/reference/superWithTypeArgument.js
Expand Up @@ -31,9 +31,7 @@ var C = /** @class */ (function () {
var D = /** @class */ (function (_super) {
__extends(D, _super);
function D() {
var _this = this;
_super.prototype..call(_this);
return _this;
return _super.call(this) || this;
}
return D;
}(C));
1 change: 0 additions & 1 deletion tests/baselines/reference/superWithTypeArgument.symbols
Expand Up @@ -12,6 +12,5 @@ class D<T> extends C {
constructor() {
super<T>();
>super : Symbol(C, Decl(superWithTypeArgument.ts, 0, 0))
>T : Symbol(T, Decl(superWithTypeArgument.ts, 4, 8))
}
}
6 changes: 2 additions & 4 deletions tests/baselines/reference/superWithTypeArgument.types
Expand Up @@ -10,9 +10,7 @@ class D<T> extends C {

constructor() {
super<T>();
>super<T>() : any
>super : any
>super : C
> : any
>super<T>() : void
>super : typeof C
}
}
19 changes: 7 additions & 12 deletions tests/baselines/reference/superWithTypeArgument2.errors.txt
@@ -1,23 +1,18 @@
tests/cases/compiler/superWithTypeArgument2.ts(6,5): error TS2377: Constructors for derived classes must contain a 'super' call.
tests/cases/compiler/superWithTypeArgument2.ts(7,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
tests/cases/compiler/superWithTypeArgument2.ts(7,14): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/compiler/superWithTypeArgument2.ts(7,14): error TS2754: 'super' may not use type arguments.
tests/cases/compiler/superWithTypeArgument2.ts(7,18): error TS2554: Expected 0 arguments, but got 1.


==== tests/cases/compiler/superWithTypeArgument2.ts (3 errors) ====
==== tests/cases/compiler/superWithTypeArgument2.ts (2 errors) ====
class C<T> {
foo: T;
}

class D<T> extends C<T> {
constructor(x) {
~~~~~~~~~~~~~~~~
super<T>(x);
~~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
~
!!! error TS1034: 'super' must be followed by an argument list or member access.
~~~
!!! error TS2754: 'super' may not use type arguments.
~
!!! error TS2554: Expected 0 arguments, but got 1.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
}
4 changes: 1 addition & 3 deletions tests/baselines/reference/superWithTypeArgument2.js
Expand Up @@ -31,9 +31,7 @@ var C = /** @class */ (function () {
var D = /** @class */ (function (_super) {
__extends(D, _super);
function D(x) {
var _this = this;
_super.prototype..call(_this, x);
return _this;
return _super.call(this, x) || this;
}
return D;
}(C));
1 change: 0 additions & 1 deletion tests/baselines/reference/superWithTypeArgument2.symbols
Expand Up @@ -19,7 +19,6 @@ class D<T> extends C<T> {

super<T>(x);
>super : Symbol(C, Decl(superWithTypeArgument2.ts, 0, 0))
>T : Symbol(T, Decl(superWithTypeArgument2.ts, 4, 8))
>x : Symbol(x, Decl(superWithTypeArgument2.ts, 5, 16))
}
}
6 changes: 2 additions & 4 deletions tests/baselines/reference/superWithTypeArgument2.types
Expand Up @@ -14,10 +14,8 @@ class D<T> extends C<T> {
>x : any

super<T>(x);
>super<T>(x) : any
>super : any
>super : C<T>
> : any
>super<T>(x) : void
>super : typeof C
>x : any
}
}
16 changes: 4 additions & 12 deletions tests/baselines/reference/superWithTypeArgument3.errors.txt
@@ -1,26 +1,18 @@
tests/cases/compiler/superWithTypeArgument3.ts(7,5): error TS2377: Constructors for derived classes must contain a 'super' call.
tests/cases/compiler/superWithTypeArgument3.ts(8,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
tests/cases/compiler/superWithTypeArgument3.ts(8,14): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/compiler/superWithTypeArgument3.ts(8,14): error TS2754: 'super' may not use type arguments.


==== tests/cases/compiler/superWithTypeArgument3.ts (3 errors) ====
==== tests/cases/compiler/superWithTypeArgument3.ts (1 errors) ====
class C<T> {
foo: T;
bar<U>(x: U) { }
}

class D<T> extends C<T> {
constructor() {
~~~~~~~~~~~~~~~
super<T>();
~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
~
!!! error TS1034: 'super' must be followed by an argument list or member access.
~~~
!!! error TS2754: 'super' may not use type arguments.
}
~~~~~
!!! error TS2377: Constructors for derived classes must contain a 'super' call.
bar() {
super.bar<T>(null);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/baselines/reference/superWithTypeArgument3.js
Expand Up @@ -36,9 +36,7 @@ var C = /** @class */ (function () {
var D = /** @class */ (function (_super) {
__extends(D, _super);
function D() {
var _this = this;
_super.prototype..call(_this);
return _this;
return _super.call(this) || this;
}
D.prototype.bar = function () {
_super.prototype.bar.call(this, null);
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/superWithTypeArgument3.symbols
Expand Up @@ -23,7 +23,6 @@ class D<T> extends C<T> {
constructor() {
super<T>();
>super : Symbol(C, Decl(superWithTypeArgument3.ts, 0, 0))
>T : Symbol(T, Decl(superWithTypeArgument3.ts, 5, 8))
}
bar() {
>bar : Symbol(D.bar, Decl(superWithTypeArgument3.ts, 8, 5))
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/superWithTypeArgument3.types
Expand Up @@ -16,10 +16,8 @@ class D<T> extends C<T> {

constructor() {
super<T>();
>super<T>() : any
>super : any
>super : C<T>
> : any
>super<T>() : void
>super : typeof C
}
bar() {
>bar : () => void
Expand Down
Expand Up @@ -3,10 +3,11 @@ tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(15,11
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(17,30): error TS2345: Argument of type '"hello"' is not assignable to parameter of type 'number'.
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(35,5): error TS2377: Constructors for derived classes must contain a 'super' call.
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(36,9): error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(36,14): error TS1034: 'super' must be followed by an argument list or member access.
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(36,14): error TS2754: 'super' may not use type arguments.
tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(36,34): error TS1034: 'super' must be followed by an argument list or member access.


==== tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts (6 errors) ====
==== tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts (7 errors) ====
export interface SomethingTaggable {
<T>(t: TemplateStringsArray, ...args: T[]): SomethingNewable;
}
Expand Down Expand Up @@ -53,7 +54,9 @@ tests/cases/conformance/es6/templates/taggedTemplatesWithTypeArguments2.ts(36,14
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~
!!! error TS17011: 'super' must be called before accessing a property of 'super' in the constructor of a derived class.
~
~~~~~~~~~~~~~~~~~~~
!!! error TS2754: 'super' may not use type arguments.
~~~~~~~~~~~~~
!!! error TS1034: 'super' must be followed by an argument list or member access.
}
~~~~~
Expand Down
Expand Up @@ -78,6 +78,5 @@ class SomeDerived<T> extends SomeBase<number, string, T> {
constructor() {
super<number, string, T> `hello world`;
>super : Symbol(SomeBase, Decl(taggedTemplatesWithTypeArguments2.ts, 27, 10))
>T : Symbol(T, Decl(taggedTemplatesWithTypeArguments2.ts, 33, 18))
}
}
Expand Up @@ -92,7 +92,7 @@ class SomeDerived<T> extends SomeBase<number, string, T> {
constructor() {
super<number, string, T> `hello world`;
>super<number, string, T> `hello world` : any
>super : any
>super<number, string, T> : any
>super : SomeBase<number, string, T>
> : any
>`hello world` : "hello world"
Expand Down

0 comments on commit 13d9f08

Please sign in to comment.