/// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public virtual string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            if (this.type != null)
            {
                writer.Write("{0} {1} {2} {3}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.VariableDeclaratorKeyword,
                             this.name.Translate(),
                             Lexems.Colon,
                             this.type.Translate());
            }
            else
            {
                writer.Write("{0} {1}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.VariableDeclaratorKeyword,
                             this.name.Translate());
            }

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public virtual string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string fieldVisibility = this.RenderedVisibilityModifier;

            if (this.Expression == null)
            {
                writer.Write("{0}{1} {2} {3}{4}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             this.Visibility.ConvertToTypeScriptEquivalent().EmitOptionalVisibility(),
                             this.RenderedName,
                             Lexems.Colon,
                             this.type.Translate(), Lexems.Newline);
            }
            else
            {
                writer.Write("{0}{1} {2} {3} {4} {5}{6}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             this.Visibility.ConvertToTypeScriptEquivalent().EmitOptionalVisibility(),
                             this.RenderedName,
                             Lexems.Colon,
                             this.type.Translate(),
                             Lexems.EqualsSign,
                             this.Expression.Translate(), Lexems.Newline);
            }

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            writer.WriteLine("{0} {1} {2} {3}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             this.RenderedModuleAccessorKeyword,
                             Lexems.ModuleKeyword,
                             this.name.Translate(),
                             Lexems.OpenCurlyBracket);

            // We render classes first
            var lastClass = this.classes.Count() > 0 ? this.classes.Last() : null;

            foreach (ITranslationUnit translationUnit in this.classes)
            {
                writer.WriteLine(translationUnit.Translate());

                if ((object)translationUnit != (object)lastClass)
                {
                    writer.WriteLine(string.Empty);
                }
            }

            // We render enums next
            var lastEnum = this.enums.Count() > 0 ? this.enums.Last() : null;

            foreach (ITranslationUnit translationUnit in this.enums)
            {
                writer.WriteLine(translationUnit.Translate());

                if ((object)translationUnit != (object)lastEnum)
                {
                    writer.WriteLine(string.Empty);
                }
            }

            // Then, interfaces
            var lastInterface = this.interfaces.Count() > 0 ? this.interfaces.Last() : null;

            foreach (ITranslationUnit translationUnit in this.interfaces)
            {
                writer.WriteLine(translationUnit.Translate());

                if ((object)translationUnit != (object)lastInterface)
                {
                    writer.WriteLine(string.Empty);
                }
            }

            // Closing declaration
            writer.WriteLine("{0}", Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public virtual string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // TODO: Improve this logic to make it more readable and efficient
            if (this.type != null)
            {
                if (this.Expression == null)
                {
                    // [var ]<name> : <type>
                    writer.Write("{0}{1} {2} {3}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.shouldRenderDeclarationKeyword ? "let " : string.Empty,
                                 this.names[0].Translate(),
                                 Lexems.Colon,
                                 this.type.Translate());
                }
                else
                {
                    // [var ]<name> : <type> = <expression>
                    writer.Write("{0}{1} {2} {3} {4} {5}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.shouldRenderDeclarationKeyword ? "let " : string.Empty,
                                 this.names[0].Translate(),
                                 Lexems.Colon,
                                 this.type.Translate(),
                                 Lexems.EqualsSign,
                                 this.Expression.Translate());
                }
            }
            else
            {
                if (this.Expression == null)
                {
                    // var <name>
                    writer.Write("{0} {1}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 Lexems.VariableDeclaratorKeyword,
                                 this.names[0].Translate());
                }
                else
                {
                    // var <name> = <expression>
                    writer.Write("{0} {1} {2} {3}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 Lexems.VariableDeclaratorKeyword,
                                 this.names[0].Translate(),
                                 Lexems.EqualsSign,
                                 this.Expression.Translate());
                }
            }

            return(writer.ToString());
        }
