private static Node GetDeclarationType(Node node) { VariableDeclarationNode variableParent = node.Parent as VariableDeclarationNode; if (variableParent != null) { return(variableParent.Type); } // Parameter paramterParent = node.Parent as Parameter; if (paramterParent != null) { return(paramterParent.Type); } // PropertyDeclaration propertyDeclarationParent = node.Parent as PropertyDeclaration; if (propertyDeclarationParent != null) { return(propertyDeclarationParent.Type); } // CallExpression callExpressionParent = node.Parent as CallExpression; if (callExpressionParent != null) { int index = callExpressionParent.Arguments.IndexOf(node); Node member = GetPropertyAccessMember(callExpressionParent); if (index >= 0 && member != null) { List <Node> parameters = new List <Node>(); if (member.Kind == NodeKind.MethodDeclaration) { parameters = (member as MethodDeclaration).Parameters; } else if (member.Kind == NodeKind.MethodSignature) { parameters = (member as MethodSignature).Parameters; } else if (member.Kind == NodeKind.Constructor) { parameters = (member as Constructor).Parameters; } if (0 <= index && index < parameters.Count) { return((parameters[index] as Parameter).Type); } } } // ReturnStatement returnParent = node.Parent as ReturnStatement; if (returnParent != null) { MethodDeclaration method = returnParent.Ancestor(NodeKind.MethodDeclaration) as MethodDeclaration; return(method?.Type); } // BinaryExpression binaryParent = node.Parent as BinaryExpression; if (binaryParent != null && binaryParent.OperatorToken.Kind == NodeKind.EqualsToken && binaryParent.Right == node) //assign { return(GetNodeType(binaryParent.Left)); } // ConditionalExpression conditionalParent = node.Parent as ConditionalExpression; if (conditionalParent != null) { return(GetDeclarationType(conditionalParent)); } // NewExpression newParent = node.Parent as NewExpression; if (newParent != null) { int index = newParent.Arguments.IndexOf(node); string clsName = TypeHelper.ToShortName(newParent.Type.Text); Project project = newParent.Project; ClassDeclaration classDeclaration = project?.GetClass(clsName); Constructor ctor = classDeclaration?.GetConstructor(); if (index >= 0 && ctor != null) { return((ctor.Parameters[index] as Parameter).Type); } } // PropertyAssignment propertyAssignParent = node.Parent as PropertyAssignment; //{a: [], b: ''} ObjectLiteralExpression objLiteralParent = propertyAssignParent?.Parent as ObjectLiteralExpression; if (objLiteralParent != null) { string memberName = propertyAssignParent.Name.Text; Node objLiteralType = GetNodeType(objLiteralParent); if (objLiteralType != null && objLiteralType.Kind == NodeKind.TypeLiteral) { PropertySignature member = (objLiteralType as TypeLiteral).Members.Find(n => (n as PropertySignature).Name.Text == memberName) as PropertySignature; if (member != null) { return(member.Type); } } } // ParenthesizedExpression parentthesizedParent = node.Parent as ParenthesizedExpression; if (parentthesizedParent != null) { return(GetDeclarationType(parentthesizedParent)); } return(null); }
private static Node GetPropertyAccessMember(Node accessNode) { string[] accessNames = GetNameParts(accessNode.Text); Document document = accessNode.Document; Project project = document?.Project; Node classNode = null; for (int i = 0; i < accessNames.Length; i++) { string memberName = accessNames[i]; if (i == 0 && memberName == "this") { classNode = accessNode.Ancestor(NodeKind.ClassDeclaration); } else if (i == 0 && memberName == "super") { ClassDeclaration thisClassNode = accessNode.Ancestor(NodeKind.ClassDeclaration) as ClassDeclaration; classNode = thisClassNode == null ? null : project.GetBaseClass(thisClassNode); if (classNode != null) { return((classNode as ClassDeclaration).GetConstructor()); } } else { Node type = null; Node member = null; if (i == 0) { type = GetIdentifierType(accessNode, memberName); } else { member = GetClassInterfaceMember(classNode, memberName); type = member?.GetValue("Type") as Node; } if (type == null) { return(null); } if (i == accessNames.Length - 1) { return(member); } string[] typeParts = GetNameParts(type.Text); memberName = typeParts[typeParts.Length - 1]; classNode = project?.GetClass(memberName); if (classNode == null) { classNode = project?.GetInterface(memberName); } } if (classNode == null) { return(null); } } return(null); }