Skip to content

Commit 4aa04a9

Browse files
committed
Addressing CR feedback
1 parent ce6b623 commit 4aa04a9

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/compiler/checker.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -2713,12 +2713,12 @@ module ts {
27132713
if (sourceProp === targetProp) {
27142714
return true;
27152715
}
2716-
var sourcePropVisibility = getDeclarationFlagsFromSymbol(sourceProp) & (NodeFlags.Private || NodeFlags.Protected);
2717-
var targetPropVisibility = getDeclarationFlagsFromSymbol(targetProp) & (NodeFlags.Private || NodeFlags.Protected);
2718-
if (sourcePropVisibility !== targetPropVisibility) {
2716+
var sourcePropAccessibility = getDeclarationFlagsFromSymbol(sourceProp) & (NodeFlags.Private | NodeFlags.Protected);
2717+
var targetPropAccessibility = getDeclarationFlagsFromSymbol(targetProp) & (NodeFlags.Private | NodeFlags.Protected);
2718+
if (sourcePropAccessibility !== targetPropAccessibility) {
27192719
return false;
27202720
}
2721-
if (sourcePropVisibility) {
2721+
if (sourcePropAccessibility) {
27222722
return getTargetSymbol(sourceProp) === getTargetSymbol(targetProp) && relate(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors);
27232723
}
27242724
else {
@@ -4011,26 +4011,35 @@ module ts {
40114011

40124012
function isClassPropertyAccessible(node: PropertyAccess, type: Type, prop: Symbol): boolean {
40134013
var flags = getDeclarationFlagsFromSymbol(prop);
4014+
// Public properties are always accessible
40144015
if (!(flags & (NodeFlags.Private | NodeFlags.Protected))) {
40154016
return true;
40164017
}
4018+
// Property is known to be private or protected at this point
4019+
// Private and protected properties are never accessible outside a class declaration
40174020
var enclosingClassDeclaration = getAncestor(node, SyntaxKind.ClassDeclaration);
40184021
if (!enclosingClassDeclaration) {
40194022
return false;
40204023
}
4024+
// Get the declaring and enclosing class instance types
40214025
var declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(prop.parent);
40224026
var enclosingClass = <InterfaceType>getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingClassDeclaration));
4027+
// Private property is accessible if declaring and enclosing class are the same
40234028
if (flags & NodeFlags.Private) {
40244029
return declaringClass === enclosingClass;
40254030
}
4031+
// Property is known to be protected at this point
4032+
// All protected properties of a supertype are accessible in a super access
40264033
if (node.left.kind === SyntaxKind.SuperKeyword) {
40274034
return true;
40284035
}
4036+
// An instance property must be accessed through an instance of the enclosing class
40294037
if (!(flags & NodeFlags.Static)) {
40304038
if (!(getTargetType(type).flags & (TypeFlags.Class | TypeFlags.Interface) && hasBaseType(<InterfaceType>type, enclosingClass))) {
40314039
return false;
40324040
}
40334041
}
4042+
// A protected property is accessible in the declaring class and classes derived from it
40344043
return hasBaseType(enclosingClass, declaringClass);
40354044
}
40364045

src/compiler/diagnosticMessages.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1012,18 +1012,18 @@
10121012
"category": "Error",
10131013
"code": 2441
10141014
},
1015-
"Types have separate declarations of a private property '{0}'.": {
1015+
"Types have separate declarations of a private property '{0}'.": {
10161016
"category": "Error",
10171017
"code": 2442
1018-
},
1019-
"Property '{0}' is protected but type '{1}' is not derived from type '{2}'.": {
1018+
},
1019+
"Property '{0}' is protected but type '{1}' is not a class derived from type '{2}'.": {
10201020
"category": "Error",
10211021
"code": 2443
1022-
},
1023-
"Property '{0}' is protected in type '{1}' but public in type '{2}'.": {
1022+
},
1023+
"Property '{0}' is protected in type '{1}' but public in type '{2}'.": {
10241024
"category": "Error",
10251025
"code": 2444
1026-
},
1026+
},
10271027

10281028
"Import declaration '{0}' is using private name '{1}'.": {
10291029
"category": "Error",

0 commit comments

Comments
 (0)