Beispiel #1
0
        private Task <Document> ReplaceIdentifier(Document document, SyntaxNode root, ArgumentSyntax nodeToReplace,
                                                  string identifierName, bool isGraphExtension, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ExpressionSyntax newExpression;

            if (identifierName == "this")
            {
                newExpression = SyntaxFactory.ThisExpression();
            }
            else
            {
                newExpression = SyntaxFactory.IdentifierName(identifierName);
            }

            if (isGraphExtension)
            {
                newExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, newExpression,
                                                                     SyntaxFactory.IdentifierName("Base"));
            }

            var newNode = nodeToReplace.WithExpression(newExpression);
            var newRoot = root.ReplaceNode(nodeToReplace, newNode);

            return(Task.FromResult(document.WithSyntaxRoot(newRoot)));
        }
Beispiel #2
0
        /// <summary>
        ///   Normalizes the <paramref name="argument" />.
        /// </summary>
        public override SyntaxNode VisitArgument(ArgumentSyntax argument)
        {
            // Special case for array accesses, as Roslyn doesn't return a symbol for the argument
            if (argument.Parent.Parent is ElementAccessExpressionSyntax)
            {
                var arrayExpression = ((ElementAccessExpressionSyntax)argument.Parent.Parent).Expression;
                var kind            = SemanticModel.GetTypeInfo(arrayExpression).Type.TypeKind;
                if (kind == TypeKind.Array || kind == TypeKind.Pointer)
                {
                    return(base.VisitArgument(argument));
                }
            }

            var parameterSymbol = argument.GetParameterSymbol(SemanticModel);

            if (parameterSymbol.RefKind != RefKind.None)
            {
                return(base.VisitArgument(argument));
            }

            var arraySymbol     = parameterSymbol.Type as IArrayTypeSymbol;
            var isParamsFormula = arraySymbol != null && parameterSymbol.IsParams && IsFormulaType(arraySymbol.ElementType);

            if (!isParamsFormula && !IsFormulaType(parameterSymbol.Type))
            {
                return(base.VisitArgument(argument));
            }

            return(argument.WithExpression(ReplaceImplicitConversion(argument.Expression)));
        }
 public override SyntaxNode?VisitArgument(ArgumentSyntax node)
 {
     if (node.Expression.ToString() == _nativeIdentifier)
     {
         return(node.WithExpression(
                    CastExpression(_nativeType, node.Expression)));
     }
     return(base.VisitArgument(node));
 }
        private Task<Document> UseNameof(Document document, SyntaxNode root, ArgumentSyntax argument, CancellationToken cancellationToken)
        {
            var nameToken = ((LiteralExpressionSyntax)argument.Expression).Token.Text;
            var newArgumentDeclaration = argument.WithExpression(SyntaxFactory.ParseExpression($"nameof({nameToken.Trim('"')})"));

            var newRoot = root.ReplaceNode(argument, newArgumentDeclaration);
            var newDocument = document.WithSyntaxRoot(newRoot);

            return Task.FromResult(newDocument);
        }
            public override SyntaxNode VisitArgument(ArgumentSyntax node)
            {
                ITypeSymbol type = _semanticModel.GetTypeInfo(node.Expression).Type;

                if (_optionTypeSymbols.Contains(type))
                {
                    return(node.WithExpression(InvocationExpression(IdentifierName("UpdateOptions")).AddArgumentListArguments(node)));
                }

                return(base.VisitArgument(node));
            }
 public override SyntaxNode VisitArgument(ArgumentSyntax node)
 {
     if (node.Expression.Kind().Equals(SyntaxKind.ParenthesizedExpression))
     {
         NumberOperations ne = new NumberOperations();
         ParenthesizedExpressionSyntax pe = (ParenthesizedExpressionSyntax)node.Expression;
         ExpressionSyntax es      = SyntaxFactory.ParseExpression(ne.ExpressionParser(pe.Expression.ToString()).ToString());
         ArgumentSyntax   newNode = node.WithExpression(es);
         return(base.VisitArgument(node.ReplaceNode(node, newNode)));
     }
     return(base.VisitArgument(node));
 }
Beispiel #7
0
 public override SyntaxNode VisitArgument(ArgumentSyntax node)
 {
     if (node.Expression.Kind().Equals(SyntaxKind.NumericLiteralExpression))
     {
         NumberOperations        ne      = new NumberOperations();
         LiteralExpressionSyntax les     = (LiteralExpressionSyntax)node.Expression;
         ExpressionSyntax        es      = SyntaxFactory.ParseExpression(ne.UnwrapNumber(les.Token.ValueText));
         ArgumentSyntax          newNode = node.WithExpression(es);
         return(base.VisitArgument(node.ReplaceNode(node, newNode)));
     }
     return(base.VisitArgument(node));
 }
Beispiel #8
0
        public override SyntaxNode VisitArgument(ArgumentSyntax node)
        {
            var invocationSyntax = node.Parent.Parent as InvocationExpressionSyntax;

            if (invocationSyntax == null)
            {
                return(base.VisitArgument(node));
            }
            var memberAccessExpressionSyntax = invocationSyntax.Expression as MemberAccessExpressionSyntax;

            if (memberAccessExpressionSyntax == null)
            {
                return(base.VisitArgument(node));
            }
            var methodNameSyntax = memberAccessExpressionSyntax.Name as SimpleNameSyntax;

            var method = model.GetSymbolInfo(methodNameSyntax).Symbol as IMethodSymbol;

            if (method == null)
            {
                return(base.VisitArgument(node));
            }
            var argList = (ArgumentListSyntax)node.Parent;
            //TODO check that parameter name preserved if specified, otherwise order can be broken
            var argIndex        = argList.Arguments.IndexOf(node);
            var parameterSymbol = method.Parameters[argIndex]; //TODO when there is params argument in method index may be well out of range

            var lambda = node.Expression as LambdaExpressionSyntax;

            if (parameterSymbol.HasAttribute <MetaRewriteLambdaParamAttribute>(model.Compilation) && lambda != null)
            {
                //TODO error if value is not lambda
                return(node.WithExpression(VisitLambdaExpression(lambda)));
            }
            if (parameterSymbol.HasAttribute <MetaRewriteParamAttribute>(model.Compilation))
            {
                return(node.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(node.Expression.ToFullString()))));
            }
            return(base.VisitArgument(node));
        }
        public override SyntaxNode VisitArgument(ArgumentSyntax node)
        {
            node = (ArgumentSyntax)base.VisitArgument(node);

            // remove unnecessarily paren'd args (cosmetic)
            if (node.Expression.IsKind(SyntaxKind.ParenthesizedExpression))
            {
                Changed = true;
                node    = node.WithExpression(((ParenthesizedExpressionSyntax)node.Expression).Expression);
            }

            return(node);
        }
