Exemple #1
0
 public static Func <TypeSyntax, MethodDeclarationSyntax> MakeMethodFactory(
     MethodDeclarationSyntax method, ParameterListSyntax preParameterList, ParameterListSyntax?postParameterList = null
     ) =>
 (syntax) => method
 .WithParameterList(
     preParameterList.AddParameters(Parameter(Identifier("handler")).WithType(syntax))
     .AddParameters(postParameterList?.Parameters.ToArray() ?? Array.Empty <ParameterSyntax>())
     );
Exemple #2
0
        /// <summary>
        /// Visit MethodDeclaration Statement and removes all not used parameters
        /// </summary>
        /// <param name="node">Visited statement</param>
        /// <returns>null, becouse statement should be removed</returns>
        public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            ParameterListSyntax newParameterList = node.ParameterList
                                                   .WithParameters(new SeparatedSyntaxList <ParameterSyntax>());

            foreach (ParameterSyntax parameter in node.ParameterList.Parameters)
            {
                if (node.Body.Statements
                    .SelectMany(statement => statement.DescendantTokens())
                    .Select(token => token.ValueText)
                    .Distinct()
                    .Contains(parameter.Identifier.ValueText))
                {
                    newParameterList = newParameterList.AddParameters(parameter);
                }
            }

            return(node.WithParameterList(newParameterList));
        }
Exemple #3
0
        /// <summary> Modify the method found in GetOriginalMethodDeclarationSyntax(). </summary>
        private static MethodDeclarationSyntax GetModifiedMethodDeclarationSyntax(MethodDeclarationSyntax method, MethodModificationData modificationData)
        {
            var nodes = method.DescendantNodes().ToList();

            ParameterListSyntax paramListSyntax = SyntaxFactory.ParameterList();

            foreach (var param in modificationData.m_NewMethodParams)
            {
                paramListSyntax = paramListSyntax.AddParameters(CodeGenerationUtility.CreateParameterSyntax(param.m_ParamName, param.m_ParamType));
            }

            method = modificationData.m_ParamModificationType == MethodParameterModificationType.REPLACE_PARAMS
                ? method.WithParameterList(paramListSyntax)
                : method.AddParameterListParameters(paramListSyntax.Parameters.ToArray());

            BlockSyntax blockSyntax   = SyntaxFactory.Block();
            var         oldStatements = method.Body.Statements.ToList();

            foreach (var statement in modificationData.m_BodyStatements)
            {
                if (modificationData.m_BodyModificationType == MethodBodyModificationType.ADD_OR_REPLACE_BODY)
                {
                    if (oldStatements.Find(x => x.ToFullString().Contains(statement)) != null)
                    {
                        continue;
                    }
                }

                blockSyntax = blockSyntax.AddStatements(SyntaxFactory.ParseStatement(statement));
            }

            // if replacing the body, the statement in the old function with be completely replaced with the new statement
            method = modificationData.m_BodyModificationType == MethodBodyModificationType.REPLACE_BODY
                ? method.WithBody(blockSyntax)
                : method.AddBodyStatements(blockSyntax.Statements.ToArray());

            return(method.NormalizeWhitespace());
        }