Example #1
0
 public override void Visit(RtFunction node)
 {
     if (node == null)
     {
         return;
     }
     Visit(node.Documentation);
     AppendTabs();
     if (Context != WriterContext.Interface)
     {
         Modifiers(node);
     }
     if (Context == WriterContext.Module)
     {
         Write("export function ");
     }
     Visit(node.Identifier);
     Write("(");
     SequentialVisit(node.Arguments, ", ");
     Write(") ");
     if (node.ReturnType != null)
     {
         Write(": ");
         Visit(node.ReturnType);
     }
     WriteLine(";");
     if (!string.IsNullOrEmpty(node.LineAfter))
     {
         AppendTabs();
         Write(node.LineAfter);
         Br();
     }
 }
Example #2
0
        public override void Visit(RtFunction node)
        {
            if (node == null)
            {
                return;
            }
            Visit(node.Documentation);
            AppendTabs();
            if (Context != WriterContext.Interface)
            {
                Decorators(node);
                Modifiers(node);
                if (node.IsAsync)
                {
                    Write("async ");
                }
            }
            Visit(node.Identifier);
            Write("(");
            SequentialVisit(node.Arguments, ", ");
            Write(") ");
            if (node.ReturnType != null)
            {
                Write(": ");
                if (node.IsAsync)
                {
                    Write("Promise<");
                }
                Visit(node.ReturnType);
                if (node.IsAsync)
                {
                    Write(">");
                }
            }

            if (Context == WriterContext.Interface)
            {
                WriteLine(";");
            }
            else
            {
                if (node.Body != null && !string.IsNullOrEmpty(node.Body.RawContent))
                {
                    CodeBlock(node.Body);
                }
                else
                {
                    EmptyBody(node.ReturnType);
                }
            }

            if (!string.IsNullOrEmpty(node.LineAfter))
            {
                AppendTabs();
                Write(node.LineAfter);
                Br();
            }
        }
        public override void Visit(RtFunction node)
        {
            var reflectionAttachedRtFunction = node as ReflectionAttachedRtFunction;

            if (reflectionAttachedRtFunction != null && !reflectionAttachedRtFunction.Handled)
            {
                reflectionAttachedRtFunction.Handled = true;

                ReflectionFunction(reflectionAttachedRtFunction);
            }
        }
Example #4
0
        /// <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 RtFunction GenerateNode(MethodInfo element, RtFunction result, TypeResolver resolver)
        {
            var b = base.GenerateNode(element, result, resolver);

            if (b == null)
            {
                return(null);
            }
            result.Identifier.IdentifierName = result.Identifier.IdentifierName + "1";
            return(b);
        }
Example #5
0
        public override RtFunction GenerateNode(MethodInfo element, RtFunction result, TypeResolver resolver)
        {
            result = base.GenerateNode(element, result, resolver);
            if (result == null)
            {
                return(null);
            }

            // here we are overriding return type to corresponding promise
            var  retType = result.ReturnType;
            bool isVoid  = (retType is RtSimpleTypeName) && (((RtSimpleTypeName)retType).TypeName == "void");

            // we use TypeResolver to get "any" type to avoid redundant type name construction
            // (or because I'm too lazy to manually construct "any" type)
            if (isVoid)
            {
                retType = resolver.ResolveTypeName(typeof(object));
            }

            // Here we override TS method return type to make it angular.IPromise
            // We are using RtSimpleType with generig parameter of existing method type
            result.ReturnType = new RtSimpleTypeName(new[] { retType }, "angular", "IPromise");

            // Here we retrieve method parameters
            // We are using .GetName() extension method to retrieve parameter name
            // It is supplied within Reinforced.Typings and retrieves parameter name
            // including possible name override with Fluent configuration or
            // [TsParameter] attribute
            var p = element.GetParameters().Select(c => string.Format("'{0}': {0}", c.GetName()));

            // Joining parameters for method body code
            var dataParameters = string.Join(", ", p);

            // Here we get path to controller
            // It is quite simple solution requiring /{controller}/{action} route
            string controller = element.DeclaringType.Name.Replace("Controller", String.Empty);
            string path       = String.Format("/{0}/{1}", controller, element.Name);

            const string code = @"var params = {{ {1} }};
return this.http.post('{0}', params)
    .then((response) => {{ response.data['requestParams'] = params; return response.data; }});";

            RtRaw body = new RtRaw(String.Format(code, path, dataParameters));

            result.Body = body;

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return(result);
        }
