Ejemplo n.º 1
0
        private static InvocationExpressionSyntax GetNewInvocation(InvocationExpressionSyntax invocation)
        {
            ArgumentListSyntax argumentList = invocation.ArgumentList;
            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            return(RefactoringHelper.ChangeInvokedMethodName(invocation, "Fail")
                   .WithArgumentList(argumentList.WithArguments(arguments.RemoveAt(0))));
        }
Ejemplo n.º 2
0
 private SeparatedSyntaxList <ArgumentSyntax> MergeContainKeyAndContainValueArguments(SeparatedSyntaxList <ArgumentSyntax> keyArguments, SeparatedSyntaxList <ArgumentSyntax> valueArguments)
 {
     return(new SeparatedSyntaxList <ArgumentSyntax>()
            .Add(keyArguments[0])
            .Add(valueArguments[0])
            .AddRange(keyArguments.RemoveAt(0))
            .AddRange(valueArguments.RemoveAt(0)));
 }
        private static InvocationExpressionSyntax GetNewInvocation(InvocationExpressionSyntax invocation)
        {
            ArgumentListSyntax argumentList = invocation.ArgumentList;
            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            if (arguments.Count == 1)
            {
                ArgumentSyntax argument = arguments[0];
                arguments = arguments.ReplaceAt(0, argument.WithExpression(StringLiteralExpression("").WithTriviaFrom(argument.Expression)));
            }
            else
            {
                arguments = arguments.RemoveAt(0);
            }

            return(RefactoringHelper.ChangeInvokedMethodName(invocation, "Fail")
                   .WithArgumentList(argumentList.WithArguments(arguments)));
        }
Ejemplo n.º 4
0
        private void DoTestAddInsertRemoveOnEmptyList(SeparatedSyntaxList <SyntaxNode> list)
        {
            Assert.Equal(0, list.Count);

            SyntaxNode nodeD = SyntaxFactory.ParseExpression("D");
            SyntaxNode nodeE = SyntaxFactory.ParseExpression("E");

            var newList = list.Add(nodeD);

            Assert.Equal(1, newList.Count);
            Assert.Equal("D", newList.ToFullString());

            newList = list.AddRange(new[] { nodeD, nodeE });
            Assert.Equal(2, newList.Count);
            Assert.Equal("D,E", newList.ToFullString());

            newList = list.Insert(0, nodeD);
            Assert.Equal(1, newList.Count);
            Assert.Equal("D", newList.ToFullString());

            newList = list.InsertRange(0, new[] { nodeD, nodeE });
            Assert.Equal(2, newList.Count);
            Assert.Equal("D,E", newList.ToFullString());

            newList = list.Remove(nodeD);
            Assert.Equal(0, newList.Count);

            Assert.Equal(-1, list.IndexOf(nodeD));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(0));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(1, nodeD));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(-1, nodeD));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.InsertRange(1, new[] { nodeD }));
            Assert.Throws <ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { nodeD }));
            Assert.Throws <ArgumentNullException>(() => list.Add(null));
            Assert.Throws <ArgumentNullException>(
                () => list.AddRange((IEnumerable <SyntaxNode>)null)
                );
            Assert.Throws <ArgumentNullException>(() => list.Insert(0, null));
            Assert.Throws <ArgumentNullException>(
                () => list.InsertRange(0, (IEnumerable <SyntaxNode>)null)
                );
        }
        public override SyntaxNode VisitArgumentList(ArgumentListSyntax node)
        {
            node = (ArgumentListSyntax)base.VisitArgumentList(node);
            SeparatedSyntaxList <ArgumentSyntax> updated = node.Arguments;
            int i = 0;

            foreach (ArgumentSyntax argument in node.Arguments)
            {
                if (RemoveArgument(argument))
                {
                    updated = updated.RemoveAt(i);
                    continue;
                }
                i++;
            }
            return(node.Update(
                       node.OpenParenToken,
                       updated,
                       node.CloseParenToken));
        }
