예제 #1
0
        private static bool IsSupportedDictionaryType(Type type, out ITypeInfo typeWrapper, out Type keyType, out Type valueType)
        {
            keyType     = valueType = null;
            typeWrapper = null;

            // Type must implement both IDictionary<TKey,TValue> and IDictionary
            if (!(Utils.ImplementsInterface(type, typeof(IDictionary <,>)) &&
                  Utils.ImplementsInterface(type, typeof(IDictionary))))
            {
                return(false);
            }

            typeWrapper = TypeFactory.GetTypeInfo(type);
            var genericArguments = typeWrapper.GetGenericArguments();

            if (genericArguments.Length != 2)
            {
                return(false);
            }
            keyType   = genericArguments[0];
            valueType = genericArguments[1];
            if (keyType != typeof(string) || valueType == typeof(object))
            {
                return(false);
            }

            return(true);
        }
        protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            var genericArgs = type.GetGenericArguments();
            var keyType     = typeGenerator.BuildAndImportType(targetUnit, genericArgs[0]);
            var valueType   = typeGenerator.BuildAndImportType(targetUnit, genericArgs[1]);

            if (typeGenerator.Options.NullabilityMode != NullabilityMode.NullableReference)
            {
                keyType   = keyType.NotNull();
                valueType = valueType.NotNull();
            }
            return(new TypeScriptTypeDefintion
            {
                Members =
                {
                    new TypeScriptTypePropertyGetterDeclaration
                    {
                        Argument = new TypeScriptArgumentDeclaration
                        {
                            Name = "key",
                            Type = keyType,
                        },
                        ResultType = valueType,
                        Optional = true,
                    }
                }
            });
        }
예제 #3
0
        protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            var elementType = type.GetGenericArguments()[0];
            var itemType    = typeGenerator.BuildAndImportType(targetUnit, elementType);

            return(new TypeScriptArrayType(itemType));
        }
        protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            var itemType           = type.IsGenericType ? type.GetGenericArguments()[0] : TypeInfo.From(typeof(void));
            var itemTypeScriptType = typeGenerator.BuildAndImportType(targetUnit, itemType);

            return(new TypeScriptPromiseOfType(itemTypeScriptType));
        }
 public static bool IsContext(this ITypeInfo type)
 {
     return(!type.IsAbstract &&
            !type.GetCustomAttributes(FullNames.BehaviorsAttribute, false).Any() &&
            !type.GetGenericArguments().Any() &&
            type.GetFields().Any(x => x.IsSpecification() || x.IsBehavior()));
 }
예제 #6
0
        public static string ToSimpleString(this ITypeInfo type)
        {
            Guard.AgainstNullArgument("type", type);

            var baseTypeName = type.Name;

            var backTickIdx = baseTypeName.IndexOf('`');

            if (backTickIdx >= 0)
            {
                baseTypeName = baseTypeName.Substring(0, backTickIdx);
            }

            var lastIndex = baseTypeName.LastIndexOf('.');

            if (lastIndex >= 0)
            {
                baseTypeName = baseTypeName.Substring(lastIndex + 1);
            }

            if (!type.IsGenericType)
            {
                return(baseTypeName);
            }

            var genericTypes = type.GetGenericArguments().ToArray();
            var simpleNames  = new string[genericTypes.Length];

            for (var idx = 0; idx < genericTypes.Length; idx++)
            {
                simpleNames[idx] = ToSimpleString(genericTypes[idx]);
            }

            return(string.Format(CultureInfo.InvariantCulture, "{0}<{1}>", baseTypeName, string.Join(", ", simpleNames)));
        }