Example #6
0
        public ReflectionAttachedRtFunction(RtFunction actual, MethodInfo methodInfo)
        {
            MethodInfo = methodInfo;

            this.IsAsync    = actual.IsAsync;
            this.Identifier = actual.Identifier;
            this.ReturnType = actual.ReturnType;
            foreach (var argument in actual.Arguments)
            {
                Arguments.Add(argument);
            }
            foreach (var decorator in actual.Decorators)
            {
                Decorators.Add(decorator);
            }
            this.Body = actual.Body;

            this.CopyActualProperties(actual);
        }
Example #7
0
 private RtDelegateType GetCallbackType(RtFunction function)
 {
     return(new RtDelegateType(function.Arguments.ToArray(), new RtSimpleTypeName("void")));
 }
Example #8
0
 public abstract void Visit(RtFunction node);
 public abstract T Visit(RtFunction node);
Example #10
0
        /// <summary>
        /// We override GenerateNode method (since it is almost single method of code generator).
        /// </summary>
        /// <param name="element">Method of controller that we generate code for</param>
        /// <param name="result">
        /// Resulting node - we do not have to create resulting node by ourselfs.
        /// But wi still can return null from GenerateNode method
        /// </param>
        /// <param name="resolver">
        /// TypeResolver object that we will use to safely convert CLR types to TypeScript types names
        /// </param>
        /// <returns>AST node for method declaration</returns>
        public override RtFunction GenerateNode(MethodInfo element, RtFunction result, TypeResolver resolver)
        {
            // Here we get default result of method export
            result = base.GenerateNode(element, result, resolver);
            if (result == null)
            {
                return(null);
            }

            // We make method static since we will call it like JQueryController.Method
            result.IsStatic = true;

            // Below we will add special arguments for specifying  element that should be
            // disabled during query (disable element) and element placeholder for
            // loading inidicator
            result.Arguments.Add(
                new RtArgument()
            {
                Identifier   = new RtIdentifier("loadingPlaceholderSelector"),
                Type         = resolver.ResolveTypeName(typeof(string)),
                DefaultValue = "''"
            });

            result.Arguments.Add(
                new RtArgument()
            {
                Identifier   = new RtIdentifier("disableElement"),
                Type         = resolver.ResolveTypeName(typeof(string)),
                DefaultValue = "''"
            });

            // We save original type name
            var retType = result.ReturnType;

            // ... and in case of void we just replace it with "any"
            bool isVoid = (retType is RtSimpleTypeName) && (((RtSimpleTypeName)retType).TypeName == "void");

            // we use TypeResolver to get "any" type to avoid redundant type name construction
            // (or because I'm too lazy to manually construct "any" type)
            if (isVoid)
            {
                retType = resolver.ResolveTypeName(typeof(object));
            }

            // Here we override TS method return type to make it JQueryPromise
            // We are using RtSimpleType with generig parameter of existing method type
            result.ReturnType = new RtSimpleTypeName("JQueryPromise", new[] { retType });

            // Here we retrieve method parameters
            // We are using .GetName() extension method to retrieve parameter name
            // It is supplied within Reinforced.Typings and retrieves parameter name
            // including possible name override with Fluent configuration or
            // [TsParameter] attribute
            var p = element.GetParameters().Select(c => string.Format("'{0}': {0}", c.GetName()));

            // Joining parameters for method body code
            var dataParameters = string.Join(", ", p);

            // Here we get path to controller
            // It is quite simple solution requiring /{controller}/{action} route
            string controller = element.DeclaringType.Name.Replace("Controller", String.Empty);
            string path       = String.Format("/{0}/{1}", controller, element.Name);

            // Here we are constructing our glue code
            // Please refer to /Scripts/ReinforcedTypings/query.ts for implementation of QueryController.query method
            string code = String.Format(
                @"return QueryController.query<{2}>(
        '{0}', 
        {{ {1} }}, 
        loadingPlaceholderSelector,
        disableElement
    );",
                path, dataParameters, retType);

            // Here we just set method body and return method node
            result.Body = new RtRaw(code);

            // Also here we will add some JSDOC
            result.Documentation = new RtJsdocNode()
            {
                Description = String.Format("Wrapper method for call {0} of {1}", element.Name, element.DeclaringType.Name)
            };

            // That's all. here we return node that will be written to target file.
            // Check result in /Scripts/ReinforcedTypings/GeneratedTypings.ts
            return(result);
        }