Ejemplo 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);
            }
        }
Ejemplo n.º 2
0
        private static Node GetBinaryExpressionType(BinaryExpression binary)
        {
            switch (binary.OperatorToken.Kind)
            {
            case NodeKind.MinusToken:
            case NodeKind.AsteriskToken:
            case NodeKind.SlashToken:
                return(NodeHelper.CreateNode(NodeKind.NumberKeyword));

            case NodeKind.PlusToken:
                Node leftType  = GetNodeType(binary.Left);
                Node rightType = GetNodeType(binary.Right);
                if (IsNumberType(leftType) && IsNumberType(rightType))
                {
                    return(NodeHelper.CreateNode(NodeKind.NumberKeyword));
                }
                return(NodeHelper.CreateNode(NodeKind.StringKeyword));

            default:
                return(null);
            }
        }
Ejemplo n.º 3
0
 public void AddChild(JObject tsNode)
 {
     this.AddChild(NodeHelper.CreateNode(tsNode));
 }