mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-24 11:43:18 -05:00
Use ancestor walk to determine if property access is within constructor #9230
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user