Ejemplo n.º 6
0
        public static Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocation,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression;

            MemberAccessExpressionSyntax newMemberAccess = memberAccess.WithName(IdentifierName("Concat").WithTriviaFrom(memberAccess.Name));

            ArgumentListSyntax argumentList = invocation.ArgumentList;
            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            ArgumentListSyntax newArgumentList = argumentList
                                                 .WithArguments(arguments.RemoveAt(0))
                                                 .WithOpenParenToken(argumentList.OpenParenToken.AppendToTrailingTrivia(arguments[0].GetLeadingAndTrailingTrivia()));

            InvocationExpressionSyntax newInvocation = invocation
                                                       .WithExpression(newMemberAccess)
                                                       .WithArgumentList(newArgumentList);

            return(document.ReplaceNodeAsync(invocation, newInvocation, cancellationToken));
        }
        private static void AnalyzeAnonyousMethodExpression(SyntaxNodeAnalysisContext context)
        {
            if (context.Node.SpanContainsDirectives())
            {
                return;
            }

            var anonymousMethod = (AnonymousMethodExpressionSyntax)context.Node;

            ParameterListSyntax parameterList = anonymousMethod.ParameterList;

            if (parameterList == null)
            {
                return;
            }

            InvocationExpressionSyntax invocationExpression = GetInvocationExpression(anonymousMethod.Body);

            if (invocationExpression == null)
            {
                return;
            }

            ExpressionSyntax expression = invocationExpression.Expression;

            if (!IsSimpleInvocation(expression))
            {
                return;
            }

            SemanticModel     semanticModel     = context.SemanticModel;
            CancellationToken cancellationToken = context.CancellationToken;

            if (!(semanticModel.GetSymbol(invocationExpression, cancellationToken) is IMethodSymbol methodSymbol))
            {
                return;
            }

            if (methodSymbol.MethodKind == MethodKind.DelegateInvoke)
            {
                return;
            }

            if (!methodSymbol.IsStatic &&
                expression.Kind() != SyntaxKind.IdentifierName)
            {
                if (!ExpressionIsParameter(expression, parameterList))
                {
                    return;
                }
                else if (methodSymbol.MethodKind == MethodKind.ReducedExtension &&
                         !context.ContainingSymbol.ContainingType.Equals(methodSymbol.ContainingType))
                {
                    return;
                }
            }

            ImmutableArray <IParameterSymbol> parameterSymbols = methodSymbol.Parameters;

            ArgumentListSyntax argumentList = invocationExpression.ArgumentList;

            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            if (arguments.Count != parameterSymbols.Length)
            {
                return;
            }

            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            bool isReduced = methodSymbol.MethodKind == MethodKind.ReducedExtension;

            if (parameters.Count != ((isReduced) ? arguments.Count + 1 : arguments.Count))
            {
                return;
            }

            MemberAccessExpressionSyntax memberAccessExpression = (isReduced) ? (MemberAccessExpressionSyntax)expression : null;

            if (isReduced)
            {
                if (!CheckParameter(
                        parameters[0],
                        ((MemberAccessExpressionSyntax)expression).Expression,
                        methodSymbol.ReducedFrom.Parameters[0]))
                {
                    return;
                }

                parameters = parameters.RemoveAt(0);
            }

            if (!CheckParameters(parameters, arguments, parameterSymbols))
            {
                return;
            }

            methodSymbol = (isReduced) ? methodSymbol.GetConstructedReducedFrom() : methodSymbol;

            if (!CheckInvokeMethod(anonymousMethod, methodSymbol, semanticModel, context.CancellationToken))
            {
                return;
            }

            if (!CheckSpeculativeSymbol(
                    anonymousMethod,
                    (isReduced) ? memberAccessExpression.Name.WithoutTrivia() : expression,
                    methodSymbol,
                    semanticModel,
                    cancellationToken))
            {
                return;
            }

            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.UseMethodGroupInsteadOfAnonymousFunction, anonymousMethod);

            FadeOut(context, null, parameterList, anonymousMethod.Block, argumentList, anonymousMethod.DelegateKeyword, memberAccessExpression);
        }
        public static void AnalyzeParenthesizedLambdaExpression(SyntaxNodeAnalysisContext context)
        {
            if (context.Node.SpanContainsDirectives())
            {
                return;
            }

            var lambda = (ParenthesizedLambdaExpressionSyntax)context.Node;

            InvocationExpressionSyntax invocationExpression = GetInvocationExpression(lambda.Body);

            if (invocationExpression == null)
            {
                return;
            }

            ExpressionSyntax expression = invocationExpression.Expression;

            if (!IsSimpleInvocation(expression))
            {
                return;
            }

            SemanticModel     semanticModel     = context.SemanticModel;
            CancellationToken cancellationToken = context.CancellationToken;

            var methodSymbol = semanticModel.GetSymbol(invocationExpression, cancellationToken) as IMethodSymbol;

            if (methodSymbol == null)
            {
                return;
            }

            ImmutableArray <IParameterSymbol> parameterSymbols = methodSymbol.Parameters;

            ArgumentListSyntax argumentList = invocationExpression.ArgumentList;

            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            if (arguments.Count != parameterSymbols.Length)
            {
                return;
            }

            ParameterListSyntax parameterList = lambda.ParameterList;

            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            bool isReduced = methodSymbol.MethodKind == MethodKind.ReducedExtension;

            if (parameters.Count != ((isReduced) ? arguments.Count + 1 : arguments.Count))
            {
                return;
            }

            MemberAccessExpressionSyntax memberAccessExpression = (isReduced) ? (MemberAccessExpressionSyntax)expression : null;

            if (isReduced)
            {
                if (!CheckParameter(
                        parameters[0],
                        memberAccessExpression.Expression,
                        methodSymbol.ReducedFrom.Parameters[0]))
                {
                    return;
                }

                parameters = parameters.RemoveAt(0);
            }

            if (!CheckParameters(parameters, arguments, parameterSymbols))
            {
                return;
            }

            methodSymbol = (isReduced) ? methodSymbol.GetConstructedReducedFrom() : methodSymbol;

            if (!CheckInvokeMethod(lambda, methodSymbol, semanticModel, context.CancellationToken))
            {
                return;
            }

            if (!CheckSpeculativeSymbol(
                    lambda,
                    (isReduced) ? memberAccessExpression.Name.WithoutTrivia() : expression,
                    methodSymbol,
                    semanticModel))
            {
                return;
            }

            context.ReportDiagnostic(DiagnosticDescriptors.UseMethodGroupInsteadOfAnonymousFunction, lambda);

            FadeOut(context, null, parameterList, lambda.Body as BlockSyntax, argumentList, lambda.ArrowToken, memberAccessExpression);
        }
        private static ArgumentListPair RewriteArgumentLists(
            ArgumentListSyntax argumentList,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ArgumentSyntax pattern      = null;
            ArgumentSyntax regexOptions = null;
            ArgumentSyntax matchTimeout = null;

            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            SeparatedSyntaxList <ArgumentSyntax> newArguments = arguments;

            for (int i = arguments.Count - 1; i >= 0; i--)
            {
                IParameterSymbol parameterSymbol = semanticModel.DetermineParameter(arguments[i], cancellationToken: cancellationToken);

                Debug.Assert(parameterSymbol != null, "");

                if (parameterSymbol != null)
                {
                    if (pattern == null &&
                        parameterSymbol.Type.IsString() &&
                        parameterSymbol.Name == "pattern")
                    {
                        pattern      = arguments[i];
                        newArguments = newArguments.RemoveAt(i);
                    }

                    if (regexOptions == null &&
                        parameterSymbol.Type.Equals(semanticModel.GetTypeByMetadataName(MetadataNames.System_Text_RegularExpressions_RegexOptions)))
                    {
                        regexOptions = arguments[i];
                        newArguments = newArguments.RemoveAt(i);
                    }

                    if (matchTimeout == null &&
                        parameterSymbol.Type.Equals(semanticModel.GetTypeByMetadataName(MetadataNames.System_TimeSpan)))
                    {
                        matchTimeout = arguments[i];
                        newArguments = newArguments.RemoveAt(i);
                    }
                }
            }

            argumentList = argumentList.WithArguments(newArguments);

            var arguments2 = new List <ArgumentSyntax>();

            if (pattern != null)
            {
                arguments2.Add(pattern);
            }

            if (regexOptions != null)
            {
                arguments2.Add(regexOptions);
            }

            if (matchTimeout != null)
            {
                arguments2.Add(matchTimeout);
            }

            return(new ArgumentListPair(argumentList, ArgumentList(arguments2.ToArray())));
        }
