Пример #1
0
        private static Task <Document> UseStringEmptyInsteadOfEmptyStringLiteralAsync(
            Document document,
            ExpressionSyntax expression,
            CancellationToken cancellationToken = default)
        {
            MemberAccessExpressionSyntax memberAccessExpression = CSharpFactory.SimpleMemberAccessExpression(
                CSharpTypeFactory.StringType(),
                SyntaxFactory.IdentifierName("Empty"));

            memberAccessExpression = memberAccessExpression.WithTriviaFrom(expression);

            return(document.ReplaceNodeAsync(expression, memberAccessExpression, cancellationToken));
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            ObjectCreationExpressionSyntax objectCreationExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            TypeSyntax type = semanticModel
                              .GetTypeByMetadataName(MetadataNames.System_EventArgs)
                              .ToMinimalTypeSyntax(semanticModel, objectCreationExpression.SpanStart);

            MemberAccessExpressionSyntax newNode = CSharpFactory.SimpleMemberAccessExpression(type, SyntaxFactory.IdentifierName("Empty"))
                                                   .WithTriviaFrom(objectCreationExpression);

            return(await document.ReplaceNodeAsync(objectCreationExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
Пример #3
0
            public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
            {
                if (_enumTypeSymbol != null &&
                    !node.IsParentKind(SyntaxKind.SimpleMemberAccessExpression))
                {
                    MemberAccessExpressionSyntax newNode = CSharpFactory.SimpleMemberAccessExpression(
                        SyntaxFactory.ParseExpression(_enumTypeSymbol.ToDisplayString(SymbolDisplayFormats.TypeNameAndContainingTypesAndNamespaces)),
                        node);

                    if (!Options.FullyQualifiedNames)
                    {
                        newNode = newNode.WithAdditionalAnnotations(_simplifierAnnotationAsArray);
                    }

                    return(newNode);
                }

                return(base.VisitIdentifierName(node));
            }
Пример #4
0
        private Dictionary <SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary <ISymbol, string> symbolMap)
        {
            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodesAndSelf(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol.IsParameter())
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (parameterInfo.ParameterSymbol.OriginalDefinition.Equals(symbol))
                                {
                                    replacementMap.Add(identifierName, parameterInfo.Expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.IsTypeParameter())
                        {
                            var typeParameter = (ITypeParameterSymbol)symbol;

                            ImmutableArray <ITypeSymbol> typeArguments = MethodSymbol.TypeArguments;

                            if (typeArguments.Length > typeParameter.Ordinal)
                            {
                                replacementMap.Add(identifierName, typeArguments[typeParameter.Ordinal].ToMinimalTypeSyntax(DeclarationSemanticModel, identifierName.SpanStart));
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (!InvocationEnclosingType
                                .BaseTypesAndSelf()
                                .Any(f => f.Equals(containingType)))
                            {
                                replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                            }
                        }

                        if (symbolMap != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(identifierName, SyntaxFactory.IdentifierName(name));
                        }
                    }
                }
                else if (symbolMap != null)
                {
                    if (kind.Is(
                            SyntaxKind.VariableDeclarator,
                            SyntaxKind.SingleVariableDesignation,
                            SyntaxKind.Parameter,
                            SyntaxKind.TypeParameter,
                            SyntaxKind.ForEachStatement,
                            SyntaxKind.ForEachVariableStatement))
                    {
                        ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, CancellationToken);

                        Debug.Assert(symbol != null || (descendant as ForEachVariableStatementSyntax)?.Variable?.Kind() == SyntaxKind.TupleExpression, kind.ToString());

                        if (symbol != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(descendant, name);
                        }
                    }
                }
            }

            return(replacementMap);
        }
Пример #5
0
        public TNode RewriteNode <TNode>(TNode node) where TNode : SyntaxNode
        {
            Dictionary <ISymbol, string> symbolMap = GetSymbolsToRename();

            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodes(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol.IsParameter())
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (parameterInfo.ParameterSymbol.OriginalDefinition.Equals(symbol))
                                {
                                    replacementMap.Add(identifierName, parameterInfo.Expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (!InvocationEnclosingType
                                .BaseTypesAndSelf()
                                .Any(f => f.Equals(containingType)))
                            {
                                replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                            }
                        }
                        if (symbolMap != null)
                        {
                            string name;
                            if (symbolMap.TryGetValue(symbol, out name))
                            {
                                replacementMap.Add(identifierName, SyntaxFactory.IdentifierName(name));
                            }
                        }
                    }
                }
                else if (symbolMap != null &&
                         kind == SyntaxKind.VariableDeclarator)
                {
                    var variableDeclarator = (VariableDeclaratorSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(variableDeclarator, CancellationToken);

                    string name;
                    if (symbolMap.TryGetValue(symbol, out name))
                    {
                        replacementMap.Add(variableDeclarator, name);
                    }
                }
            }

            var rewriter = new InlineMethodRewriter(replacementMap);

            return((TNode)rewriter.Visit(node));
        }