Beispiel #10
0
            public override SyntaxNode VisitArgument(ArgumentSyntax node)
            {
                if (node.Parent is ArgumentListSyntax argumentList &&
                    argumentList.Arguments.IndexOf(node) == 0 &&
                    argumentList.Parent is InvocationExpressionSyntax invocation &&
                    invocation.TryGetMethodName(out var method) &&
                    method == "GetString")
                {
                    return(node.WithExpression(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(this.newKey))));
                }

                return(base.VisitArgument(node));
            }
Beispiel #11
0
        private SyntaxNode linkArray(ExcessContext ctx, SyntaxNode linkNode, SyntaxNode newNode, SemanticModel model)
        {
            ArrayCreationExpressionSyntax ace = newNode as ArrayCreationExpressionSyntax;
            ArgumentSyntax arg     = null;
            bool           asParam = newNode is ArgumentSyntax;

            if (asParam)
            {
                arg = (ArgumentSyntax)newNode;
                ace = (ArrayCreationExpressionSyntax)arg.Expression;
            }

            ITypeSymbol arrayType = null;

            foreach (var expr in ace.Initializer.Expressions)
            {
                ITypeSymbol type = model.GetSpeculativeTypeInfo(expr.SpanStart, expr, SpeculativeBindingOption.BindAsExpression).Type;

                if (arrayType == null)
                {
                    arrayType = type;
                }
                else if (type != arrayType)
                {
                    if (isSuperClass(type, arrayType))
                    {
                        arrayType = type; //downcast
                    }
                    else if (!isSuperClass(arrayType, type))
                    {
                        //td: error
                        return(newNode); //unable to refine
                    }
                }
            }

            if (arrayType == null)
            {
                return(newNode);
            }

            ace = ace.WithType(SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName(arrayType.Name + "[]")));
            if (asParam)
            {
                return(arg.WithExpression(ace));
            }

            return(ace);
        }
            public override SyntaxNode VisitArgument(ArgumentSyntax node)
            {
                ExpressionSyntax newExpression = node.Expression.PrependToLeadingTrivia(Whitespace(new string(' ', 18 - node.NameColon?.Name.Identifier.ValueText.Length ?? 0)));

                if (node.NameColon != null)
                {
                    node = node.WithNameColon(node.NameColon.AppendToLeadingTrivia(TriviaList(NewLine(), Whitespace("            "))));
                }
                else
                {
                    newExpression = newExpression.AppendToLeadingTrivia(TriviaList(NewLine(), Whitespace("            ")));
                }

                return(node.WithExpression(newExpression));
            }
