8000 Merge pull request #3897 from Microsoft/errorForUseSuperInNullExtension · rvdn/TypeScript@6af52ee · GitHub
[go: up one dir, main page]

Skip t 8000 o content

Commit 6af52ee

Browse files
committed
Merge pull request microsoft#3897 from Microsoft/errorForUseSuperInNullExtension
Error for use super in null extension
2 parents 266e5e6 + 5e60781 commit 6af52ee

File tree

7 files changed

+86
-5
lines changed

7 files changed

+86
-5
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10431,9 +10431,17 @@ namespace ts {
1043110431
// TS 1.0 spec (April 2014): 8.3.2
1043210432
// Constructors of classes with no extends clause may not contain super calls, whereas
1043310433
// constructors of derived classes must contain at least one super call somewhere in their function body.
10434-
if (getClassExtendsHeritageClauseElement(<ClassDeclaration>node.parent)) {
10434+
let containingClassDecl = <ClassDeclaration>node.parent;
10435+
if (getClassExtendsHeritageClauseElement(containingClassDecl)) {
10436+
let containingClassSymbol = getSymbolOfNode(containingClassDecl);
10437+
let containingClassInstanceType = <InterfaceType>getDeclaredTypeOfSymbol(containingClassSymbol);
10438+
let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType);
1043510439

1043610440
if (containsSuperCall(node.body)) {
10441+
if (baseConstructorType === nullType) {
10442+
error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null);
10443+
}
10444+
1043710445
// The first statement in the body of a constructor (excluding prologue directives) must be a super call
1043810446
// if both of the following are true:
1043910447
// - The containing class is a derived class.
@@ -10466,7 +10474,7 @@ namespace ts {
1046610474
}
1046710475
}
1046810476
}
10469-
else {
10477+
else if (baseConstructorType !== nullType) {
1047010478
error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call);
1047110479
}
1047210480
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,5 +614,6 @@ namespace ts {
614614
Expected_corresponding_JSX_closing_tag_for_0: { code: 17002, category: DiagnosticCategory.Error, key: "Expected corresponding JSX closing tag for '{0}'." },
615615
JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." },
616616
Cannot_use_JSX_unless_the_jsx_flag_is_provided: { code: 17004, category: DiagnosticCategory.Error, key: "Cannot use JSX unless the '--jsx' flag is provided." },
617+
A_constructor_cannot_contain_a_super_call_when_its_class_extends_null: { code: 17005, category: DiagnosticCategory.Error, key: "A constructor cannot contain a 'super' call when its class extends 'null'" },
617618
};
618619
}

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"Unterminated string literal.": {
33
"category": "Error",
44
"code": 1002
@@ -2449,5 +2449,9 @@
24492449
"Cannot use JSX unless the '--jsx' flag is provided.": {
24502450
"category": "Error",
24512451
"code": 17004
2452+
},
2453+
"A constructor cannot contain a 'super' call when its class extends 'null'": {
2454+
"category": "Error",
2455+
"code": 17005
24522456
}
24532457
}

src/compiler/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,10 +1777,10 @@ namespace ts {
17771777
StringLike = String | StringLiteral,
17781778
NumberLike = Number | Enum,
17791779
ObjectType = Class | Interface | Reference | Tuple | Anonymous,
1780-
UnionOrIntersection = Union | Intersection,
1780+
UnionOrIntersection = Union | Intersection,
17811781
StructuredType = ObjectType | Union | Intersection,
17821782
/* @internal */
1783-
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
1783+
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral
17841784
}
17851785

17861786
// Properties common to all types
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/classExtendsNull.ts(2,5): error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
2+
3+
4+
==== tests/cases/compiler/classExtendsNull.ts (1 errors) ====
5+
class C extends null {
6+
constructor() {
7+
~~~~~~~~~~~~~~~
8+
super();
9+
~~~~~~~~~~~~~~~~
10+
return Object.create(null);
11+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12+
}
13+
~~~~~
14+
!!! error TS17005: A constructor cannot contain a 'super' call when its class extends 'null'
15+
}
16+
17+
class D extends null {
18+
constructor() {
19+
return Object.create(null);
20+
}
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [classExtendsNull.ts]
2+
class C extends null {
3+
constructor() {
4+
super();
5+
return Object.create(null);
6+
}
7+
}
8+
9+
class D extends null {
10+
constructor() {
11+
return Object.create(null);
12+
}
13+
}
14+
15+
//// [classExtendsNull.js]
16+
var __extends = (this && this.__extends) || function (d, b) {
17+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
18+
function __() { this.constructor = d; }
19+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
20+
};
21+
var C = (function (_super) {
22+
__extends(C, _super);
23+
function C() {
24+
_super.call(this);
25+
return Object.create(null);
26+
}
27+
return C;
28+
})(null);
29+
var D = (function (_super) {
30+
__extends(D, _super);
31+
function D() {
32+
return Object.create(null);
33+
}
34+
return D;
35+
})(null);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class C extends null {
2+
constructor() {
3+
super();
4+
return Object.create(null);
5+
}
6+
}
7+
8+
class D extends null {
9+
constructor() {
10+
return Object.create(null);
11+
}
12+
}

0 commit comments

Comments
 (0)
0