Пример #6
0
        private Dictionary <SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary <ISymbol, string> symbolMap)
        {
            var replacementMap = new Dictionary <SyntaxNode, object>();

            foreach (SyntaxNode descendant in node.DescendantNodesAndSelf(node.Span))
            {
                SyntaxKind kind = descendant.Kind();

                if (kind == SyntaxKind.IdentifierName)
                {
                    var identifierName = (IdentifierNameSyntax)descendant;

                    ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);

                    if (symbol != null)
                    {
                        if (symbol is IParameterSymbol parameterSymbol)
                        {
                            foreach (ParameterInfo parameterInfo in ParameterInfos)
                            {
                                if (ParameterEquals(parameterInfo, parameterSymbol))
                                {
                                    ExpressionSyntax expression = parameterInfo.Expression;

                                    if (expression == null &&
                                        parameterInfo.ParameterSymbol.HasExplicitDefaultValue)
                                    {
                                        expression = parameterInfo.ParameterSymbol.GetDefaultValueMinimalSyntax(InvocationSemanticModel, Node.SpanStart);
                                    }

                                    replacementMap.Add(identifierName, expression);
                                    break;
                                }
                            }
                        }
                        else if (symbol.Kind == SymbolKind.TypeParameter)
                        {
                            var typeParameter = (ITypeParameterSymbol)symbol;

                            ImmutableArray <ITypeSymbol> typeArguments = TypeArguments;

                            if (typeArguments.Length > typeParameter.Ordinal)
                            {
                                replacementMap.Add(identifierName, typeArguments[typeParameter.Ordinal].ToMinimalTypeSyntax(DeclarationSemanticModel, identifierName.SpanStart));
                            }
                        }
                        else if (symbol.IsStatic &&
                                 !identifierName.IsParentKind(SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.QualifiedName))
                        {
                            INamedTypeSymbol containingType = symbol.ContainingType;

                            if (containingType != null)
                            {
                                if (!NodeEnclosingType
                                    .BaseTypesAndSelf()
                                    .Any(f => f.Equals(containingType)))
                                {
                                    replacementMap.Add(identifierName, CSharpFactory.SimpleMemberAccessExpression(containingType.ToTypeSyntax().WithSimplifierAnnotation(), identifierName));
                                }
                            }
                            else if (symbol is ITypeSymbol typeSymbol)
                            {
                                replacementMap.Add(identifierName, typeSymbol.ToMinimalTypeSyntax(InvocationSemanticModel, Node.SpanStart));
                            }
                        }

                        if (symbolMap != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(identifierName, IdentifierName(name));
                        }
                    }
                }
                else if (symbolMap != null)
                {
                    if (kind.Is(
                            SyntaxKind.VariableDeclarator,
                            SyntaxKind.SingleVariableDesignation,
                            SyntaxKind.Parameter,
                            SyntaxKind.TypeParameter,
                            SyntaxKind.ForEachStatement,
                            SyntaxKind.ForEachVariableStatement))
                    {
                        ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, CancellationToken);

                        Debug.Assert(symbol != null || (descendant as ForEachVariableStatementSyntax)?.Variable?.Kind() == SyntaxKind.TupleExpression, kind.ToString());

                        if (symbol != null &&
                            symbolMap.TryGetValue(symbol, out string name))
                        {
                            replacementMap.Add(descendant, name);
                        }
                    }
                }
            }

            return(replacementMap);

            bool ParameterEquals(in ParameterInfo parameterInfo, IParameterSymbol parameterSymbol2)
            {
                IParameterSymbol parameterSymbol = parameterInfo.ParameterSymbol;

                if (parameterSymbol.ContainingSymbol is IMethodSymbol methodSymbol)
                {
                    if (parameterInfo.IsThis ||
                        methodSymbol.MethodKind == MethodKind.ReducedExtension)
                    {
                        int ordinal = parameterSymbol.Ordinal;

                        if (methodSymbol.MethodKind == MethodKind.ReducedExtension)
                        {
                            ordinal++;
                        }

                        return(ordinal == parameterSymbol2.Ordinal &&
                               string.Equals(parameterSymbol.Name, parameterSymbol2.Name, StringComparison.Ordinal));
                    }
                }

                return(parameterSymbol.OriginalDefinition.Equals(parameterSymbol2));
            }
        }