예제 #1
0
        private static string GetNormalizedTypeName(Node type)
        {
            string name = "";

            if (type.Kind == NodeKind.TypeLiteral)
            {
                List <string> itemTypes = new List <string>();
                foreach (Node item in (type as TypeLiteral).Members)
                {
                    if (item.Kind == NodeKind.PropertySignature)
                    {
                        PropertySignature psItem        = item as PropertySignature;
                        string            questionToken = psItem.QuestionToken != null ? "?" : "";
                        itemTypes.Add($"{psItem.Name.Text}{questionToken}:{psItem.Type.Text}");
                    }
                    else
                    {
                        itemTypes.Add(item.Text);
                    }
                }
                name = string.Join(',', itemTypes);
            }
            else
            {
                name = type.Text;
            }

            string ret = "";

            string[] parts = name.TrimEnd(';').Split(" ", StringSplitOptions.RemoveEmptyEntries);
            foreach (var part in parts)
            {
                int index = part.LastIndexOf('.');
                if (index >= 0)
                {
                    ret += part.Substring(index + 1);
                }
                else
                {
                    ret += part;
                }
            }
            return(ret.Replace(" ", ""));
        }
예제 #2
0
        public static Node GetTypeLiteralMemberType(TypeLiteral typeLiteral, string name)
        {
            foreach (Node node in typeLiteral.Members)
            {
                switch (node.Kind)
                {
                case NodeKind.PropertySignature:
                    PropertySignature prop = node as PropertySignature;
                    if (prop.Name.Text == name)
                    {
                        return(prop.Type);
                    }
                    break;

                case NodeKind.IndexSignature:
                    IndexSignature index = node as IndexSignature;
                    return(index.Type);

                default:
                    break;
                }
            }
            return(null);
        }
예제 #3
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);
        }