/// <summary> /// Main code generator method. This method should write corresponding TypeScript code for element (1st argument) to /// WriterWrapper (3rd argument) using TypeResolver if necessary /// </summary> /// <param name="element">Element code to be generated to output</param> /// <param name="result">Resulting node</param> /// <param name="resolver">Type resolver</param> public override RtField GenerateNode(MemberInfo element, RtField result, TypeResolver resolver) { var b = base.GenerateNode(element, result, resolver); if (b == null) { return(null); } var pascalCaseName = b.Identifier.IdentifierName.Substring(0, 1).ToUpperInvariant() + b.Identifier.IdentifierName.Substring(1); var newField = new RtField() { AccessModifier = b.AccessModifier, Identifier = new RtIdentifier(pascalCaseName), InitializationExpression = b.InitializationExpression, IsStatic = b.IsStatic, Documentation = b.Documentation, Order = b.Order, Type = b.Type }; if (Context.Location.CurrentClass != null) { Context.Location.CurrentClass.Members.Add(newField); } if (Context.Location.CurrentInterface != null) { Context.Location.CurrentInterface.Members.Add(newField); } return(b); }
private IEnumerable <RtNode> GetImplementationMembers(RtInterface result, string hub) { List <RtNode> members = new List <RtNode>(); IEnumerable <RtFunction> functions = result.Members.OfType <RtFunction>(); foreach (RtFunction function in functions) { List <string> rtTypeNames = function.Arguments.Select(a => a.Type.ToString()).ToList(); string generics = string.Join(",", rtTypeNames); if (rtTypeNames.Count > 1) { generics = $"[{generics}]"; } string stringType = rtTypeNames.Count == 0 ? "() => void" : $"SimpleEventDispatcher<{generics}>"; RtField eventDispatcher = new RtField { AccessModifier = AccessModifier.Public, Identifier = new RtIdentifier($"on{function.Identifier.ToString().FirstCharToUpper()}"), Type = new RtSimpleTypeName(stringType), InitializationExpression = $"new {stringType}()" }; members.Add(eventDispatcher); } return(members); }
public override void Visit(RtField node) { if (node == null) { return; } Visit(node.Documentation); AppendTabs(); if (Context == WriterContext.Module) { Write("export var "); } if (Context == WriterContext.None) { Write("declare var "); } if (Context == WriterContext.Class) { Modifiers(node); } Visit(node.Identifier); Write(": "); Visit(node.Type); WriteLine(";"); if (!string.IsNullOrEmpty(node.LineAfter)) { AppendTabs(); Write(node.LineAfter); Br(); } }
public override void Visit(RtField node) { if (node == null) { return; } Visit(node.Documentation); AppendTabs(); if (Context != WriterContext.Interface) { Decorators(node); Modifiers(node); } Visit(node.Identifier); Write(": "); Visit(node.Type); if (!string.IsNullOrEmpty(node.InitializationExpression)) { Write(" = "); Write(node.InitializationExpression); } Write(";"); Br(); if (!string.IsNullOrEmpty(node.LineAfter)) { AppendTabs(); Write(node.LineAfter); Br(); } }
private ReflectionAttachedRtField(RtField actual) { this.Identifier = actual.Identifier; this.Type = actual.Type; this.InitializationExpression = actual.InitializationExpression; foreach (var decorator in actual.Decorators) { Decorators.Add(decorator); } this.CopyActualProperties(actual); }
public override RtField GenerateNode(MemberInfo element, RtField result, TypeResolver resolver) { var generated = base.GenerateNode(element, result, resolver); var propertyType = (element as PropertyInfo).PropertyType; if (propertyType.IsNullable()) { generated.Documentation = new RtJsdocNode() { Description = "Nullable" }; } return(generated); }
public override RtField GenerateNode(MemberInfo element, RtField result, TypeResolver resolver) { if (!ProducedNullableType) { if (element.GetCustomAttribute(typeof(NullablePropertyAttribute)) != null) { if (Context.Location.CurrentNamespace != null) { Context.Location.CurrentNamespace.CompilationUnits.Add(new RtRaw("type Nullable<T> = T | null")); ProducedNullableType = true; } } } // actually no need for the attribute - can use the Type property - RtTypeName and change it return(base.GenerateNode(element, result, resolver)); }
public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver) { result = base.GenerateNode(element, result, resolver); if (result == null) return null; // We add some docs to keep you oriented result.Documentation = new RtJsdocNode(){Description = "Result of AngularControllerGenerator activity"}; // Here we just create ng.IHttpService type name because it is used several times var httpServiceType = new RtSimpleTypeName("IHttpService") { Namespace = "angular" }; // Here we are declaring constructor for our angular service using $http as parameter // It is quite simple so no more details RtConstructor constructor = new RtConstructor(); constructor.Arguments.Add(new RtArgument(){Type = httpServiceType,Identifier = new RtIdentifier("$http")}); constructor.Body = new RtRaw("this.http = $http;"); // Here we declaring class field for storing $http instance RtField httpServiceField = new RtField() { Type = httpServiceType, Identifier = new RtIdentifier("http"), AccessModifier = AccessModifier.Private, Documentation = new RtJsdocNode() { Description = "Keeps $http instance received on construction"} }; // Here we are adding our constructor and field to resulting class result.Members.Add(httpServiceField); result.Members.Add(constructor); // Also we will add controller registration to our app instance // To automatically get it registered in Angular's IoC const string initializerFormat = "if (window['app']) window['app'].factory('Api.{0}', ['$http', ($http: angular.IHttpService) => new {1}($http)]);"; RtRaw registration = new RtRaw(String.Format(initializerFormat,element.Name,result.Name)); // Since RtModule.compilationUnits is not typed and could contain any type then we // simply add RtRaw node here with registration glue code // app variable is declared in /Scripts/ReinforcedTypings/App.ts since it is used in // corresponding client script Context.Location.CurrentModule.CompilationUnits.Add(registration); // That's all. here we return node that will be written to target file. // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts return result; }
public override void Visit(RtField node) { var reflectionAttachedRtField = node as ReflectionAttachedRtField; if (reflectionAttachedRtField != null && !reflectionAttachedRtField.Handled) { reflectionAttachedRtField.Handled = true; if (reflectionAttachedRtField.PropertyInfo != null) { ReflectionProperty(reflectionAttachedRtField); } else { ReflectionField(reflectionAttachedRtField); } } }
private IEnumerable <RtNode> GetImplementationMembers(RtInterface result) { var members = new List <RtNode>(); var functions = result.Members.OfType <RtFuncion>(); foreach (var function in functions) { var rtTypeNames = function.Arguments.Select(a => a.Type.ToString()).ToList(); var generics = string.Join(",", rtTypeNames); if (rtTypeNames.Count > 1) { generics = $"[{generics}]"; } var stringType = rtTypeNames.Count == 0 ? "SignalDispatcher" : $"SimpleEventDispatcher<{generics}>"; var eventDispatcher = new RtField { AccessModifier = AccessModifier.Public, Identifier = new RtIdentifier($"on{function.Identifier.ToString().FirstCharToUpper()}"), Type = new RtSimpleTypeName(stringType), InitializationExpression = $"new {stringType}()" }; members.Add(eventDispatcher); } members.Add(new RtConstructor { Arguments = { new RtArgument { Type = new RtSimpleTypeName("HubConnectionProvider"), Identifier = new RtIdentifier("hubConnectionProvider") } }, Body = GetEventRegistrationBody(functions) }); return(members); }
public abstract T Visit(RtField node);
public override RtField GenerateNode(MemberInfo element, RtField result, TypeResolver resolver) { Context.Warnings.Add(new Reinforced.Typings.Exceptions.RtWarning(0, "Log", "Hello!")); return(base.GenerateNode(element, result, resolver)); }
public ReflectionAttachedRtField(RtField actual, FieldInfo fieldInfo) : this(actual) { FieldInfo = fieldInfo; }
public ReflectionAttachedRtField(RtField actual, PropertyInfo propertyInfo) : this(actual) { PropertyInfo = propertyInfo; }
public override RtClass GenerateNode(Type element, RtClass result, TypeResolver resolver) { result = base.GenerateNode(element, result, resolver); if (result == null) { return(null); } // We add some docs to keep you oriented result.Documentation = new RtJsdocNode() { Description = "Result of AngularControllerGenerator activity" }; // Here we just create ng.IHttpService type name because it is used several times var httpServiceType = new RtSimpleTypeName("IHttpService") { Namespace = "angular" }; // Here we are declaring constructor for our angular service using $http as parameter // It is quite simple so no more details RtConstructor constructor = new RtConstructor(); constructor.Arguments.Add(new RtArgument() { Type = httpServiceType, Identifier = new RtIdentifier("$http") }); constructor.Body = new RtRaw("this.http = $http;"); // Here we declaring class field for storing $http instance RtField httpServiceField = new RtField() { Type = httpServiceType, Identifier = new RtIdentifier("http"), AccessModifier = AccessModifier.Private, Documentation = new RtJsdocNode() { Description = "Keeps $http instance received on construction" } }; // Here we are adding our constructor and field to resulting class result.Members.Add(httpServiceField); result.Members.Add(constructor); // Also we will add controller registration to our app instance // To automatically get it registered in Angular's IoC const string initializerFormat = "if (window['app']) window['app'].factory('Api.{0}', ['$http', ($http: angular.IHttpService) => new {1}($http)]);"; RtRaw registration = new RtRaw(String.Format(initializerFormat, element.Name, result.Name)); // Since RtModule.compilationUnits is not typed and could contain any type then we // simply add RtRaw node here with registration glue code // app variable is declared in /Scripts/ReinforcedTypings/App.ts since it is used in // corresponding client script Context.Location.CurrentModule.CompilationUnits.Add(registration); // That's all. here we return node that will be written to target file. // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts return(result); }
public abstract void Visit(RtField node);
public override void Visit(RtField node) { if (node == null) return; Visit(node.Documentation); AppendTabs(); if (Context == WriterContext.Module) Write("export var "); if (Context == WriterContext.None) Write("declare var "); if (Context == WriterContext.Class) Modifiers(node); Visit(node.Identifier); Write(": "); Visit(node.Type); WriteLine(";"); }