Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f79fca7

Browse filesBrowse files
authored
Merge pull request microsoft#12675 from Microsoft/subsubclass-can-access-protected-constructor
Subsubclass can access protected constructor
2 parents 2166364 + b321d50 commit f79fca7
Copy full SHA for f79fca7

5 files changed

+155-2Lines changed: 155 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/checker.ts‎

Copy file name to clipboardExpand all lines: src/compiler/checker.ts
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13475,13 +13475,14 @@ namespace ts {
1347513475
const containingClass = getContainingClass(node);
1347613476
if (containingClass) {
1347713477
const containingType = getTypeOfNode(containingClass);
13478-
const baseTypes = getBaseTypes(<InterfaceType>containingType);
13479-
if (baseTypes.length) {
13478+
let baseTypes = getBaseTypes(containingType as InterfaceType);
13479+
while (baseTypes.length) {
1348013480
const baseType = baseTypes[0];
1348113481
if (modifiers & ModifierFlags.Protected &&
1348213482
baseType.symbol === declaration.parent.symbol) {
1348313483
return true;
1348413484
}
13485+
baseTypes = getBaseTypes(baseType as InterfaceType);
1348513486
}
1348613487
}
1348713488
if (modifiers & ModifierFlags.Private) {
Collapse file
+51Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [subSubClassCanAccessProtectedConstructor.ts]
2+
class Base {
3+
protected constructor() { }
4+
public instance1 = new Base(); // allowed
5+
}
6+
7+
class Subclass extends Base {
8+
public instance1_1 = new Base(); // allowed
9+
public instance1_2 = new Subclass(); // allowed
10+
}
11+
12+
class SubclassOfSubclass extends Subclass {
13+
public instance2_1 = new Base(); // allowed
14+
public instance2_2 = new Subclass(); // allowed
15+
public instance2_3 = new SubclassOfSubclass(); // allowed
16+
}
17+
18+
19+
//// [subSubClassCanAccessProtectedConstructor.js]
20+
var __extends = (this && this.__extends) || function (d, b) {
21+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
22+
function __() { this.constructor = d; }
23+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
24+
};
25+
var Base = (function () {
26+
function Base() {
27+
this.instance1 = new Base(); // allowed
28+
}
29+
return Base;
30+
}());
31+
var Subclass = (function (_super) {
32+
__extends(Subclass, _super);
33+
function Subclass() {
34+
var _this = _super.apply(this, arguments) || this;
35+
_this.instance1_1 = new Base(); // allowed
36+
_this.instance1_2 = new Subclass(); // allowed
37+
return _this;
38+
}
39+
return Subclass;
40+
}(Base));
41+
var SubclassOfSubclass = (function (_super) {
42+
__extends(SubclassOfSubclass, _super);
43+
function SubclassOfSubclass() {
44+
var _this = _super.apply(this, arguments) || this;
45+
_this.instance2_1 = new Base(); // allowed
46+
_this.instance2_2 = new Subclass(); // allowed
47+
_this.instance2_3 = new SubclassOfSubclass(); // allowed
48+
return _this;
49+
}
50+
return SubclassOfSubclass;
51+
}(Subclass));
Collapse file
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=== tests/cases/compiler/subSubClassCanAccessProtectedConstructor.ts ===
2+
class Base {
3+
>Base : Symbol(Base, Decl(subSubClassCanAccessProtectedConstructor.ts, 0, 0))
4+
5+
protected constructor() { }
6+
public instance1 = new Base(); // allowed
7+
>instance1 : Symbol(Base.instance1, Decl(subSubClassCanAccessProtectedConstructor.ts, 1, 31))
8+
>Base : Symbol(Base, Decl(subSubClassCanAccessProtectedConstructor.ts, 0, 0))
9+
}
10+
11+
class Subclass extends Base {
12+
>Subclass : Symbol(Subclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 3, 1))
13+
>Base : Symbol(Base, Decl(subSubClassCanAccessProtectedConstructor.ts, 0, 0))
14+
15+
public instance1_1 = new Base(); // allowed
16+
>instance1_1 : Symbol(Subclass.instance1_1, Decl(subSubClassCanAccessProtectedConstructor.ts, 5, 29))
17+
>Base : Symbol(Base, Decl(subSubClassCanAccessProtectedConstructor.ts, 0, 0))
18+
19+
public instance1_2 = new Subclass(); // allowed
20+
>instance1_2 : Symbol(Subclass.instance1_2, Decl(subSubClassCanAccessProtectedConstructor.ts, 6, 36))
21+
>Subclass : Symbol(Subclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 3, 1))
22+
}
23+
24+
class SubclassOfSubclass extends Subclass {
25+
>SubclassOfSubclass : Symbol(SubclassOfSubclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 8, 1))
26+
>Subclass : Symbol(Subclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 3, 1))
27+
28+
public instance2_1 = new Base(); // allowed
29+
>instance2_1 : Symbol(SubclassOfSubclass.instance2_1, Decl(subSubClassCanAccessProtectedConstructor.ts, 10, 43))
30+
>Base : Symbol(Base, Decl(subSubClassCanAccessProtectedConstructor.ts, 0, 0))
31+
32+
public instance2_2 = new Subclass(); // allowed
33+
>instance2_2 : Symbol(SubclassOfSubclass.instance2_2, Decl(subSubClassCanAccessProtectedConstructor.ts, 11, 36))
34+
>Subclass : Symbol(Subclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 3, 1))
35+
36+
public instance2_3 = new SubclassOfSubclass(); // allowed
37+
>instance2_3 : Symbol(SubclassOfSubclass.instance2_3, Decl(subSubClassCanAccessProtectedConstructor.ts, 12, 40))
38+
>SubclassOfSubclass : Symbol(SubclassOfSubclass, Decl(subSubClassCanAccessProtectedConstructor.ts, 8, 1))
39+
}
40+
Collapse file
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
=== tests/cases/compiler/subSubClassCanAccessProtectedConstructor.ts ===
2+
class Base {
3+
>Base : Base
4+
5+
protected constructor() { }
6+
public instance1 = new Base(); // allowed
7+
>instance1 : Base
8+
>new Base() : Base
9+
>Base : typeof Base
10+
}
11+
12+
class Subclass extends Base {
13+
>Subclass : Subclass
14+
>Base : Base
15+
16+
public instance1_1 = new Base(); // allowed
17+
>instance1_1 : Base
18+
>new Base() : Base
19+
>Base : typeof Base
20+
21+
public instance1_2 = new Subclass(); // allowed
22+
>instance1_2 : Subclass
23+
>new Subclass() : Subclass
24+
>Subclass : typeof Subclass
25+
}
26+
27+
class SubclassOfSubclass extends Subclass {
28+
>SubclassOfSubclass : SubclassOfSubclass
29+
>Subclass : Subclass
30+
31+
public instance2_1 = new Base(); // allowed
32+
>instance2_1 : Base
33+
>new Base() : Base
34+
>Base : typeof Base
35+
36+
public instance2_2 = new Subclass(); // allowed
37+
>instance2_2 : Subclass
38+
>new Subclass() : Subclass
39+
>Subclass : typeof Subclass
40+
41+
public instance2_3 = new SubclassOfSubclass(); // allowed
42+
>instance2_3 : SubclassOfSubclass
43+
>new SubclassOfSubclass() : SubclassOfSubclass
44+
>SubclassOfSubclass : typeof SubclassOfSubclass
45+
}
46+
Collapse file
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Base {
2+
protected constructor() { }
3+
public instance1 = new Base(); // allowed
4+
}
5+
6+
class Subclass extends Base {
7+
public instance1_1 = new Base(); // allowed
8+
public instance1_2 = new Subclass(); // allowed
9+
}
10+
11+
class SubclassOfSubclass extends Subclass {
12+
public instance2_1 = new Base(); // allowed
13+
public instance2_2 = new Subclass(); // allowed
14+
public instance2_3 = new SubclassOfSubclass(); // allowed
15+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.