Example #1
0
        public void CreateImports()
        {
            TypeScriptProvider tsProvider = new TypeScriptProvider();

            TsCodeDom.Entities.TsCodeNamespace newNamespace = new Entities.TsCodeNamespace();
            // we don't want to use a namespace for the generated interface (so we are not setting the namespace name)
            //add import all (alternative 1) (simply not setting an import type)
            newNamespace.Imports.Add(new Entities.TsCodeNamespaceImport()
            {
                Path = "@scope/package"
            });

            //add import all (alternative 2)
            var importWithTypes = new Entities.TsCodeNamespaceImport()
            {
                Path = "@scope/package"
            };

            importWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                //dont set the name to import all
            });
            newNamespace.Imports.Add(importWithTypes);

            //add import all as
            importWithTypes = new Entities.TsCodeNamespaceImport()
            {
                Path = "@scope/package"
            };
            importWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                //dont set the name to import all
                Alias = "myImport"
            });
            newNamespace.Imports.Add(importWithTypes);

            //add import with specific types
            importWithTypes = new Entities.TsCodeNamespaceImport()
            {
                Path = "@scope/package"
            };
            importWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                Name = "Cat"
            });
            importWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                Name  = "Dog",
                Alias = "Wuffi"
            });
            newNamespace.Imports.Add(importWithTypes);

            var tsCode = tsProvider.GenerateCodeFromNamespace(newNamespace);

            Assert.AreEqual(tsCode, "import * from '@scope/package';\r\nimport * from '@scope/package';\r\nimport * as myImport from '@scope/package';\r\nimport {Cat, Dog as Wuffi} from '@scope/package';\r\n");
        }
Example #2
0
        public void CreateExports()
        {
            TypeScriptProvider tsProvider = new TypeScriptProvider();

            Entities.TsCodeNamespace newNamespace = new Entities.TsCodeNamespace();
            //add export all (alternative 1) (simply not setting an import type)
            newNamespace.Imports.Add(new Entities.TsCodeNamespaceImport()
            {
                Path     = "@scope/package",
                IsExport = true
            });

            //add export all (alternative 2)
            var exportWithTypes = new Entities.TsCodeNamespaceImport()
            {
                Path     = "@scope/package",
                IsExport = true
            };

            exportWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                //dont set the name to import all
            });
            newNamespace.Imports.Add(exportWithTypes);

            //export multiple types
            exportWithTypes = new Entities.TsCodeNamespaceImport()
            {
                Path     = "@scope/package",
                IsExport = true
            };
            exportWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                Name = "Dog"
            });
            exportWithTypes.ImportTypes.Add(new Entities.TsCodeImportType()
            {
                Name = "Cat"
            });
            newNamespace.Imports.Add(exportWithTypes);

            var tsCode = tsProvider.GenerateCodeFromNamespace(newNamespace);

            Assert.AreEqual(tsCode, "export * from '@scope/package';\r\nexport * from '@scope/package';\r\nexport {Cat, Dog} from '@scope/package';\r\n");
        }
Example #3
0
        public void CreateClass_WithMethod()
        {
            TypeScriptProvider tsProvider = new TypeScriptProvider();

            TsCodeDom.Entities.TsCodeNamespace newNamespace = new Entities.TsCodeNamespace();

            // create a public abstract class
            var classType = new Entities.TsCodeTypeDeclaration("MyClass")
            {
                ElementType = Enumerations.TsElementTypes.Class,
                Attributes  = Enumerations.TsTypeAttributes.Public
            };

            var myMethod = new TsCodeMemberMethod()
            {
                Name       = "myMethod",
                Attributes = Enumerations.TsMemberAttributes.Private,
                // type void
                ReturnType = new TsCodeTypeReference()
            };

            // add console statement
            var consoleLogExpression = new TsCodeMethodInvokeExpression()
            {
                Method = new TsCodeMethodReferenceExpression()
                {
                    MethodName   = "log",
                    TargetObject = new TsCodeVariableReferenceExpression("console")
                }
            };

            consoleLogExpression.Parameters.Add(new TsCodePrimitiveExpression("hello world"));
            myMethod.Statements.Add(new TsCodeExpressionStatement(consoleLogExpression));
            classType.Members.Add(myMethod);

            newNamespace.Types.Add(classType);
            var tsCode = tsProvider.GenerateCodeFromNamespace(newNamespace);
        }