Exemple #5
0
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string enumVisibility = this.RenderedVisibilityModifier;

            if (this.injectedBefore == null)
            {
                writer.WriteLine("{0}{1} {2} {3}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 enumVisibility,
                                 Lexems.EnumKeyword,
                                 this.Name.Translate(),
                                 Lexems.OpenCurlyBracket);
            }
            else
            {
                writer.WriteLine("{0} {1}{2} {3} {4}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.injectedBefore.Translate(),
                                 enumVisibility,
                                 Lexems.EnumKeyword,
                                 this.Name.Translate(),
                                 Lexems.OpenCurlyBracket);
            }

            if (this.ShouldRenderMembers)
            {
                var lastMember = this.members.Count() > 0 ? this.members.Last() : null;
                foreach (ITranslationUnit translationUnit in this.members)
                {
                    if ((object)translationUnit == (object)lastMember)
                    {
                        writer.WriteLine("{0}", translationUnit.Translate());
                    }
                    else
                    {
                        writer.WriteLine("{0}{1}", translationUnit.Translate(), Lexems.Comma);
                    }
                }
            }

            // Closing
            writer.WriteLine("{0}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            writer.WriteLine("{0} {1} {2} {3}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.ExportKeyword,
                             Lexems.ModuleKeyword,
                             this.name.Translate(),
                             Lexems.OpenCurlyBracket);

            // We render classes first
            foreach (ITranslationUnit translationUnit in this.classes)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }

                // Classes need injection for observing indentation
                if (translationUnit as ITranslationInjector != null)
                {
                    ((ITranslationInjector)translationUnit).InjectedTranslationUnitBefore = IdentifierTranslationUnit.Create(Lexems.ExportKeyword);
                }

                writer.WriteLine(translationUnit.Translate());
            }

            // Then, interfaces
            foreach (ITranslationUnit translationUnit in this.interfaces)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }
                writer.WriteLine("{0} {1}",
                                 Lexems.ExportKeyword,
                                 translationUnit.Translate());
            }

            // Closing declaration
            writer.WriteLine("{0}", Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string interfaceVisibility = this.RenderedVisibilityModifier;
            string extensionList       = this.BuildInterfaceExtensionList();

            if (this.injectedBefore == null)
            {
                writer.WriteLine("{0}{1} {2} {3} {4}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 interfaceVisibility,
                                 Lexems.InterfaceKeyword,
                                 this.Name.Translate(),
                                 extensionList,
                                 Lexems.OpenCurlyBracket);
            }
            else
            {
                writer.WriteLine("{0} {1}{2} {3} {4} {5}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.injectedBefore.Translate(),
                                 interfaceVisibility,
                                 Lexems.InterfaceKeyword,
                                 this.Name.Translate(),
                                 extensionList,
                                 Lexems.OpenCurlyBracket);
            }

            // Signatures
            if (this.ShouldRenderSignatures)
            {
                foreach (ITranslationUnit translationUnit in this.signatures)
                {
                    writer.WriteLine("{0}{1}", translationUnit.Translate(), Lexems.Semicolon);
                }
            }

            // Closing
            writer.WriteLine("{0}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }
Exemple #8
0
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public virtual string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            writer.Write("{0} {1} {2} {3}",
                         text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                         TokenUtility.ToString(this.Visibility),
                         this.Name.Translate(),
                         Lexems.Colon,
                         this.type.Translate());

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public virtual string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string fieldVisibility = this.RenderedVisibilityModifier;

            writer.Write("{0}{1} {2} {3}",
                         text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                         this.Modifiers.ConvertToTypeScriptEquivalent().EmitOptionalVisibility(),
                         this.RenderedName,
                         Lexems.Colon,
                         this.type.Translate());

            return(writer.ToString());
        }
Exemple #10
0
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string classVisibility = TokenUtility.ToString(this.Visibility);
            string baseList        = this.BuildClassInheritanceAndInterfaceImplementationList();

            if (this.injectedBefore == null)
            {
                writer.WriteLine("{0} class {1} {2} {3}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 classVisibility,
                                 this.Name.Translate(),
                                 baseList,
                                 Lexems.OpenCurlyBracket);
            }
            else
            {
                writer.WriteLine("{0} {1} class {2} {3} {4}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.injectedBefore.Translate(),
                                 classVisibility,
                                 this.Name.Translate(),
                                 baseList,
                                 Lexems.OpenCurlyBracket);
            }

            // Translating members first
            foreach (ITranslationUnit translationUnit in this.memberDeclarations)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }
                writer.WriteLine("{0}{1}", translationUnit.Translate(), Lexems.Semicolon);
            }

            // Then constructors
            foreach (ITranslationUnit translationUnit in this.constructorDeclarations)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }
                writer.WriteLine(translationUnit.Translate());
            }

            // Then properties
            foreach (ITranslationUnit translationUnit in this.propertyDeclarations)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }
                writer.WriteLine(translationUnit.Translate());
            }

            // Finally methods
            foreach (ITranslationUnit translationUnit in this.methodDeclarations)
            {
                if (translationUnit as NestedElementTranslationUnit != null)
                {
                    ((NestedElementTranslationUnit)translationUnit).NestingLevel = this.NestingLevel + 1;
                }
                writer.WriteLine(translationUnit.Translate());
            }

            // Closing
            writer.WriteLine("{0}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }
        /// <summary>
        /// Translate the unit into TypeScript.
        /// </summary>
        /// <returns></returns>
        public string Translate()
        {
            FormatWriter writer = new FormatWriter()
            {
                Formatter = this.Formatter
            };

            // Opening declaration
            string classVisibility = this.RenderedVisibilityModifier;
            string baseList        = this.BuildClassInheritanceAndInterfaceImplementationList();

            if (this.injectedBefore == null)
            {
                writer.WriteLine("{0}{1} {2} {3} {4}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 classVisibility,
                                 Lexems.ClassKeyword,
                                 this.Name.Translate(),
                                 baseList,
                                 Lexems.OpenCurlyBracket);
            }
            else
            {
                writer.WriteLine("{0} {1}{2} {3} {4} {5}",
                                 text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                                 this.injectedBefore.Translate(),
                                 classVisibility,
                                 Lexems.ClassKeyword,
                                 this.Name.Translate(),
                                 baseList,
                                 Lexems.OpenCurlyBracket);
            }

            // Translating members first
            foreach (ITranslationUnit translationUnit in this.memberDeclarations)
            {
                writer.WriteLine("{0}{1}", translationUnit.Translate(), Lexems.Semicolon);
            }

            // Adding a newline
            if (this.memberDeclarations.Count() > 0 && (
                    this.constructorDeclarations.Count() > 0 ||
                    this.propertyDeclarations.Count() > 0 ||
                    this.methodDeclarations.Count() > 0))
            {
                writer.WriteLine(string.Empty);
            }

            // Then constructors
            foreach (ITranslationUnit translationUnit in this.constructorDeclarations)
            {
                writer.WriteLine("{0}{1}", translationUnit.Translate(), this.RenderedConstructorDeclarationAfterSeparator);
            }

            // Adding a newline
            if (this.constructorDeclarations.Count() > 0 && (
                    this.propertyDeclarations.Count() > 0 ||
                    this.methodDeclarations.Count() > 0))
            {
                writer.WriteLine(string.Empty);
            }

            // Then properties
            foreach (ITranslationUnit translationUnit in this.propertyDeclarations)
            {
                writer.WriteLine("{0}{1}", translationUnit.Translate(), this.RenderedPropertyDeclarationAfterSeparator);
            }

            // Adding a newline
            if (this.propertyDeclarations.Count() > 0 && this.methodDeclarations.Count() > 0)
            {
                writer.WriteLine(string.Empty);
            }

            // Finally methods
            foreach (ITranslationUnit translationUnit in this.methodDeclarations)
            {
                writer.WriteLine("{0}{1}", translationUnit.Translate(), this.RenderedMethodDeclarationAfterSeparator);
            }

            // Closing
            writer.WriteLine("{0}",
                             text => ClassDeclarationCodePerfect.RefineDeclaration(text),
                             Lexems.CloseCurlyBracket);

            return(writer.ToString());
        }