private Task <Solution> UseNameofAsync(Document document, SyntaxNode root,
                                               ObjectCreationExpressionSyntax objectCreationExpression)
        {
            var method              = objectCreationExpression.Ancestors().OfType <MethodDeclarationSyntax>().First();
            var methodParameters    = method.ParameterList.Parameters;
            var expressionArguments =
                objectCreationExpression.ArgumentList.Arguments.Select(x => x.Expression)
                .OfType <LiteralExpressionSyntax>();

            foreach (var expressionArgument in expressionArguments)
            {
                foreach (var methodParameter in methodParameters)
                {
                    if (string.Equals((string)methodParameter.Identifier.Value, (string)expressionArgument.Token.Value,
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        var newExpression = SyntaxFactory.ParseExpression($"nameof({methodParameter.Identifier})");
                        var newParent     = objectCreationExpression.ReplaceNode(expressionArgument, newExpression);
                        var newRoot       = root.ReplaceNode(objectCreationExpression, newParent);
                        var newDocument   = document.WithSyntaxRoot(newRoot);
                        return(Task.FromResult(newDocument.Project.Solution));
                    }
                }
            }

            return(null);
        }
        public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            SyntaxNode newRoot;

            if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
            {
                var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
                var statement            = assignmentExpression.Parent as ExpressionStatementSyntax;
                var identitySymbol       = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
                newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
                    ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
                    : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
            {
                var variableDeclarator  = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
                var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
                var statement           = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
                newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
            {
                var identifierName = GetIdentifierName(objectCreation, semanticModel);

                var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
                                          .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
                                                                                              .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));

                var arg     = objectCreation.Parent as ArgumentSyntax;
                var args    = objectCreation.Parent.Parent as ArgumentListSyntax;
                var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));

                StatementSyntax statement = objectCreation.FirstAncestorOfType <ExpressionStatementSyntax>();
                if (statement != null)
                {
                    var exprStatement    = statement.ReplaceNode(args, newArgs);
                    var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
                                           .WithDeclaration(variableDeclaration);
                    return(root.ReplaceNode(statement, newUsingStatment));
                }

                statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
                var newStatement = statement.ReplaceNode(args, newArgs);
                var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
                var usingBlock     = SyntaxFactory.Block(statementsForUsing);
                var usingStatement = CreateUsingStatement(newStatement, usingBlock)
                                     .WithDeclaration(variableDeclaration);
                var statementsToReplace = new List <StatementSyntax> {
                    statement
                };
                statementsToReplace.AddRange(statementsForUsing.Skip(1));
                newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
            }
            else
            {
                newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
            }
            return(newRoot);
        }
        public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            SyntaxNode newRoot;
            if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
            {
                var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
                var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
                var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
                newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
                    ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
                    : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
            {
                var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
                var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
                var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
                newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
            {
                var identifierName = GetIdentifierName(objectCreation, semanticModel);

                var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
                    .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
                    .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));

                var arg = objectCreation.Parent as ArgumentSyntax;
                var args = objectCreation.Parent.Parent as ArgumentListSyntax;
                var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));

                StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>();
                if (statement != null)
                {
                    var exprStatement = statement.ReplaceNode(args, newArgs);
                    var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
                        .WithDeclaration(variableDeclaration);
                    return root.ReplaceNode(statement, newUsingStatment);
                }

                statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
                var newStatement = statement.ReplaceNode(args, newArgs);
                var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
                var usingBlock = SyntaxFactory.Block(statementsForUsing);
                var usingStatement = CreateUsingStatement(newStatement, usingBlock)
                    .WithDeclaration(variableDeclaration);
                var statementsToReplace = new List<StatementSyntax> { statement };
                statementsToReplace.AddRange(statementsForUsing.Skip(1));
                newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
            }
            else
            {
                newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
            }
            return newRoot;
        }
        private Task <Solution> UseNameofAsync(Document document, SyntaxNode root, ObjectCreationExpressionSyntax objectCreationExpression)
        {
            var method = objectCreationExpression.Ancestors().OfType <MethodDeclarationSyntax>(SyntaxKind.MethodDeclaration).FirstOrDefault();
            PropertyDeclarationSyntax property = default(PropertyDeclarationSyntax);

            if (method == null)
            {
                // Fired from a property setter
                property = objectCreationExpression.Ancestors()
                           .OfType <PropertyDeclarationSyntax>(SyntaxKind.PropertyDeclaration)
                           .First();
            }

            var expressionArguments =
                objectCreationExpression.ArgumentList.Arguments.Select(x => x.Expression)
                .OfType <LiteralExpressionSyntax>();

            foreach (var expressionArgument in expressionArguments)
            {
                if (property != default(PropertyDeclarationSyntax))
                {
                    if (string.Equals(expressionArgument.Token.ValueText, "value", StringComparison.OrdinalIgnoreCase))
                    {
                        return(CreateNewExpressionAsync(root, "value", objectCreationExpression, expressionArgument, document));
                    }
                }
                else
                {
                    Debug.Assert(method != null);
                    var methodParameters = method.ParameterList.Parameters;
                    foreach (var methodParameter in methodParameters)
                    {
                        if (string.Equals(methodParameter.Identifier.ValueText, expressionArgument.Token.ValueText, StringComparison.OrdinalIgnoreCase))
                        {
                            return(CreateNewExpressionAsync(root, methodParameter.Identifier.ValueText, objectCreationExpression, expressionArgument, document));
                        }
                    }
                }
            }

            throw new InvalidOperationException("No corresponding parameter could be found");
        }
