Exemple #1
0
        public TNode RewriteNode <TNode>(TNode node) where TNode : SyntaxNode
        {
            Dictionary <ISymbol, string> symbolMap = GetSymbolsToRename();

            Dictionary <SyntaxNode, object> replacementMap = GetReplacementMap(node, symbolMap);

            var rewriter = new InlineMethodRewriter(replacementMap);

            return((TNode)rewriter.Visit(node));
        }
Exemple #2
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));
        }