Beispiel #13
0
        public override SyntaxNode VisitArgument(ArgumentSyntax node)
        {
            var type = SemanticModel.GetTypeInfo(node.Expression).Type as ITypeSymbol;

            if (type != null && type.IsReferenceType && type.Name.Contains("Cluster"))
            {
                return(node.WithExpression(
                           SyntaxFactory.MemberAccessExpression(
                               SyntaxKind.SimpleMemberAccessExpression,
                               node.Expression,
                               SyntaxFactory.IdentifierName(
                                   @"val")
                               ).WithOperatorToken(
                               SyntaxFactory.Token(SyntaxKind.DotToken))
                           ));
            }
            return(base.VisitArgument(node));
        }
        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)));
        }
Beispiel #15
0
        /// <summary>
        ///   Lifts the expression represented by <paramref name="argument" />, if necessary.
        /// </summary>
        public override SyntaxNode VisitArgument(ArgumentSyntax argument)
        {
            if (!_liftedMethodStack.Peek())
            {
                return(base.VisitArgument(argument));
            }

            var requiresLifting = argument.HasAttribute <LiftExpressionAttribute>(SemanticModel);

            argument = (ArgumentSyntax)base.VisitArgument(argument);

            if (!requiresLifting)
            {
                return(argument);
            }

            var lambda = Syntax.ValueReturningLambdaExpression(Enumerable.Empty <ParameterSyntax>(), argument.Expression).WithTrivia(argument);

            return(argument.WithExpression((ExpressionSyntax)lambda));
        }
Beispiel #16
0
        private async Task <Solution> AddAsSpan(Document document, ArgumentSyntax typeDecl, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var substringInvocation = typeDecl.Expression as InvocationExpressionSyntax;
            var memberAccess        = substringInvocation.Expression as MemberAccessExpressionSyntax;
            var asSpan = InvocationExpression(
                MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    InvocationExpression(
                        MemberAccessExpression(
                            SyntaxKind.SimpleMemberAccessExpression,
                            memberAccess.Expression,
                            IdentifierName("AsSpan"))),
                    IdentifierName("Slice")))
                         .WithArgumentList(substringInvocation.ArgumentList);
            var newRoot = root.ReplaceNode(typeDecl, typeDecl.WithExpression(asSpan)).NormalizeWhitespace();


            return(document.Project.Solution.WithDocumentSyntaxRoot(document.Id, newRoot));
        }
        private static Task <Document> ApplyFixAsync(CodeFixContext context, SyntaxNode syntaxRoot, SemanticModel semanticModel, ArgumentSyntax argument, bool isMember)
        {
            var text = ((LiteralExpressionSyntax)argument.Expression).Token.ValueText;
            var identifierNameSyntax = SyntaxFactory.IdentifierName(text);
            var expression           = isMember && !argument.UsesUnderscoreNames(semanticModel, context.CancellationToken)
                ? SyntaxFactory.MemberAccessExpression(
                SyntaxKind.SimpleMemberAccessExpression,
                SyntaxFactory.ThisExpression(),
                identifierNameSyntax)
                : (ExpressionSyntax)identifierNameSyntax;

            var argumentList = SyntaxFactory.ArgumentList(
                SyntaxFactory.SingletonSeparatedList(
                    SyntaxFactory.Argument(expression)));
            var nameofInvocation = SyntaxFactory.InvocationExpression(NameofIdentifier, argumentList);

            return(Task.FromResult(context.Document.WithSyntaxRoot(syntaxRoot.ReplaceNode(argument, argument.WithExpression(nameofInvocation)))));
        }
            private ArgumentListSyntax CreateNewArgumentList(ArgumentListSyntax argumentList)
            {
                // Create ObjectCreationExpression for ParameterObject
                // Considers:
                // foo(a, >b, c,< d) -> foo(a, new ParameterObject(b, c), d)

                // Split arguments into two lists: one for ObjectCreationExpression and another for InvocationExpression
                SeparatedSyntaxList <ArgumentSyntax> ctorArguments       = Syntax.SeparatedList <ArgumentSyntax>();
                SeparatedSyntaxList <ArgumentSyntax> invocationArguments = Syntax.SeparatedList <ArgumentSyntax>();

                // Note: Pre-order visiting argument, as it may also contain method call
                // Considers:
                // foo(a, b, foo(1, 2, 3, 4), d) -> foo(a, new ParameterObject(b, foo(1, new ParameterObject(2, 3), 4)), d)

                for (int i = 0; i < argumentList.Arguments.Count; ++i)
                {
                    ArgumentSyntax argument = argumentList.Arguments[i];

                    if (IsCompressedArgument(argument, i))
                    {
                        ArgumentSyntax processedArgument = (ArgumentSyntax)base.VisitArgument(argument);
                        ctorArguments = ctorArguments.Add(processedArgument);
                    }
                }

                // TODO: Hard-coded type name!
                ObjectCreationExpressionSyntax creationExpression = Syntax.ObjectCreationExpression(Syntax.ParseTypeName("ParameterObject"))
                                                                    .WithArgumentList(Syntax.ArgumentList(ctorArguments));

                bool isParameterObjectAdded = false;

                for (int i = 0; i < argumentList.Arguments.Count; ++i)
                {
                    ArgumentSyntax argument = argumentList.Arguments[i];

                    if (!IsCompressedArgument(argument, i))
                    {
                        ArgumentSyntax processedArgument = (ArgumentSyntax)base.VisitArgument(argument);
                        invocationArguments = invocationArguments.Add(processedArgument);
                    }
                    else if (!isParameterObjectAdded)
                    {
                        // The first occurrence of compressed argument should be replaced with invocation expression
                        ArgumentSyntax processedArgument = (ArgumentSyntax)base.VisitArgument(argument);

                        ArgumentSyntax objectCreationArgument = processedArgument.WithExpression(creationExpression)
                                                                .WithAdditionalAnnotations(CodeAnnotations.Formatting);

                        if (argument.NameColon != null)
                        {
                            objectCreationArgument = objectCreationArgument.WithNameColon(Syntax.NameColon(Syntax.IdentifierName(this.parameterObjectName)));
                        }

                        invocationArguments = invocationArguments.Add(objectCreationArgument);

                        isParameterObjectAdded = true;
                    }
                }

                ArgumentListSyntax newArgumentList = Syntax.ArgumentList(invocationArguments);

                return(newArgumentList);
            }
