Use ancestor walk to determine if property access is within constructor #9230

This commit is contained in:
Charles Pierce
2017-10-09 10:57:08 -07:00
parent 9e00df590d
commit 79f5d968a1
7 changed files with 123 additions and 32 deletions

View File

@@ -14844,10 +14844,9 @@ namespace ts {
// Referencing Abstract Properties within Constructors is not allowed
if ((flags & ModifierFlags.Abstract) && symbolHasNonMethodDeclaration(prop)) {
const declaringClassDeclaration = <ClassLikeDeclaration>getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop));
const declaringClassConstructor = declaringClassDeclaration && findConstructorDeclaration(declaringClassDeclaration);
if (declaringClassConstructor && isNodeWithinFunction(node, declaringClassConstructor)) {
error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_constructor, symbolToString(prop), typeToString(getDeclaringClass(prop)));
if (declaringClassDeclaration && isNodeWithinConstructor(node, declaringClassDeclaration)) {
error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), typeToString(getDeclaringClass(prop)));
return false;
}
}
@@ -23153,8 +23152,16 @@ namespace ts {
return result;
}
function isNodeWithinFunction(node: Node, functionDeclaration: FunctionLike) {
return getContainingFunction(node) === functionDeclaration;
function isNodeWithinConstructor(node: Node, classDeclaration: ClassLikeDeclaration) {
return findAncestor(node, element => {
if (isConstructorDeclaration(element) && nodeIsPresent(element.body)) {
return true;
} else if (element === classDeclaration || isFunctionLikeDeclaration(element)) {
return "quit";
}
return false;
});
}
function isNodeWithinClass(node: Node, classDeclaration: ClassLikeDeclaration) {

View File

@@ -2220,7 +2220,7 @@
"category": "Error",
"code": 2714
},
"Abstract property '{0}' in class '{1}' cannot be accessed in constructor.": {
"Abstract property '{0}' in class '{1}' cannot be accessed in the constructor.": {
"category": "Error",
"code": 2715
},