Ejemplo n.º 10
0
        private void DoTestAddInsertRemoveOnEmptyList(SeparatedSyntaxList<SyntaxNode> list)
        {
            Assert.Equal(0, list.Count);

            SyntaxNode nodeD = SyntaxFactory.ParseExpression("D");
            SyntaxNode nodeE = SyntaxFactory.ParseExpression("E");

            var newList = list.Add(nodeD);
            Assert.Equal(1, newList.Count);
            Assert.Equal("D", newList.ToFullString());

            newList = list.AddRange(new[] { nodeD, nodeE });
            Assert.Equal(2, newList.Count);
            Assert.Equal("D,E", newList.ToFullString());

            newList = list.Insert(0, nodeD);
            Assert.Equal(1, newList.Count);
            Assert.Equal("D", newList.ToFullString());

            newList = list.InsertRange(0, new[] { nodeD, nodeE });
            Assert.Equal(2, newList.Count);
            Assert.Equal("D,E", newList.ToFullString());

            newList = list.Remove(nodeD);
            Assert.Equal(0, newList.Count);

            Assert.Equal(-1, list.IndexOf(nodeD));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(1, nodeD));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, nodeD));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(1, new[] { nodeD }));
            Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { nodeD }));
            Assert.Throws<ArgumentNullException>(() => list.Add(null));
            Assert.Throws<ArgumentNullException>(() => list.AddRange((IEnumerable<SyntaxNode>)null));
            Assert.Throws<ArgumentNullException>(() => list.Insert(0, null));
            Assert.Throws<ArgumentNullException>(() => list.InsertRange(0, (IEnumerable<SyntaxNode>)null));
        }
        private static void AnalyzeParenthesizedLambdaExpression(SyntaxNodeAnalysisContext context)
        {
            if (!ConvertToMethodGroup(context))
            {
                return;
            }

            if (context.Node.SpanContainsDirectives())
            {
                return;
            }

            var lambda = (ParenthesizedLambdaExpressionSyntax)context.Node;

            InvocationExpressionSyntax invocationExpression = GetInvocationExpression(lambda.Body);

            if (invocationExpression == null)
            {
                return;
            }

            ExpressionSyntax expression = invocationExpression.Expression;

            if (!IsSimpleInvocation(expression))
            {
                return;
            }

            SemanticModel     semanticModel     = context.SemanticModel;
            CancellationToken cancellationToken = context.CancellationToken;

            if (semanticModel.GetSymbol(invocationExpression, cancellationToken) is not IMethodSymbol methodSymbol)
            {
                return;
            }

            if (methodSymbol.MethodKind == MethodKind.DelegateInvoke)
            {
                return;
            }

            if (!methodSymbol.IsStatic &&
                expression.Kind() != SyntaxKind.IdentifierName)
            {
                if (!ExpressionIsParameter(expression, lambda.ParameterList))
                {
                    return;
                }
                else if (methodSymbol.MethodKind == MethodKind.ReducedExtension &&
                         !SymbolEqualityComparer.Default.Equals(context.ContainingSymbol.ContainingType, methodSymbol.ContainingType))
                {
                    return;
                }
            }

            ImmutableArray <IParameterSymbol> parameterSymbols = methodSymbol.Parameters;

            ArgumentListSyntax argumentList = invocationExpression.ArgumentList;

            SeparatedSyntaxList <ArgumentSyntax> arguments = argumentList.Arguments;

            if (arguments.Count != parameterSymbols.Length)
            {
                return;
            }

            ParameterListSyntax parameterList = lambda.ParameterList;

            SeparatedSyntaxList <ParameterSyntax> parameters = parameterList.Parameters;

            bool isReduced = methodSymbol.MethodKind == MethodKind.ReducedExtension;

            if (parameters.Count != ((isReduced) ? arguments.Count + 1 : arguments.Count))
            {
                return;
            }

            MemberAccessExpressionSyntax memberAccessExpression = (isReduced) ? (MemberAccessExpressionSyntax)expression : null;

            if (isReduced)
            {
                if (!CheckParameter(
                        parameters[0],
                        memberAccessExpression.Expression,
                        methodSymbol.ReducedFrom.Parameters[0]))
                {
                    return;
                }

                parameters = parameters.RemoveAt(0);
            }

            if (!CheckParameters(parameters, arguments, parameterSymbols))
            {
                return;
            }

            methodSymbol = (isReduced) ? methodSymbol.GetConstructedReducedFrom() : methodSymbol;

            if (!CheckInvokeMethod(lambda, methodSymbol, semanticModel, context.CancellationToken))
            {
                return;
            }

            if (!CheckSpeculativeSymbol(
                    lambda,
                    (isReduced) ? memberAccessExpression.Name.WithoutTrivia() : expression,
                    methodSymbol,
                    semanticModel,
                    cancellationToken))
            {
                return;
            }

            ReportAnonymousFunction(context, lambda);
        }
 public SeparatedSyntaxList <TItem> RemoveAt(SeparatedSyntaxList <TItem> list, int index)
 {
     return(list.RemoveAt(index));
 }
