mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #7262 from Microsoft/stop-destructuring-private-properties
Stop destructuring private properties
This commit is contained in:
commit
5e53ba06cf
38 files changed
+366
-239
No files matched your search
+16
-9
@@ -8971,15 +8971,13 @@ namespace ts {
|
||||
* @param type The type of left.
|
||||
* @param prop The symbol for the right hand side of the property access.
|
||||
*/
|
||||
function checkClassPropertyAccess(node: PropertyAccessExpression | QualifiedName, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean {
|
||||
function checkClassPropertyAccess(node: PropertyAccessExpression | QualifiedName | VariableLikeDeclaration, left: Expression | QualifiedName, type: Type, prop: Symbol): boolean {
|
||||
const flags = getDeclarationFlagsFromSymbol(prop);
|
||||
const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(getParentOfSymbol(prop));
|
||||
|
||||
const errorNode = node.kind === SyntaxKind.PropertyAccessExpression || node.kind === SyntaxKind.VariableDeclaration ?
|
||||
(<PropertyAccessExpression | VariableDeclaration>node).name :
|
||||
(<QualifiedName>node).right;
|
||||
if (left.kind === SyntaxKind.SuperKeyword) {
|
||||
const errorNode = node.kind === SyntaxKind.PropertyAccessExpression ?
|
||||
(<PropertyAccessExpression>node).name :
|
||||
(<QualifiedName>node).right;
|
||||
|
||||
// TS 1.0 spec (April 2014): 4.8.2
|
||||
// - In a constructor, instance member function, instance member accessor, or
|
||||
// instance member variable initializer where this references a derived class instance,
|
||||
@@ -9020,7 +9018,7 @@ namespace ts {
|
||||
// Private property is accessible if declaring and enclosing class are the same
|
||||
if (flags & NodeFlags.Private) {
|
||||
if (declaringClass !== enclosingClass) {
|
||||
error(node, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass));
|
||||
error(errorNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -9034,7 +9032,7 @@ namespace ts {
|
||||
}
|
||||
// A protected property is accessible in the declaring class and classes derived from it
|
||||
if (!enclosingClass || !hasBaseType(enclosingClass, declaringClass)) {
|
||||
error(node, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass));
|
||||
error(errorNode, Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass));
|
||||
return false;
|
||||
}
|
||||
// No further restrictions for static properties
|
||||
@@ -9049,7 +9047,7 @@ namespace ts {
|
||||
|
||||
// TODO: why is the first part of this check here?
|
||||
if (!(getTargetType(type).flags & (TypeFlags.Class | TypeFlags.Interface) && hasBaseType(<InterfaceType>type, enclosingClass))) {
|
||||
error(node, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass));
|
||||
error(errorNode, Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -13310,6 +13308,15 @@ namespace ts {
|
||||
if (node.propertyName && node.propertyName.kind === SyntaxKind.ComputedPropertyName) {
|
||||
checkComputedPropertyName(<ComputedPropertyName>node.propertyName);
|
||||
}
|
||||
|
||||
// check private/protected variable access
|
||||
const parent = <VariableLikeDeclaration>(<BindingPattern>node.parent).parent;
|
||||
const parentType = getTypeForBindingElementParent(parent);
|
||||
const name = node.propertyName || <Identifier>node.name;
|
||||
const property = getPropertyOfType(parentType, getTextOfPropertyName(name));
|
||||
if (parent.initializer && property && getParentOfSymbol(property)) {
|
||||
checkClassPropertyAccess(parent, parent.initializer, parentType, property);
|
||||
}
|
||||
}
|
||||
|
||||
// For a binding pattern, check contained binding elements
|
||||
|
||||
Reference in new issue
Block a user