Example #4
0
        public void CreateInterface()
        {
            TypeScriptProvider tsProvider = new TypeScriptProvider();

            TsCodeDom.Entities.TsCodeNamespace newNamespace = new Entities.TsCodeNamespace();

            // create interface
            TsCodeDom.Entities.TsCodeTypeDeclaration interfaceType = new Entities.TsCodeTypeDeclaration("myInterface");
            interfaceType.ElementType = TsElementTypes.Interface;

            // add base type
            interfaceType.BaseTypes.Add(new Entities.TsCodeTypeReference("IBase"));

            //add a comment
            interfaceType.Comments.Add(new Entities.TsCodeCommentStatement("This is myInterface description"));

            // we want to export the interface so we set the public attribute
            interfaceType.Attributes = TsTypeAttributes.Public;

            // setting a decorator
            interfaceType.Decorators.Add(new Entities.TsCodeAttributeDeclaration()
            {
                Name = "MyDecorator"
            });

            //add field members
            var field = new TsCodeDom.Entities.TsCodeMemberField()
            {
                Name = "myField"
            };

            field.Comments.Add(new Entities.TsCodeCommentStatement("This is my property"));

            // field can have multiple types
            field.Types.Add(new Entities.TsCodeTypeReference(typeof(string)));
            field.Types.Add(new Entities.TsCodeTypeReference(typeof(int)));
            field.Types.Add(new Entities.TsCodeTypeReference("Cat"));
            interfaceType.Members.Add(field);

            // method
            var method = new Entities.TsCodeMemberMethod()
            {
                Name = "myMethod"
            };

            method.Comments.Add(new Entities.TsCodeCommentStatement("This is my method"));

            // add method parameter
            var methodParameter = new Entities.TsCodeParameterDeclarationExpression("myParam")
            {
                IsNullable = true
            };

            methodParameter.Types.Add(new Entities.TsCodeTypeReference(typeof(DateTime)));
            method.Parameters.Add(methodParameter);

            method.ReturnType = new Entities.TsCodeTypeReference(typeof(string));
            interfaceType.Members.Add(method);


            newNamespace.Types.Add(interfaceType);
            var tsCode = tsProvider.GenerateCodeFromNamespace(newNamespace);

            Assert.AreEqual(tsCode, "\r\n/** This is myInterface description */\r\n@MyDecorator()\r\nexport interface myInterface extends IBase {\r\n    /** This is my property */\r\n    myField: string | number | Cat;\r\n\r\n    /** This is my method */\r\n    myMethod(myParam?: Date): string;\r\n}\r\n");
        }
Example #5
0
        public void CreateClass_WithProperty()
        {
            TypeScriptProvider tsProvider = new TypeScriptProvider();

            TsCodeDom.Entities.TsCodeNamespace newNamespace = new Entities.TsCodeNamespace();

            // create a public abstract class
            var classType = new Entities.TsCodeTypeDeclaration("MyClass")
            {
                ElementType = Enumerations.TsElementTypes.Class,
                Attributes  = Enumerations.TsTypeAttributes.Abstract | Enumerations.TsTypeAttributes.Public
            };

            // add comment
            classType.Comments.Add(new Entities.TsCodeCommentStatement("This is my class"));

            // add decorator
            classType.Decorators.Add(new Entities.TsCodeAttributeDeclaration()
            {
                Name = "MyDecorator"
            });

            // inherit from class
            classType.BaseTypes.Add(new Entities.TsCodeTypeReference("MyBaseClass"));
            // implement interface
            classType.BaseTypes.Add(new Entities.TsCodeTypeReference("IBase", Enumerations.TsElementTypes.Interface));


            var myPropertyField = new Entities.TsCodeMemberField()
            {
                Name = "_myProperty",
                //Attributes = Enumerations.TsMemberAttributes.Private,
                TypeAttributes = Enumerations.TsTypeAttributes.Private
            };

            myPropertyField.Types.Add(new Entities.TsCodeTypeReference(typeof(string)));
            myPropertyField.InitStatement = new Entities.TsCodePrimitiveExpression("init value");
            classType.Members.Add(myPropertyField);


            //add property
            var myProperty = new Entities.TsCodeMemberProperty("myProperty");

            myProperty.Types.Add(new Entities.TsCodeTypeReference(typeof(string)));
            myProperty.Attributes       = Enumerations.TsMemberAttributes.Protected;
            myProperty.TypeAttributes   = Enumerations.TsTypeAttributes.Readonly;
            myProperty.SetParameterName = "value";
            myProperty.HasGet           = true;
            myProperty.HasSet           = true;

            //helper: reference to this.myField (comine fieldname with this operator)
            var thisFieldExpression = new TsCodeFieldReferenceExpression()
            {
                FieldName    = myPropertyField.Name,
                TargetObject = new TsCodeThisReferenceExpression()
            };

            //define getter return statements
            var getterReturnStatement = new TsCodeMethodReturnStatement()
            {
                Expression = thisFieldExpression
            };

            myProperty.GetStatements.Add(getterReturnStatement);

            //define setter statement
            var setterStatement = new TsCodeAssignStatement();

            setterStatement.Left  = new TsCodeVariableReferenceExpression(myPropertyField.Name);
            setterStatement.Right = new TsCodeVariableReferenceExpression(myProperty.SetParameterName);

            myProperty.SetStatements.Add(setterStatement);

            classType.Members.Add(myProperty);


            newNamespace.Types.Add(classType);
            var tsCode = tsProvider.GenerateCodeFromNamespace(newNamespace);

            Assert.AreEqual(tsCode, "\r\n/** This is my class */\r\n@MyDecorator()\r\nexport abstract class MyClass extends MyBaseClass implements IBase {\r\n    private _myProperty: string = 'init value';\r\n    protected get myProperty(): string{\r\n        return this._myProperty;\r\n    }\r\n    protected set myProperty(value: string){\r\n        _myProperty = value;\r\n    }\r\n}\r\n");
        }