Exemplo n.º 1
0
 public TsInterfaceMember(string name, TsTypeReference type, MemberInfo memberInfo, bool isOptional)
 {
     Name       = name;
     Type       = type;
     MemberInfo = memberInfo;
     IsOptional = isOptional;
 }
Exemplo n.º 2
0
        private static TsTypeReference BuildTsTypeReferenceToPropertyType(MemberInfo member, TypeBuilderConfig config, string currentTsNamespace, bool isOptional)
        {
            if (member is FieldInfo fieldInfo && fieldInfo.IsLiteral)
            {
                var typeValue     = default(string);
                var constantValue = fieldInfo.GetRawConstantValue();
                if (constantValue is string s)
                {
                    typeValue = $"'{s}'";
                }
                else if (constantValue is double d)
                {
                    typeValue = d.ToString(CultureInfo.InvariantCulture);
                }
                else if (constantValue is float f)
                {
                    typeValue = f.ToString(CultureInfo.InvariantCulture);
                }
                else if (constantValue is int i)
                {
                    typeValue = i.ToString(CultureInfo.InvariantCulture);
                }
                else if (constantValue is long l)
                {
                    typeValue = l.ToString(CultureInfo.InvariantCulture);
                }

                if (typeValue != default)
                {
                    return(TsTypeReference.Simple(typeValue, isOptional));
                }
            }

            var type = member is PropertyInfo propertyInfo ? propertyInfo.PropertyType : ((FieldInfo)member).FieldType;
            var typeScriptTypeAttribute = GetTypeScriptTypeAttribute(member);

            if (typeScriptTypeAttribute != null)
            {
                if (typeScriptTypeAttribute.ConstructorArguments[0].Value is string typeString)
                {
                    return(TsTypeReference.Simple(typeString));
                }
                else if (typeScriptTypeAttribute.ConstructorArguments[0].Value is Type replaceWithType)
                {
                    type = replaceWithType;
                }
            }

            return(BuildTsTypeReference(type, config, currentTsNamespace, false));
        }
Exemplo n.º 3
0
        private void CreateTypeBuilderConfig()
        {
            var typeMappings = new Dictionary <string, TsTypeReference>();

            void AddTypeMappingsFromAssembly(Assembly asm)
            {
                foreach (var attribute in TypeUtils.GetAssemblyCustomAttributesData(asm).Where(a =>
                                                                                               a.AttributeType.Name == Constants.DefineTypeScriptTypeForExternalTypeAttributeName && a.ConstructorArguments.Count == 2 && a.ConstructorArguments[1].Value is string))
                {
                    string key;
                    if (attribute.ConstructorArguments[0].Value is Type type)
                    {
                        key = TypeUtils.GetFullNameWithGenericArguments(type);
                    }
                    else if (attribute.ConstructorArguments[0].Value is string s)
                    {
                        key = s;
                    }
                    else
                    {
                        continue;
                    }

                    typeMappings[key] = TsTypeReference.Simple((string)attribute.ConstructorArguments[1].Value);
                }
            }

            foreach (var assembly in _generatorContext.Assemblies)
            {
                AddTypeMappingsFromAssembly(assembly);
            }

            // override mappings from config
            foreach (var mapping in _config.TypeMappings)
            {
                var reference = TsTypeReference.Simple(mapping.Value);
                typeMappings[mapping.Key] = reference;
            }

            var mappings = ImmutableDictionary.CreateRange(typeMappings);

            _typeBuilderConfig = new TypeBuilderConfig(mappings, _config.CustomTypeScriptIgnoreAttributeFullName, _config.WrapConstEnumsInTemplateStrings);
        }