Beispiel #19
0
        public override SyntaxNode VisitArgument(ArgumentSyntax node)
        {
            var ti = this.semanticModel.GetTypeInfo(node.Expression);

            ITypeSymbol      type      = null;
            IMethodSymbol    method    = null;
            IParameterSymbol parameter = null;

            if (ti.Type != null && ti.Type.TypeKind == TypeKind.Delegate)
            {
                type = ti.Type;
            }
            else if (ti.ConvertedType != null && ti.ConvertedType.TypeKind == TypeKind.Delegate)
            {
                type = ti.ConvertedType;
            }

            if (type != null)
            {
                var list       = node.Parent as ArgumentListSyntax;
                var invocation = node.Parent.Parent as InvocationExpressionSyntax;

                if (list != null && invocation != null)
                {
                    method = this.semanticModel.GetSymbolInfo(invocation).Symbol as IMethodSymbol;

                    if (method != null)
                    {
                        if (node.NameColon != null)
                        {
                            if (node.NameColon.Name != null)
                            {
                                var nameText = node.NameColon.Name.Identifier.ValueText;
                                if (nameText != null)
                                {
                                    foreach (var p in method.Parameters)
                                    {
                                        if (string.Equals(p.Name, nameText, StringComparison.Ordinal))
                                        {
                                            parameter = p;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            var index = list.Arguments.IndexOf(node);
                            if (index >= 0)
                            {
                                if (index < method.Parameters.Length)
                                {
                                    parameter = method.Parameters[index];
                                }
                                else if (index >= method.Parameters.Length && method.Parameters[method.Parameters.Length - 1].IsParams)
                                {
                                    parameter = method.Parameters[method.Parameters.Length - 1];
                                }
                            }
                        }
                    }
                }
            }
            var isParam = parameter != null && !SyntaxHelper.IsAnonymous(parameter.Type);
            var parent  = isParam && parameter.IsParams ? (InvocationExpressionSyntax)node.Parent.Parent : null;

            node = (ArgumentSyntax)base.VisitArgument(node);

            if (isParam)
            {
                var pType = parameter.Type;
                if (parameter.IsParams && SharpSixRewriter.IsExpandedForm(this.semanticModel, parent, method))
                {
                    pType = ((IArrayTypeSymbol)parameter.Type).ElementType;
                }

                if (node.Expression is CastExpressionSyntax && type.Equals(pType) || parameter.RefKind != RefKind.None)
                {
                    return(node);
                }

                if (pType.TypeKind == TypeKind.Delegate || parameter.IsParams && ((IArrayTypeSymbol)parameter.Type).ElementType.TypeKind == TypeKind.Delegate)
                {
                    var name = SyntaxFactory.IdentifierName(pType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)).WithoutTrivia();
                    var expr = node.Expression;

                    if (expr is LambdaExpressionSyntax || expr is AnonymousMethodExpressionSyntax)
                    {
                        expr = SyntaxFactory.ParenthesizedExpression(expr);
                    }

                    var cast = SyntaxFactory.CastExpression(name, expr);
                    node = node.WithExpression(cast);
                }
            }

            return(node);
        }