Exemplo n.º 1
0
        public void SetParameter(string name, ITSType type)
        {
            var indexOf = Parameters.IndexOf(KVP => KVP.Key == name);

            Parameters[indexOf] = KVP(name, new TSParameterDescription()
            {
                Type = type
            });
        }
Exemplo n.º 2
0
 public void AddParameter(string name, ITSType type)
 {
     if (Parameters == null)
     {
         Parameters = new List <KeyValuePair <string, TSParameterDescription> >();
     }
     Parameters.Add(name, new TSParameterDescription()
     {
         Type = type
     });
 }
Exemplo n.º 3
0
        public static ITSType MappedType(ITSType t, Func <ITSType, ITSType> mapper)
        {
            switch (t)
            {
            case TSSimpleType x:
            case TSPlaceholder y:
                break;

            case TSGenericType x:
                for (int i = 0; i < x.Parameters.Count; i++)
                {
                    x.Parameters[i] = mapper(x.Parameters[i]);
                }
                break;

            case TSComposedType x:
                for (int i = 0; i < x.Parts.Count; i++)
                {
                    var toAdd = x.Parts.Select(mapper).ToList();
                    x.Parts.Clear();
                    toAdd.AddRangeTo(x.Parts);
                }
                break;

            case TSTupleType x:
                for (int i = 0; i < x.Members.Count; i++)
                {
                    x.Members[i] = mapper(x.Members[i]);
                }
                break;

            case TSObjectType x:
                x.Members.ForEachKVP((name, t2) => {
                    x.Members[name] = (mapper(t2.type), t2.@readonly);
                });
                break;

            case TSFunctionType x:
                var m = x.FunctionDescription;
                m.ReturnType = mapper(m.ReturnType);
                for (int i = 0; i < m.Parameters.Count; i++)
                {
                    var(name, p) = m.Parameters[i];
                    p.Type       = mapper(p.Type);
                }
                break;

            default:
                throw new NotImplementedException();
            }
            return(mapper(t));
        }
Exemplo n.º 4
0
 public static bool IsLiteralType(this ITSType type) => type is TSSimpleType x && x.IsLiteralType;
Exemplo n.º 5
0
 public bool Equals(ITSType other) => other is TSSimpleType x && FullName == x.FullName;
Exemplo n.º 6
0
        public static string GetTypeString(ITSType type, string ns, bool asConstraint = false)
        {
            string ret = null;

            switch (type)
            {
            case TSSimpleType x when x.FullName == "Array":
                ret = "[]";
                break;

            case TSSimpleType x:
                ret = RelativeName(x.FullName, ns);
                break;

            case TSTupleType x:
                ret = $"[{x.Members.Joined(", ", y => GetTypeString(y, ns))}]";
                break;

            case TSObjectType x:
                var joined = x.Members.JoinedKVP((key, val) => {
                    var @readonly = val.@readonly ? "readonly " : "";
                    if (key.Contains("."))
                    {
                        key = $"'{key}'";
                    }
                    return($"{@readonly}{key}: {GetTypeString(val.type, ns)}");
                }, ", ");
                ret = $"{{{joined}}}";
                break;

            case TSFunctionType x:
                ret = $"({x.FunctionDescription.Parameters.Joined(", ", y => GetParameterString(y, ns))}) => {GetTypeString(x.FunctionDescription.ReturnType, ns)}";
                break;

            case TSComposedType x:
                ret = x.Parts.Select(y => GetTypeString(y, ns)).OrderBy(y => y).Joined($" {x.Operator} ");
                break;

            case TSGenericType x when x.Name == "Array" && x.Parameters.Count == 1 && (x.Parameters.Single() is TSSimpleType || x.Parameters.Single() is TSPlaceholder):
                var prm = x.Parameters.Single();
                ret = $"{GetTypeString(prm, ns)}[]";
                break;

            case TSGenericType x:
                ret = $"{x.Name}<{x.Parameters.Joined(", ", y => GetTypeString(y, ns))}>";
                break;

            case TSPlaceholder x:
                var @default = "";
                var extends  = "";
                if (asConstraint)
                {
                    if (x.Default != null)
                    {
                        @default = $" = {GetTypeString(x.Default, ns)}";
                    }
                    if (x.Extends != null)
                    {
                        extends = $" extends {GetTypeString(x.Extends, ns)}";
                    }
                }
                ret = $"{x.Name}{extends}{@default}";
                break;

            case TSKeyOf x:
                ret = $"keyof {GetTypeString(x.Operand, ns)}";
                break;

            case TSLookup x:
                ret = $"{GetTypeString(x.Type, ns)}[{x.Accessor}]";
                break;

            default:
                if (type != null)
                {
                    throw new NotImplementedException();
                }
                break;
            }

            return(ret);
        }