コード例 #5
0
        internal static string GuessParameterName(SemanticModel model, ObjectCreationExpressionSyntax objectCreateExpression, List <string> validNames)
        {
            if (validNames.Count == 1)
            {
                return(validNames[0]);
            }
            var parent = objectCreateExpression.Ancestors().OfType <IfStatementSyntax>().FirstOrDefault();

            if (parent == null)
            {
                return(null);
            }
            return(GetParameterName(model, parent.Condition));
        }
コード例 #6
0
        private static bool IsInsideComponent(ObjectCreationExpressionSyntax creation, SemanticModel model)
        {
            var classDeclaration = creation
                                   .Ancestors()
                                   .OfType <ClassDeclarationSyntax>()
                                   .FirstOrDefault();

            if (classDeclaration == null)
            {
                return(false);
            }

            var symbol = model.GetDeclaredSymbol(classDeclaration) as ITypeSymbol;

            return(symbol.Extends(typeof(UnityEngine.Component)));
        }
        private Task<Solution> UseNameofAsync(Document document, SyntaxNode root, ObjectCreationExpressionSyntax objectCreationExpression)
        {
            var method = objectCreationExpression.Ancestors().OfType<MethodDeclarationSyntax>().First();
            var methodParameters = method.ParameterList.Parameters;
            var expressionArguments = objectCreationExpression.ArgumentList.Arguments.Select(x => x.Expression).OfType<LiteralExpressionSyntax>();

            foreach (var expressionArgument in expressionArguments)
            {
                foreach (var methodParameter in methodParameters)
                {
                    if (string.Equals((string) methodParameter.Identifier.Value, (string) expressionArgument.Token.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        var newExpression = SyntaxFactory.ParseExpression($"nameof({methodParameter.Identifier})");
                        var newParent = objectCreationExpression.ReplaceNode(expressionArgument, newExpression);
                        var newRoot = root.ReplaceNode(objectCreationExpression, newParent);
                        var newDocument = document.WithSyntaxRoot(newRoot);
                        return Task.FromResult(newDocument.Project.Solution);
                    }
                }
            }

            return null;
        }
コード例 #8
0
 internal static string GuessParameterName(SemanticModel model, ObjectCreationExpressionSyntax objectCreateExpression, List<string> validNames)
 {
     if (validNames.Count == 1)
         return validNames[0];
     var parent = objectCreateExpression.Ancestors().OfType<IfStatementSyntax>().FirstOrDefault();
     if (parent == null)
         return null;
     return GetParameterName(model, parent.Condition);
 }
コード例 #9
0
        private static SqlStatement ExtractSqlStatement(
            SyntaxNodeAnalysisContext context,
            ObjectCreationExpressionSyntax npgsqlCommandCtor)
        {
            if (!npgsqlCommandCtor.ArgumentList.Arguments.Any())
            {
                /**
                 * Query is not passed through the constructor,
                 * therefore it might be assigned to the CommandText prop
                 */
                var commandTextAssignment = FindVariableAssignmentsInNodes(
                    npgsqlCommandCtor.Ancestors(),
                    name => name.Contains($".{nameof(NpgsqlCommand.CommandText)}"))
                                            .FirstOrDefault();
                if (commandTextAssignment is null)
                {
                    // Query is not assigned to the CommandText prop
                    return(SqlStatement.StatementNotFound);
                }

                if (commandTextAssignment.Right.IsKind(SyntaxKind.StringLiteralExpression))
                {
                    return(new SqlStatement(
                               statement: ExtractQuery(commandTextAssignment.Right.ToString()),
                               location: commandTextAssignment.Right.GetLocation()));
                }
                else if (commandTextAssignment.Right.IsKind(SyntaxKind.IdentifierName))
                {
                    var varDeclarator = FindVariableDeclaratorInNodes(
                        nodes: npgsqlCommandCtor.Ancestors(),
                        variableName: commandTextAssignment.Right.ToString());
                    var varAssignments = FindVariableAssignmentsInNodes(
                        nodes: npgsqlCommandCtor.Ancestors(),
                        variableName: commandTextAssignment.Right.ToString());

                    if (varAssignments.Any())
                    {
                        var declarationSymbol  = context.SemanticModel.GetDeclaredSymbol(varDeclarator);
                        int declarationLine    = declarationSymbol.Locations.First().GetLineSpan().StartLinePosition.Line;
                        int commandTextLine    = commandTextAssignment.GetLocation().GetLineSpan().StartLinePosition.Line;
                        var variableAssignment = varAssignments
                                                 .OrderBy(assignment =>
                        {
                            int assignmentLine = assignment.GetLocation().GetLineSpan().StartLinePosition.Line;
                            return(Math.Abs(commandTextLine - assignmentLine));
                        })
                                                 .First();

                        int assignmentLine = variableAssignment.GetLocation().GetLineSpan().StartLinePosition.Line;
                        if (Math.Abs(commandTextLine - assignmentLine) < Math.Abs(commandTextLine - declarationLine))
                        {
                            return(new SqlStatement(
                                       statement: ExtractQuery(variableAssignment.Right.ToString()),
                                       location: variableAssignment.Right.GetLocation()));
                        }
                    }

                    // The syntax used to assign a value to NpgsqlCommand.CommandText is not supported
                    return(new SqlStatement(
                               statement: ExtractQuery(varDeclarator.Initializer.Value.ToString()),
                               location: varDeclarator.Initializer.Value.GetLocation()));
                }

                return(default);
        public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            SyntaxNode newRoot;
            if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
            {
                var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
                var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
                var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
                newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
                    ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
                    : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
            {
                var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
                var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
                var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
                newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
            {
                var identifierName = GetIdentifierName(objectCreation, semanticModel);

                var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
                    .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
                    .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));

                var arg = objectCreation.Parent as ArgumentSyntax;
                var args = objectCreation.Parent.Parent as ArgumentListSyntax;
                var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));

                StatementSyntax statement = objectCreation.FirstAncestorOfType<ExpressionStatementSyntax>();
                if (statement != null)
                {
                    var exprStatement = statement.ReplaceNode(args, newArgs);
                    var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
                        .WithDeclaration(variableDeclaration);
                    return root.ReplaceNode(statement, newUsingStatment);
                }

                statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
                var newStatement = statement.ReplaceNode(args, newArgs);
                var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
                var usingBlock = SyntaxFactory.Block(statementsForUsing);
                var usingStatement = CreateUsingStatement(newStatement, usingBlock)
                    .WithDeclaration(variableDeclaration);
                var statementsToReplace = new List<StatementSyntax> { statement };
                statementsToReplace.AddRange(statementsForUsing.Skip(1));
                newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                var newVariableName = objectCreation.Type.ToString();
                var newVariableNameParts = newVariableName.Split('.');
                newVariableName = newVariableNameParts[newVariableNameParts.Length - 1].ToLowerCaseFirstLetter();
                var parentStatement = objectCreation.Parent.FirstAncestorOrSelfThatIsAStatement();
                var originalName = newVariableName;
                for (int nameIncrement = 1; ; nameIncrement++)
                {
                    var speculativeSymbol = semanticModel.GetSpeculativeSymbolInfo(parentStatement.GetLocation().SourceSpan.Start, SyntaxFactory.IdentifierName(newVariableName), SpeculativeBindingOption.BindAsExpression);
                    if (speculativeSymbol.Symbol == null) break;
                    newVariableName = originalName + nameIncrement;
                }
                var newVariable = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("var"),
                    SyntaxFactory.SeparatedList(new[] {
                        SyntaxFactory.VariableDeclarator(newVariableName).WithInitializer(SyntaxFactory.EqualsValueClause(objectCreation))
                    })));
                newRoot = root.TrackNodes(parentStatement, objectCreation);
                newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(objectCreation), SyntaxFactory.IdentifierName(newVariableName));
                var newTrackedParentStatement = newRoot.GetCurrentNode(parentStatement);
                newRoot = newRoot.InsertNodesBefore(newTrackedParentStatement, new[] { newVariable });
                var statement = (LocalDeclarationStatementSyntax)newRoot.GetCurrentNode(parentStatement).GetPreviousStatement();
                var variableDeclaration = statement.Declaration;
                var variableDeclarator = variableDeclaration.Variables.First();
                newRoot = CreateRootWithUsing(newRoot, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
            }
            else
            {
                newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
            }
            return newRoot;
        }
        public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
        {
            SyntaxNode newRoot;

            if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
            {
                var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
                var statement            = assignmentExpression.Parent as ExpressionStatementSyntax;
                var identitySymbol       = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
                newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
                    ? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
                    : CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
            {
                var variableDeclarator  = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
                var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
                var statement           = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
                newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.Argument))
            {
                var identifierName = GetIdentifierName(objectCreation, semanticModel);

                var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.IdentifierName(@"var"))
                                          .WithVariables(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(SyntaxFactory.Identifier(identifierName))
                                                                                              .WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.Token(SyntaxKind.EqualsToken), objectCreation))));

                var arg     = objectCreation.Parent as ArgumentSyntax;
                var args    = objectCreation.Parent.Parent as ArgumentListSyntax;
                var newArgs = args.ReplaceNode(arg, arg.WithExpression(SyntaxFactory.IdentifierName(identifierName)));

                StatementSyntax statement = objectCreation.FirstAncestorOfType <ExpressionStatementSyntax>();
                if (statement != null)
                {
                    var exprStatement    = statement.ReplaceNode(args, newArgs);
                    var newUsingStatment = CreateUsingStatement(exprStatement, SyntaxFactory.Block(exprStatement))
                                           .WithDeclaration(variableDeclaration);
                    return(root.ReplaceNode(statement, newUsingStatment));
                }

                statement = (StatementSyntax)objectCreation.Ancestors().First(node => node is StatementSyntax);
                var newStatement = statement.ReplaceNode(args, newArgs);
                var statementsForUsing = new[] { newStatement }.Concat(GetChildStatementsAfter(statement));
                var usingBlock     = SyntaxFactory.Block(statementsForUsing);
                var usingStatement = CreateUsingStatement(newStatement, usingBlock)
                                     .WithDeclaration(variableDeclaration);
                var statementsToReplace = new List <StatementSyntax> {
                    statement
                };
                statementsToReplace.AddRange(statementsForUsing.Skip(1));
                newRoot = root.ReplaceNodes(statementsToReplace, (node, _) => node.Equals(statement) ? usingStatement : null);
            }
            else if (objectCreation.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                var newVariableName      = objectCreation.Type.ToString();
                var newVariableNameParts = newVariableName.Split('.');
                newVariableName = newVariableNameParts[newVariableNameParts.Length - 1].ToLowerCaseFirstLetter();
                var parentStatement = objectCreation.Parent.FirstAncestorOrSelfThatIsAStatement();
                var originalName    = newVariableName;
                for (int nameIncrement = 1; ; nameIncrement++)
                {
                    var speculativeSymbol = semanticModel.GetSpeculativeSymbolInfo(parentStatement.GetLocation().SourceSpan.Start, SyntaxFactory.IdentifierName(newVariableName), SpeculativeBindingOption.BindAsExpression);
                    if (speculativeSymbol.Symbol == null)
                    {
                        break;
                    }
                    newVariableName = originalName + nameIncrement;
                }
                var newVariable = SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("var"),
                                                                                                            SyntaxFactory.SeparatedList(new[] {
                    SyntaxFactory.VariableDeclarator(newVariableName).WithInitializer(SyntaxFactory.EqualsValueClause(objectCreation))
                })));
                newRoot = root.TrackNodes(parentStatement, objectCreation);
                newRoot = newRoot.ReplaceNode(newRoot.GetCurrentNode(objectCreation), SyntaxFactory.IdentifierName(newVariableName));
                var newTrackedParentStatement = newRoot.GetCurrentNode(parentStatement);
                newRoot = newRoot.InsertNodesBefore(newTrackedParentStatement, new[] { newVariable });
                var statement           = (LocalDeclarationStatementSyntax)newRoot.GetCurrentNode(parentStatement).GetPreviousStatement();
                var variableDeclaration = statement.Declaration;
                var variableDeclarator  = variableDeclaration.Variables.First();
                newRoot = CreateRootWithUsing(newRoot, statement, u => u.WithDeclaration(variableDeclaration.WithoutLeadingTrivia()));
            }
            else
            {
                newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
            }
            return(newRoot);
        }