Exemplo n.º 4
0
        private static TsTypeReference BuildTsTypeReference(Type type, TypeBuilderConfig config, string currentTsNamespace, bool returnTheMainTypeEvenIfItHasADerivedTypesUnion)
        {
            var isOptional = false;

            var underlyingNullableType = GetUnderlyingNullableType(type);

            if (underlyingNullableType != null)
            {
                type       = underlyingNullableType;
                isOptional = true;
            }

            if (type.IsGenericParameter)
            {
                return(TsTypeReference.Simple(type.Name, isOptional));
            }

            var typeFullName = TypeUtils.GetFullNameWithGenericArguments(type);

            if (typeFullName != null && config.TypeMappings.TryGetValue(typeFullName, out var mappedType))
            {
                return(TsTypeReference.Wrap(mappedType, isOptional));
            }

            var typeScriptTypeAttribute = GetTypeScriptTypeAttribute(type);

            if (typeScriptTypeAttribute != null)
            {
                if (typeScriptTypeAttribute.ConstructorArguments[0].Value is string typeString)
                {
                    return(TsTypeReference.Simple(typeString));
                }
                else if (typeScriptTypeAttribute.ConstructorArguments[0].Value is Type replaceWithType)
                {
                    type = replaceWithType;
                }
            }

            if (
                TypeUtils.Is <byte>(type) ||
                TypeUtils.Is <sbyte>(type) ||
                TypeUtils.Is <int>(type) ||
                TypeUtils.Is <uint>(type) ||
                TypeUtils.Is <long>(type) ||
                TypeUtils.Is <ulong>(type) ||
                TypeUtils.Is <short>(type) ||
                TypeUtils.Is <ushort>(type) ||
                TypeUtils.Is <double>(type) ||
                TypeUtils.Is <decimal>(type) ||
                TypeUtils.Is <float>(type)
                )
            {
                return(TsTypeReference.Simple("number", isOptional));
            }

            if (TypeUtils.Is <string>(type))
            {
                return(TsTypeReference.Simple("string", isOptional));
            }

            if (TypeUtils.Is <bool>(type))
            {
                return(TsTypeReference.Simple("boolean", isOptional));
            }

            var dictionaryTypes = GetDictionaryUnderlyingTypes(type);

            if (dictionaryTypes != null)
            {
                return(TsTypeReference.Dictionary(BuildTsTypeReference(dictionaryTypes.Item1, config, currentTsNamespace, false), BuildTsTypeReference(dictionaryTypes.Item2, config, currentTsNamespace, false)));
            }

            var enumerableUnderlyingType = GetEnumerableUnderlyingType(type);

            if (enumerableUnderlyingType != null)
            {
                return(TsTypeReference.Array(BuildTsTypeReference(enumerableUnderlyingType, config, currentTsNamespace, false)));
            }

            if (typeFullName != null && (type.IsClass || type.IsInterface || type.IsValueType))
            {
                var namespaceName = GetTypescriptNamespace(type);
                if (!string.IsNullOrEmpty(namespaceName))
                {
                    var derivedTypesUnionName = returnTheMainTypeEvenIfItHasADerivedTypesUnion ? null : GetDerivedTypesUnionName(type);

                    var typeName = TypeUtils.GetNameWithoutGenericArity(type);

                    var prefix = "";
                    var suffix = "";
                    if (type.IsEnum && config.WrapConstEnumsInTemplateStrings)
                    {
                        prefix = "`${";
                        suffix = "}`";
                    }

                    var result = default(TsTypeReference);
                    if (namespaceName == currentTsNamespace)
                    {
                        result = TsTypeReference.Simple(prefix + (derivedTypesUnionName ?? typeName) + suffix, isOptional);
                    }
                    else
                    {
                        result = TsTypeReference.Simple(prefix + namespaceName + "." + (derivedTypesUnionName ?? typeName) + suffix, isOptional);
                    }

                    if (result != null)
                    {
                        if (type.GenericTypeArguments.Length > 0)
                        {
                            var typeArgs = type.GenericTypeArguments.Select(t => BuildTsTypeReference(t, config, currentTsNamespace, false));
                            result = TsTypeReference.Generic(result, typeArgs);
                        }

                        return(result);
                    }
                }
            }

            return(TsTypeReference.Simple("unknown", isOptional));
        }
Exemplo n.º 5
0
 public GenericTsTypeReference(TsTypeReference genericType, IEnumerable <TsTypeReference> arguments)
 {
     _genericType = genericType;
     _arguments   = ImmutableArray.CreateRange(arguments);
 }
Exemplo n.º 6
0
 public static TsTypeReference Wrap(TsTypeReference inner, bool isOptional)
 {
     return(new WrappedTsTypeReference(inner, isOptional));
 }
Exemplo n.º 7
0
 public WrappedTsTypeReference(TsTypeReference inner, bool isOptional)
 {
     _inner     = inner;
     IsOptional = isOptional;
 }
Exemplo n.º 8
0
 public static TsTypeReference Generic(TsTypeReference genericType, IEnumerable <TsTypeReference> arguments)
 {
     return(new GenericTsTypeReference(genericType, arguments));
 }
Exemplo n.º 9
0
 public static TsTypeReference Array(TsTypeReference elementTypeReference)
 {
     return(new ArrayTsTypeReference(elementTypeReference));
 }
Exemplo n.º 10
0
 public static TsTypeReference Dictionary(TsTypeReference keyType, TsTypeReference valueType)
 {
     return(new DictionaryTsTypeReference(keyType, valueType));
 }
Exemplo n.º 11
0
 public DictionaryTsTypeReference(TsTypeReference keyType, TsTypeReference valueType)
 {
     _keyType   = keyType;
     _valueType = valueType;
 }
Exemplo n.º 12
0
 public ArrayTsTypeReference(TsTypeReference itemType)
 {
     _itemType = itemType;
 }