예제 #7
0
        static string ConvertToSimpleTypeName(ITypeInfo type)
        {
            var baseTypeName = type.Name;

            int backTickIdx = baseTypeName.IndexOf('`');

            if (backTickIdx >= 0)
            {
                baseTypeName = baseTypeName.Substring(0, backTickIdx);
            }

            var lastIndex = baseTypeName.LastIndexOf('.');

            if (lastIndex >= 0)
            {
                baseTypeName = baseTypeName.Substring(lastIndex + 1);
            }

            if (!type.IsGenericType)
            {
                return(baseTypeName);
            }

            ITypeInfo[] genericTypes = type.GetGenericArguments().ToArray();
            string[]    simpleNames  = new string[genericTypes.Length];

            for (int idx = 0; idx < genericTypes.Length; idx++)
            {
                simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]);
            }

            return(String.Format("{0}<{1}>", baseTypeName, String.Join(", ", simpleNames)));
        }
예제 #8
0
        /// <summary>
        /// Converts a type into a name string.
        /// </summary>
        /// <param name="type">The type to convert.</param>
        /// <returns>Name string of type.</returns>
        public static string ConvertToSimpleTypeName(ITypeInfo type)
        {
            var baseTypeName = type.Name;

            var backTickIdx = baseTypeName.IndexOf('`');

            if (backTickIdx >= 0)
            {
                baseTypeName = baseTypeName.Substring(0, backTickIdx);
            }

            var lastIndex = baseTypeName.LastIndexOf('.');

            if (lastIndex >= 0)
            {
                baseTypeName = baseTypeName.Substring(lastIndex + 1);
            }

            if (!type.IsGenericType)
            {
                return(baseTypeName);
            }

            var genericTypes = type.GetGenericArguments().ToArray();
            var simpleNames  = new string[genericTypes.Length];

            for (var idx = 0; idx < genericTypes.Length; idx++)
            {
                simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]);
            }

            return($"{baseTypeName}<{string.Join(", ", simpleNames)}>");
        }
예제 #9
0
        public static int GetGenericArgumentsToSkip(ITypeInfo type)
        {
            if (type.IsArray)
            {
                return(1 + GetGenericArgumentsToSkip(type.GetElementType()));
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>))))
            {
                return(0);
            }

            if (!type.IsGenericType)
            {
                return(type.IsValueType ? 0 : 1);
            }

            var count = 1;

            foreach (var argument in type.GetGenericArguments())
            {
                count += GetGenericArgumentsToSkip(argument);
            }

            return(count);
        }
예제 #10
0
        public static bool Equals(ITypeInfo self, ITypeInfo other)
        {
            if (self == null && other == null)
            {
                return(true);
            }

            if (self == null || other == null)
            {
                return(false);
            }

            if (self.IsArray)
            {
                return(other.IsArray && self.GetElementType().Equals(other.GetElementType()));
            }

            if (self.IsGenericParameter)
            {
                return(other.IsGenericParameter && self.Name == other.Name);
            }

            return(self.Name == other.Name &&
                   self.Namespace == other.Namespace &&
                   self.IsGenericTypeDefinition == other.IsGenericTypeDefinition &&
                   GenericArgumentsEquals(self.GetGenericArguments(), other.GetGenericArguments()));
        }
        protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            var typeReference = typeGenerator.BuildAndImportType(targetUnit, type.GetGenericTypeDefinition());

            return(new TypeScriptGenericTypeReference(
                       (TypeScriptTypeReference)typeReference,
                       type.GetGenericArguments().Select(x => GetArgumentType(x, typeGenerator, targetUnit)).ToArray()
                       ));
        }
예제 #12
0
 private TypeScriptType GetTypeScriptType(TypeScriptUnit targetUnit, ITypeInfo typeInfo, IAttributeProvider?attributeProvider)
 {
     if (typeDeclarations.ContainsKey(typeInfo))
     {
         return(typeDeclarations[typeInfo].ReferenceFrom(targetUnit, this, attributeProvider));
     }
     if (typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>))))
     {
         return(new TypeScriptNullableType(GetTypeScriptType(targetUnit, typeInfo.GetGenericArguments()[0], null)));
     }
     return(ResolveType(typeInfo).ReferenceFrom(targetUnit, this, attributeProvider));
 }
