private void AddFromJsFunction(DeclarationBase src, ClassType target) { var fromJS = new RawStatements(); if (target.Extends != null) { fromJS.Add("super.fromJS(obj);\n"); } foreach (var targetItem in target.Members.OfType <PropertyMember>()) { var srcItem = src.Members.OfType <PropertyMember>().First(x => x.Name == targetItem.Name); TypescriptTypeBase itemType = null; if (srcItem.MemberType != null && srcItem.MemberType.ReferencedType != null) { itemType = srcItem.MemberType.ReferencedType; } if (Options.JsConversion_Function_PropertyExistenceCheck) { fromJS.Add("if (obj." + srcItem.Name + ") { "); } if (itemType is ArrayType) { var elementType = srcItem.MemberType.ExtractArrayElement(); if (elementType != null && elementType.ReferencedType != null && elementType.ReferencedType is DeclarationBase) { fromJS.Add("this." + srcItem.Name + "(obj." + srcItem.Name + ".map(item=>new ", elementType, "().fromJS(item)))"); } else { //todo:conversions fromJS.Add("this." + srcItem.Name + "(obj." + srcItem.Name + ")"); } } else { if (itemType == PrimitiveType.Date) { fromJS.Add("this." + srcItem.Name + "(new Date(obj." + srcItem.Name + "))"); } else { fromJS.Add("this." + srcItem.Name + "(obj." + srcItem.Name + ")"); } } if (Options.JsConversion_Function_PropertyExistenceCheck) { fromJS.Add("};"); } fromJS.Add("\n"); } fromJS.Add("return this;"); target.Members.Add(new FunctionMember("fromJS", fromJS) { Parameters = { new FunctionParameter("obj") } }); }
public static bool IsSame(this TypescriptTypeBase t1, TypescriptTypeBase t2) { if (t1 == null || t2 == null) { return(t1 == t2); } if (t1.GetType() != t2.GetType()) { return(false); } if (t1 is PrimitiveType) { return(IsSame((PrimitiveType)t1, (PrimitiveType)t2)); } if (t1 is ArrayType) { return(IsSame((ArrayType)t1, (ArrayType)t2)); } return(t1 == t2); }
private void GenerateReference(TypescriptTypeBase referencedType) { if (referencedType is ArrayType) { GenerateReference((ArrayType)referencedType); } else if (referencedType is PrimitiveType) { GenerateReference((PrimitiveType)referencedType); } else if (referencedType is EnumType) { GenerateReference((EnumType)referencedType); } else if (referencedType is DeclarationBase) { GenerateReference((DeclarationBase)referencedType); } else { throw new ArgumentOutOfRangeException("Cannot generate reference " + referencedType); } }
public virtual TypescriptTypeBase RewriteReference(TypescriptTypeBase referencedType) { if (referencedType is ArrayType) { return(RewriteArrayReference((ArrayType)referencedType)); } else if (referencedType is PrimitiveType) { return(RewritePrimitiveReference((PrimitiveType)referencedType)); } else if (referencedType is EnumType) { return(RewriteEnumReference((EnumType)referencedType)); } else if (referencedType is DeclarationBase) { return(RewriteObjectReference((DeclarationBase)referencedType)); } else { throw NonVisitable(referencedType, "referenced type {0}"); } }
public virtual void VisitReference(TypescriptTypeBase referencedType) { if (referencedType is ArrayType) { VisitReference((ArrayType)referencedType); } else if (referencedType is PrimitiveType) { VisitReference((PrimitiveType)referencedType); } else if (referencedType is EnumType) { VisitReference((EnumType)referencedType); } else if (referencedType is DeclarationBase) { VisitReference((DeclarationBase)referencedType); } else { NonVisitable(referencedType, "referenced type {0}"); } }
private int GetIndexInModule(List <ModuleElement> ordered, TypescriptTypeBase type) { return(ordered.FindIndex(x => (x is DeclarationModuleElement) && (((DeclarationModuleElement)x).Declaration == type))); }
public bool ShouldGenerateGenericTypeArgument(TypescriptTypeBase result, Type genericTypeArgument) { return(true); }
public TypescriptTypeReference(TypescriptTypeBase type) : this() { ReferencedType = type; }
protected virtual TypescriptTypeReference TypeGenerator(Type type, TypescriptTypeBase result) { if (Nullable.GetUnderlyingType(type) != null) { var r = GenerateFromType(Nullable.GetUnderlyingType(type)); r.ExtraData[SOURCETYPE_KEY] = type; return(r); } if (type.IsPrimitive) { return(PrimitiveType.Number); //others are in dictionary } if (type.IsGenericParameter) { //TODO:?!? //return new GenericParameter() { Name = NamingStrategy.GetGenericArgumentName(type), ExtraData = { { SOURCETYPE_KEY, type } } }; return(new TypescriptTypeReference(NamingStrategy.GetGenericArgumentName(type)) { ExtraData = { { SOURCETYPE_KEY, type } } }); } if (type.IsArray) { if (type.GetArrayRank() == 1) { return new ArrayType(GenerateFromType(type.GetElementType())) { ExtraData = { { SOURCETYPE_KEY, type } } } } ; } if (typeof(IDictionary <,>).IsAssignableFrom(type) || typeof(IDictionary).IsAssignableFrom(type)) { //if (type.IsConstructedGenericType) return(new TypescriptTypeReference( new RawStatements( "{", "[ key: ", GenerateFromType(type.GetGenericArguments()[0]), "]: ", GenerateFromType(type.GetGenericArguments()[1]), "}" ) ) { ExtraData = { { SOURCETYPE_KEY, type } } }); } if (typeof(IEnumerable <>).IsAssignableFrom(type) || typeof(IEnumerable).IsAssignableFrom(type)) { if (type.IsConstructedGenericType) { return new ArrayType(GenerateFromType(type.GetGenericArguments()[0])) { ExtraData = { { SOURCETYPE_KEY, type } } } } ; } if (type.IsEnum) { return(GenerateEnum(type)); } if (type.IsClass || type.IsInterface) { if (type.IsGenericType && !type.IsGenericTypeDefinition) { var tref = GenerateFromType(type.GetGenericTypeDefinition()); foreach (var genericTypeArgument in type.GenericTypeArguments) { if (GenerationStrategy.ShouldGenerateGenericTypeArgument(result, genericTypeArgument)) { tref.GenericParameters.Add(GenerateFromType(genericTypeArgument)); } } return(tref); } return(GenerateObjectDeclaration(type)); } return(new AnyType() { ExtraData = { { SOURCETYPE_KEY, type } } }); }