Esempio n. 1
0
        private static Node GetObjectLiteralType(ObjectLiteralExpression objectLiteral)
        {
            Node type = GetDeclarationType(objectLiteral);

            if (type != null)
            {
                return(type);
            }

            List <Node> properties = objectLiteral.Properties;

            if (properties.Count == 0)
            {
                TypeLiteral typeLiteral = NodeHelper.CreateNode(NodeKind.TypeLiteral) as TypeLiteral;
                typeLiteral.Members.Add(NodeHelper.CreateNode(NodeKind.IndexSignature));
                return(typeLiteral);
            }
            else
            {
                TypeLiteral typeLiteral = NodeHelper.CreateNode(NodeKind.TypeLiteral) as TypeLiteral;
                foreach (PropertyAssignment prop in properties)
                {
                    Node initValue   = prop.Initializer;
                    Node elementType = null;
                    if (initValue.Kind != NodeKind.ObjectLiteralExpression && initValue.Kind != NodeKind.ArrayLiteralExpression)
                    {
                        elementType = GetNodeType(initValue);
                    }
                    elementType          = elementType ?? NodeHelper.CreateNode(NodeKind.AnyKeyword);
                    elementType.NodeName = "type";

                    Node propSignature = NodeHelper.CreateNode(NodeKind.PropertySignature);
                    propSignature.AddChild(prop.Name.TsNode);
                    propSignature.AddChild(elementType);

                    typeLiteral.Members.Add(propSignature);
                }
                return(typeLiteral);
            }
        }
Esempio n. 2
0
        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);
        }