예제 #13
0
        private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeInfo typeInfo)
        {
            if (BuiltinTypeBuildingContext.Accept(typeInfo))
            {
                return(new BuiltinTypeBuildingContext(typeInfo));
            }

            if (ArrayTypeBuildingContext.Accept(typeInfo))
            {
                return(new ArrayTypeBuildingContext(typeInfo, Options));
            }

            if (DictionaryTypeBuildingContext.Accept(typeInfo))
            {
                return(new DictionaryTypeBuildingContext(typeInfo, Options));
            }

            if (typeInfo.IsEnum)
            {
                var targetUnit = typeUnitFactory.GetOrCreateTypeUnit(typeLocation);
                return(Options.EnumGenerationMode == EnumGenerationMode.FixedStringsAndDictionary
                           ? (ITypeBuildingContext) new FixedStringsAndDictionaryTypeBuildingContext(targetUnit, typeInfo)
                           : new TypeScriptEnumTypeBuildingContext(targetUnit, typeInfo));
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>))))
            {
                var underlyingType = typeInfo.GetGenericArguments().Single();
                if (Options.EnableExplicitNullability)
                {
                    return(new NullableTypeBuildingContext(underlyingType, Options.UseGlobalNullable));
                }
                return(GetTypeBuildingContext(typeLocation, underlyingType));
            }

            if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition)
            {
                return(new GenericTypeTypeBuildingContext(typeInfo, Options));
            }

            if (typeInfo.IsGenericParameter)
            {
                return(new GenericParameterTypeBuildingContext(typeInfo));
            }

            if (typeInfo.IsGenericTypeDefinition)
            {
                return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
            }

            return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo));
        }
예제 #14
0
        protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            var itemTypeScriptType = typeGenerator.BuildAndImportType(targetUnit, type.GetGenericArguments()[0]);

            if (typeGenerator.Options.NullabilityMode == NullabilityMode.None)
            {
                return(itemTypeScriptType);
            }

            return(typeGenerator.Options.UseGlobalNullable
                       ? (TypeScriptType) new TypeScriptNullableType(itemTypeScriptType)
                       : new TypeScriptOrNullType(itemTypeScriptType));
        }
예제 #15
0
        protected virtual ITypeInfo ResolveReturnType(ITypeInfo typeInfo)
        {
            if (typeInfo.IsGenericType)
            {
                var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
                if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task<>))))
                    return ResolveReturnType(typeInfo.GetGenericArguments()[0]);
            }

            if (typeInfo.Equals(TypeInfo.From<Task>()))
                return TypeInfo.From(typeof(void));
            return typeInfo;
        }
예제 #16
0
        private ITypeInfo GetElementType(ITypeInfo arrayType)
        {
            if (arrayType.IsArray)
            {
                return(arrayType.GetElementType() ?? throw new ArgumentNullException($"Array type's {arrayType.Name} element type is not defined"));
            }

            if (arrayType.IsGenericType && arrayType.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(List <>))))
            {
                return(arrayType.GetGenericArguments()[0]);
            }

            throw new ArgumentException("arrayType should be either Array or List<T>", nameof(arrayType));
        }
        private static ITypeInfo GetTypeFromGenericArgumentReferenceType(IMetadataGenericArgumentReferenceType reference, ITypeInfo typeInfo, IMethodInfo methodInfo)
        {
            var argument = reference.Argument;
            switch (argument.Kind)
            {
                case GenericArgumentKind.Type:
                    return typeInfo.GetGenericArguments().ToList()[(int)argument.Index];

                case GenericArgumentKind.Method:
                    return methodInfo.GetGenericArguments().ToList()[(int)argument.Index];
            }

            return null;
        }
예제 #18
0
        public static (bool, ITypeInfo) ProcessNullable(IAttributeProvider?attributeProvider, ITypeInfo type, NullabilityMode nullabilityMode)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>))))
            {
                var underlyingType = type.GetGenericArguments()[0];
                return(true, underlyingType);
            }

            if (attributeProvider == null || !type.IsClass && !type.IsInterface)
            {
                return(false, type);
            }

            return(CanBeNull(attributeProvider, nullabilityMode), type);
        }
