static bool WriteCodeMemberMethod(CodeMemberMethod memberMethod, TextWriter w, CodeGeneratorOptions o) { if (memberMethod == null) { return(false); } var isCodeConstructor = memberMethod is CodeConstructor; var methodName = isCodeConstructor ? "constructor" : memberMethod.Name; w.Write(o.IndentString + methodName + "("); WriteCodeParameterDeclarationExpressionCollection(memberMethod.Parameters, w); w.Write(")"); var returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(memberMethod.ReturnType); if (!(isCodeConstructor || returnTypeText == "void" || memberMethod.ReturnType == null)) { w.Write(": " + returnTypeText); } w.WriteLine("{"); WriteCodeStatementCollection(memberMethod.Statements, w, o); w.WriteLine(o.IndentString + "}"); return(true); }
static void WriteCodeParameterDeclarationExpressionCollection(CodeParameterDeclarationExpressionCollection parameterDeclarations, TextWriter w) { var pairs = parameterDeclarations.OfType <CodeParameterDeclarationExpression>() .Select(d => { var s = $"{d.Name}: {TypeMapper.MapCodeTypeReferenceToTsText(d.Type)}"; Debug.WriteLine("vvvv " + s); return(s); }); w.Write(String.Join(", ", pairs)); }
static bool WriteCodeObjectCreateExpression(CodeObjectCreateExpression objectCreateExpression, TextWriter w, CodeGeneratorOptions o) { if (objectCreateExpression == null) { return(false); } w.Write($"new {TypeMapper.MapCodeTypeReferenceToTsText(objectCreateExpression.CreateType)}("); WriteCodeExpressionCollection(objectCreateExpression.Parameters, w, o); w.Write(")"); return(true); }
static string GetBaseTypeExpression(CodeTypeDeclaration typeDeclaration) { var baseTypes = typeDeclaration.BaseTypes .OfType <CodeTypeReference>() .Where(reference => TypeMapper.IsValidTypeForDerivation(reference)) .Select(reference => TypeMapper.MapCodeTypeReferenceToTsText(reference)) .ToList(); if (baseTypes.Any() && !typeDeclaration.IsEnum) { return($" extends {string.Join(",", baseTypes)}"); } return(String.Empty); }
static bool WriteCodeVariableDeclarationStatement(CodeVariableDeclarationStatement variableDeclarationStatement, TextWriter w, CodeGeneratorOptions o) { if (variableDeclarationStatement == null) { return(false); } w.Write(o.IndentString); w.Write($"var {variableDeclarationStatement.Name}: {TypeMapper.MapCodeTypeReferenceToTsText(variableDeclarationStatement.Type)}"); if (variableDeclarationStatement.InitExpression != null) { w.Write(" = "); GenerateCodeFromExpression(variableDeclarationStatement.InitExpression, w, o); } return(true); }
static string GetTypeParametersExpression(CodeTypeDeclaration typeDeclaration) { if (typeDeclaration.TypeParameters.Count == 0) { return(string.Empty); } var parameterNames = typeDeclaration.TypeParameters.OfType <CodeTypeParameter>().Select(d => { var typeParameterConstraint = string.Empty; if (d.Constraints.Count > 0) { var constraint = d.Constraints.OfType <CodeTypeReference>().First(); var type = TypeMapper.MapCodeTypeReferenceToTsText(constraint); typeParameterConstraint = $" extends {type}"; } return($"{d.Name}{typeParameterConstraint}"); }).ToArray(); return(parameterNames.Length == 0 ? String.Empty : $"<{String.Join(", ", parameterNames)}>"); }
//http://www.codebelt.com/typescript/javascript-getters-setters-typescript-accessor-tutorial/ static bool WriteCodeMemberProperty(CodeMemberProperty codeMemberProperty, TextWriter w, CodeGeneratorOptions o) { if (codeMemberProperty == null) { return(false); } WriteCodeCommentStatementCollection(codeMemberProperty.Comments, w, o); var accessibility = GetAccessibilityModifier(codeMemberProperty.Attributes); var currentIndent = o.IndentString; var propertyType = TypeMapper.MapCodeTypeReferenceToTsText(codeMemberProperty.Type); if (codeMemberProperty.GetStatements.Count > 0) { w.Write(o.IndentString); w.WriteLine($"{accessibility} get {codeMemberProperty.Name}(): {propertyType}"); w.WriteLine("{"); o.IndentString += BasicIndent; WriteCodeStatementCollection(codeMemberProperty.GetStatements, w, o); o.IndentString = currentIndent; } if (codeMemberProperty.SetStatements.Count > 0) { w.Write(o.IndentString); w.WriteLine($"{accessibility} set {codeMemberProperty.Name}(value : {propertyType})"); w.WriteLine("{"); o.IndentString += BasicIndent; WriteCodeStatementCollection(codeMemberProperty.SetStatements, w, o); o.IndentString = currentIndent; } return(true); }
internal static void GenerateCodeFromExpression(CodeExpression e, TextWriter w, CodeGeneratorOptions o) { if (e == null) { return; } var argumentReferenceExpression = e as CodeArgumentReferenceExpression; if (argumentReferenceExpression != null) { w.Write(argumentReferenceExpression.ParameterName); return; } if (WriteCodeArrayCreateExpression(e as CodeArrayCreateExpression, w, o)) { return; } if (WriteCodeArrayIndexerExpression(e as CodeArrayIndexerExpression, w, o)) { return; } var baseReferenceExpression = e as CodeBaseReferenceExpression; if (baseReferenceExpression != null) { w.Write("super"); return; } if (WriteCodeBinaryOperatorExpression(e as CodeBinaryOperatorExpression, w, o)) { return; } if (WriteCodeFieldReferenceExpression(e as CodeFieldReferenceExpression, w, o)) { return; } if (WriteCodeIndexerExpression(e as CodeIndexerExpression, w, o)) { return; } if (WriteCodeMethodInvokeExpression(e as CodeMethodInvokeExpression, w, o)) { return; } var methodReferenceExpression = e as CodeMethodReferenceExpression; if (methodReferenceExpression != null) { WriteCodeMethodReferenceExpression(methodReferenceExpression, w, o); return; } if (WriteCodeObjectCreateExpression(e as CodeObjectCreateExpression, w, o)) { return; } var parameterDeclarationExpression = e as CodeParameterDeclarationExpression; if (parameterDeclarationExpression != null) { w.Write($"{parameterDeclarationExpression.Name}: {TypeMapper.MapCodeTypeReferenceToTsText(parameterDeclarationExpression.Type)}"); return; } if (WriteCodePrimitiveExpression(e as CodePrimitiveExpression, w)) { return; } if (WriteCodePropertyReferenceExpression(e as CodePropertyReferenceExpression, w, o)) { return; } var snippetExpression = e as CodeSnippetExpression; if (snippetExpression != null) { w.Write(snippetExpression.Value); return; } var thisReferenceExpression = e as CodeThisReferenceExpression; if (thisReferenceExpression != null) { w.Write("this"); return; } var typeOfExpression = e as CodeTypeOfExpression; if (typeOfExpression != null) { w.Write("typeof " + TypeMapper.MapCodeTypeReferenceToTsText(typeOfExpression.Type)); return; } var typeReferenceExpression = e as CodeTypeReferenceExpression; if (typeReferenceExpression != null) { w.Write(TypeMapper.MapCodeTypeReferenceToTsText(typeReferenceExpression.Type)); return; } var variableReferenceExpression = e as CodeVariableReferenceExpression; if (variableReferenceExpression != null) { w.Write(variableReferenceExpression.VariableName); return; } Trace.TraceWarning($"CodeExpression not supported: {e.ToString()}"); }
static string GetCodeTypeReferenceText(CodeTypeReference codeTypeReference) { return(TypeMapper.MapCodeTypeReferenceToTsText(codeTypeReference)); }
string ICodeGenerator.GetTypeOutput(CodeTypeReference type) { return(TypeMapper.MapCodeTypeReferenceToTsText(type)); }