private static Node GetVariableStatementType(List <Node> statements, string name) { foreach (var statement in statements) { if (statement.Kind != NodeKind.VariableStatement) { continue; } VariableDeclarationList declarationList = (statement as VariableStatement).DeclarationList as VariableDeclarationList; VariableDeclarationNode declarationNode = declarationList.Declarations[0] as VariableDeclarationNode; if (declarationNode.Name.Text == name) { return(declarationNode.Type); } } return(null); }
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); }