예제 #19
0
        private static ITypeInfo GetTypeFromGenericArgumentReferenceType(IMetadataGenericArgumentReferenceType reference, ITypeInfo typeInfo, IMethodInfo methodInfo)
        {
            var argument = reference.Argument;

            switch (argument.Kind)
            {
            case GenericArgumentKind.Type:
                return(typeInfo.GetGenericArguments().ToList()[(int)argument.Index]);

            case GenericArgumentKind.Method:
                return(methodInfo.GetGenericArguments().ToList()[(int)argument.Index]);
            }

            return(null);
        }
        protected override ITypeInfo ResolveReturnType(ITypeInfo typeInfo)
        {
            if (typeInfo.IsGenericType)
            {
                var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
                if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task <>))) || genericTypeDefinition.Equals(TypeInfo.From(typeof(ActionResult <>))))
                {
                    return(ResolveReturnType(typeInfo.GetGenericArguments()[0]));
                }
            }

            if (typeInfo.Equals(TypeInfo.From <Task>()) || typeInfo.Equals(TypeInfo.From <ActionResult>()))
            {
                return(TypeInfo.From(typeof(void)));
            }

            return(typeInfo);
        }
예제 #21
0
        static string ConvertToSimpleTypeName(ITypeInfo type)
        {
            var baseTypeName = type.Name;

            var backTickIdx = baseTypeName.IndexOf('`');
            if (backTickIdx >= 0)
                baseTypeName = baseTypeName.Substring(0, backTickIdx);

            var lastIndex = baseTypeName.LastIndexOf('.');
            if (lastIndex >= 0)
                baseTypeName = baseTypeName.Substring(lastIndex + 1);

            if (!type.IsGenericType)
                return baseTypeName;

            var genericTypes = type.GetGenericArguments().ToArray();
            var simpleNames = new string[genericTypes.Length];

            for (var idx = 0; idx < genericTypes.Length; idx++)
                simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]);

            return $"{baseTypeName}<{string.Join(", ", simpleNames)}>";
        }
예제 #22
0
 public DictionaryTypeBuildingContext(ITypeInfo dictionaryType, TypeScriptGenerationOptions options)
 {
     keyType      = dictionaryType.GetGenericArguments()[0];
     valueType    = dictionaryType.GetGenericArguments()[1];
     this.options = options;
 }
예제 #23
0
 public CollectionTypeBuildingContext(ITypeInfo arrayType)
 {
     elementType = arrayType.GetGenericArguments()[0];
 }
예제 #24
0
        static string ConvertToSimpleTypeName(ITypeInfo type)
        {
            var baseTypeName = type.Name;

            int backTickIdx = baseTypeName.IndexOf('`');
            if (backTickIdx >= 0)
                baseTypeName = baseTypeName.Substring(0, backTickIdx);

            var lastIndex = baseTypeName.LastIndexOf('.');
            if (lastIndex >= 0)
                baseTypeName = baseTypeName.Substring(lastIndex + 1);

            if (!type.IsGenericType)
                return baseTypeName;

            ITypeInfo[] genericTypes = type.GetGenericArguments().ToArray();
            string[] simpleNames = new string[genericTypes.Length];

            for (int idx = 0; idx < genericTypes.Length; idx++)
                simpleNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]);

            return String.Format("{0}<{1}>", baseTypeName, String.Join(", ", simpleNames));
        }
예제 #25
0
 public static bool Accept(ITypeInfo typeInfo)
 {
     return(typeInfo.IsGenericType &&
            typeInfo.GetGenericArguments().Length == 1 &&
            typeInfo.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(ICollection <>)))));
 }
예제 #26
0
 public IEnumerable <ITypeInfo> GetGenericArguments()
 {
     return(inner.GetGenericArguments());
 }
 public IEnumerable <ITypeInfo> GetGenericArguments()
 {
     return(_typeInfoImplementation.GetGenericArguments());
 }