/// <summary>
        /// Resolves a type
        /// </summary>
        /// <param name="type">The type to resolve</param>
        /// <returns>The TypeScript type definition</returns>
        protected virtual TsType OnResolve(ITypeSymbol type)
        {
            if (TypeLookup.TryGetValue(type, out var tsType))
            {
                return(tsType);
            }

            if (IsGuid(type))
            {
                return(TsPrimitive.String);
            }

            else if (TypeFilter != null && !TypeFilter(type))             // should this assembly be considered?
            {
                tsType = TsPrimitive.Any;
            }
            else if (type is INamedTypeSymbol namedType && namedType.IsGenericType)
            {
                if ($"{namedType.ContainingNamespace}.{namedType.MetadataName}" == typeof(Task <>).FullName)
                {
                    //Ignore the type "Task" and use the type argument instead.
                    tsType = Resolve(namedType.TypeArguments[0]);
                }
                else
                {
                    var tsGenericType = new TsGenericType(new TsName(type.Name));
                    foreach (var argument in namedType.TypeArguments)
                    {
                        var tsArgType = Resolve(argument);
                        tsGenericType.TypeArguments.Add(tsArgType);
                    }
                    tsType = tsGenericType;
                }
            }
Exemple #2
0
        /// <summary>
        /// Resolves a type
        /// </summary>
        /// <param name="type">The type to resolve</param>
        /// <returns>The TypeScript type definition</returns>
        protected virtual TsType OnResolve(Type type)
        {
            var typeInfo = type.GetTypeInfo();

            TsType tsType;

            if (this.TypeLookup.TryGetValue(type, out tsType))
            {
                return(tsType);
            }
            else if (this.AssemblyFilter != null && !this.AssemblyFilter(typeInfo.Assembly)) // should this assembly be considered?
            {
                tsType = TsPrimitive.Any;
            }
            else if (this.TypeFilter != null && !this.TypeFilter(type)) // should this assembly be considered?
            {
                tsType = TsPrimitive.Any;
            }
            else if (type.IsGenericParameter)
            {
                tsType = new TsGenericType(new TsName(type.Name));
            }
            else if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                var           tsGenericTypeDefinition = Resolve(type.GetGenericTypeDefinition());
                TsGenericType tsGenericType           = new TsGenericType(tsGenericTypeDefinition.Name);
                foreach (var argument in type.GetTypeInfo().GetGenericArguments())
                {
                    var tsArgType = this.Resolve(argument);
                    tsGenericType.TypeArguments.Add(tsArgType);
                }
                tsType = tsGenericType;
            }
            else if (type.IsArray && type.HasElementType)
            {
                var elementType = this.Resolve(type.GetElementType());
                tsType = new TsArray(elementType, type.GetArrayRank());
            }
            else if (typeInfo.IsEnum)
            {
                tsType = GenerateEnum(type);
            }
            else if (typeInfo.IsAnsiClass)
            {
                tsType = GenerateInterface(type);
            }
            else if (typeInfo.IsInterface)
            {
                tsType = GenerateInterface(type);
            }
            else
            {
                tsType = TsPrimitive.Any;
            }

            return(tsType);
        }
 /// <summary>
 /// Formats a generic type
 /// </summary>
 /// <param name="tsGenericType">The generic type</param>
 /// <returns>The string representation of the generic type</returns>
 public virtual string Format(TsGenericType tsGenericType)
 {
     return
         ($"{ResolveTypeName(tsGenericType.Name, tsGenericType.IsExternallyDefined)}{(tsGenericType.TypeArguments.Count > 0 ? $"<{string.Join(", ", tsGenericType.TypeArguments.Select(Format))}>" : string.Empty)}");
 }