/// <summary>
 /// Formats an enumeration
 /// </summary>
 /// <param name="tsEnum">The enumeration</param>
 /// <returns>The string representation of the enumeration</returns>
 public virtual string Format(TsEnum tsEnum)
 {
     if (EnumsAsString)
     {
         return(FormatEnumAsStrings(tsEnum));
     }
     else
     {
         return(FormatEnumAsIntegers(tsEnum));
     }
 }
        /// <summary>
        /// Generates a TypeScript enum for a particular CLR enum type
        /// </summary>
        /// <param name="type">The enum type</param>
        /// <returns>The resulting TypeScript enum</returns>
        private TsEnum GenerateEnum(ITypeSymbol type)
        {
            var fields  = type.GetMembers().OfType <IFieldSymbol>();
            var entries = new Dictionary <string, long?>();

            long lastValue = 0;

            foreach (var field in fields)
            {
                var syntaxReference = field.DeclaringSyntaxReferences.FirstOrDefault();
                if (syntaxReference == null)
                {
                    //If syntaxReference is null then the enum comes from another projects which are not in the current
                    //Project.

                    return(new TsEnum(GetName(type), entries, true));
                }
                var parent    = _compilation.GetSemanticModel(syntaxReference.SyntaxTree);
                var syn       = syntaxReference.GetSyntax();
                var enumValue = syn.DescendantNodes().OfType <LiteralExpressionSyntax>().SingleOrDefault();
                if (enumValue != null)
                {
                    Optional <object> constantValue = parent.GetConstantValue(enumValue);
                    if (constantValue.HasValue)
                    {
                        lastValue = Convert.ToInt64(constantValue.Value);
                        entries.Add(field.Name, lastValue);
                    }
                    else
                    {
                        entries.Add(field.Name, ++lastValue);
                    }
                }
                else
                {
                    entries.Add(field.Name, ++lastValue);
                }
            }

            //The Enum can be defined within a class. In that case the class is a
            //namespace and the enum will be generated in another module (Since we cannot
            //define an enum within a typescript interface.
            bool isExternallyDefined = !(type.ContainingSymbol is INamespaceSymbol);

            var tsEnum = new TsEnum(GetName(type), entries, isExternallyDefined);

            AddType(tsEnum, type);
            return(tsEnum);
        }
Beispiel #3
0
        /// <summary>
        /// Generates a TypeScript enum for a particular CLR enum type
        /// </summary>
        /// <param name="type">The enum type</param>
        /// <returns>The resulting TypeScrpt enum</returns>
        private TsEnum GenerateEnum(Type type)
        {
            var typeInfo = type.GetTypeInfo();
            var names    = typeInfo.GetEnumNames();
            var values   = typeInfo.GetEnumValues();
            var entries  = new Dictionary <string, long?>();

            for (int i = 0; i < values.Length; i++)
            {
                entries.Add(names[i], Convert.ToInt64(values.GetValue(i)));
            }
            var tsEnum = new TsEnum(GetName(type), entries);

            this.AddType(tsEnum, type);
            return(tsEnum);
        }
 /// <summary>
 /// Formats an enumeration as string
 /// </summary>
 /// <param name="tsEnum">The enumeration</param>
 /// <returns>The string representation of the enumeration</returns>
 protected string FormatEnumAsStrings(TsEnum tsEnum)
 {
     using (var sbc = new StringBuilderContext(this))
     {
         WriteIndent();
         Write("type {0} = ", Format(tsEnum.Name));
         var values = tsEnum.Values.OrderBy(x => x.Key).ToArray();
         for (int i = 0; i < values.Length; i++)
         {
             var postFix = i < values.Length - 1 ? " | " : string.Empty;
             var entry   = values[i];
             Write("\'{0}\'{1}", entry.Key, postFix);
         }
         Write(";");
         WriteNewline();
         return(sbc.ToString());
     }
 }