Ejemplo n.º 13
0
        private async Task <Document> CompleteWithItIsAny(Document document, TypeDeclarationSyntax typeDecl, CancellationToken cancellationToken, ArgumentListSyntax mockedMethod)
        {
            var mockedMethodDeclaration = SymbolFinder.FindDeclarationsAsync(
                document.Project,
                ((IdentifierNameSyntax)((MemberAccessExpressionSyntax)((InvocationExpressionSyntax)mockedMethod.Parent).Expression).Name).Identifier.ValueText,
                false,
                cancellationToken).Result;
            var parameters = ((IMethodSymbol)mockedMethodDeclaration.First()).Parameters;


            var length = parameters.Length;

            SeparatedSyntaxList <ArgumentSyntax> arguments = mockedMethod.Arguments;

            for (int index = 0; index < length; index++)
            {
                var argument  = arguments.ElementAtOrDefault(index);
                var isMissing = argument?.IsMissing ?? true;

                if (isMissing)
                {
                    var parameter = parameters[index];

                    if (argument != null)
                    {
                        arguments = arguments.RemoveAt(index);
                    }

                    var argumentSyntax = SyntaxFactory.Argument(
                        SyntaxFactory.InvocationExpression(
                            SyntaxFactory.MemberAccessExpression(
                                SyntaxKind.SimpleMemberAccessExpression,
                                SyntaxFactory.IdentifierName("It"),
                                SyntaxFactory.GenericName(SyntaxFactory.Identifier("IsAny"))
                                .WithTypeArgumentList(
                                    SyntaxFactory.TypeArgumentList(
                                        SyntaxFactory.SingletonSeparatedList(
                                            SyntaxFactory.ParseTypeName(parameter.ToDisplayString()))))))
                        .NormalizeWhitespace());
                    arguments = arguments.Insert(index, argumentSyntax);
                }
            }
            var updatedMockedMethod = mockedMethod.WithArguments(arguments);
            var syntaxTree          = await document.GetSyntaxTreeAsync(cancellationToken);

            var updatedSyntaxTree =
                syntaxTree.GetRoot().ReplaceNode(mockedMethod, updatedMockedMethod);

            return(document.WithSyntaxRoot(updatedSyntaxTree));

            return(null);

            // Compute new uppercase name.
            var identifierToken = typeDecl.Identifier;
            var newName         = identifierToken.Text.ToUpperInvariant();

            // Get the symbol representing the type to be renamed.
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken);

            var typeSymbol = semanticModel.GetDeclaredSymbol(typeDecl, cancellationToken);

            // Produce a new solution that has all references to that type renamed, including the declaration.
            var originalSolution = document.Project.Solution;
            var optionSet        = originalSolution.Workspace.Options;
            var newSolution      = await Renamer.RenameSymbolAsync(document.Project.Solution, typeSymbol, newName, optionSet, cancellationToken).ConfigureAwait(false);

            // Return the new solution with the now-uppercase type name.
            //return newSolution;
        }
 public override SeparatedSyntaxListWrapper <TNode> RemoveAt(int index)
 => new AutoWrapSeparatedSyntaxList <TSyntax>(syntaxList.RemoveAt(index));