Exemple #1
0
        /// <summary>
        /// Maps a type into another.
        /// </summary>
        /// <param name="originalType">The original type.</param>
        /// <returns>The new type, or the original type if no mapping is possible.</returns>
        public static string MapType(this TypeSyntax originalTypeSyntax)
        {
            //  ArrayTypeSyntax
            //  NameSyntax
            //NullableTypeSyntax
            //OmittedTypeArgumentSyntax
            //PointerTypeSyntax
            //PredefinedTypeSyntax
            //RefTypeSyntax
            //TupleTypeSyntax

            string originalType = null;

            switch (originalTypeSyntax.Kind())
            {
            case SyntaxKind.PredefinedType:
                var predefinedSyntaxNode = originalTypeSyntax as PredefinedTypeSyntax;

                originalType = predefinedSyntaxNode.Keyword.Text;

                break;

            case SyntaxKind.GenericName:
                var genericNameSyntaxNode = originalTypeSyntax as GenericNameSyntax;

                originalType = genericNameSyntaxNode.Identifier.ValueText;

                break;

            case SyntaxKind.IdentifierName:
                var simpleNameSyntaxNode = originalTypeSyntax as SimpleNameSyntax;

                originalType = simpleNameSyntaxNode.Identifier.ValueText;
                break;

            case SyntaxKind.ArrayType:
                var arrayTypeSyntaxNode = originalTypeSyntax as ArrayTypeSyntax;

                originalType = arrayTypeSyntaxNode.ElementType.MapType();
                break;

            case SyntaxKind.NullableType:
                var nullableTypeSyntaxNode = originalTypeSyntax as NullableTypeSyntax;

                originalType = nullableTypeSyntaxNode.ElementType.MapType();
                break;

            case SyntaxKind.QualifiedName:
                var qualifiedNameSyntaxNode = originalTypeSyntax as QualifiedNameSyntax;

                originalType = qualifiedNameSyntaxNode.Right.MapType();

                //TODO should we handle the left? or leave it up to TypeScript cleanup?

                break;

            case SyntaxKind.RefType:
            case SyntaxKind.TupleType:
            default:
                throw new NotImplementedException();

                //break;
            }

            if (IsVoid(originalType))
            {
                originalType = Lexems.VoidReturnType;
            }

            if (IsString(originalType))
            {
                originalType = Lexems.StringType;
            }

            if (IsInt(originalType))
            {
                originalType = Lexems.NumberType;
            }

            if (IsDouble(originalType))
            {
                originalType = Lexems.NumberType;
            }

            if (IsFloat(originalType))
            {
                originalType = Lexems.NumberType;
            }

            if (IsBool(originalType))
            {
                originalType = Lexems.BooleanType;
            }

            if (IsKnockoutObservable(originalType))
            {
                originalType = "KnockoutObservable";
            }

            if (IsKnockoutObservableArray(originalType))
            {
                originalType = "KnockoutObservableArray";
            }

            if (IsKnockoutComputedObservable(originalType))
            {
                originalType = "KnockoutComputed";
            }

            if (IsObject(originalType))
            {
                originalType = Lexems.AnyType;
            }

            switch (originalTypeSyntax.Kind())
            {
            case SyntaxKind.GenericName:
                var genericNameSyntaxNode = originalTypeSyntax as GenericNameSyntax;

                originalType = originalType +
                               SyntaxUtility.ToAngleBracketEnclosedList(genericNameSyntaxNode.TypeArgumentList.Arguments.Select(unit => unit.MapType()));

                break;

            case SyntaxKind.ArrayType:
                var arrayTypeSyntaxNode = originalTypeSyntax as ArrayTypeSyntax;

                originalType = originalType + "[]";

                break;

            case SyntaxKind.NullableType:
                var nullableTypeSyntaxNode = originalTypeSyntax as NullableTypeSyntax;

                originalType = originalType + "|null";

                break;

            default:
                break;
            }

            return(originalType);
        }