public static Task <Document> RemoveRedundantAssignmentAfterLocalDeclarationAsync( Document document, VariableDeclaratorSyntax declarator, CancellationToken cancellationToken = default) { var declaration = (VariableDeclarationSyntax)declarator.Parent; var localDeclaration = (LocalDeclarationStatementSyntax)declaration.Parent; StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(localDeclaration); SyntaxList <StatementSyntax> statements = statementsInfo.Statements; int index = statements.IndexOf(localDeclaration); StatementSyntax nextStatement = statements[index + 1]; var expressionStatement = (ExpressionStatementSyntax)nextStatement; var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression; ExpressionSyntax right = assignment.Right; EqualsValueClauseSyntax initializer = declarator.Initializer; ExpressionSyntax value = initializer?.Value; VariableDeclaratorSyntax newDeclarator = (value != null) ? declarator.ReplaceNode(value, right) : declarator.WithInitializer(EqualsValueClause(right)); LocalDeclarationStatementSyntax newLocalDeclaration = localDeclaration.ReplaceNode(declarator, newDeclarator); SyntaxTriviaList trailingTrivia = localDeclaration.GetTrailingTrivia(); if (!trailingTrivia.IsEmptyOrWhitespace()) { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(trailingTrivia.Concat(nextStatement.GetTrailingTrivia())); } else { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(nextStatement.GetTrailingTrivia()); } SyntaxTriviaList leadingTrivia = nextStatement.GetLeadingTrivia(); if (!leadingTrivia.IsEmptyOrWhitespace()) { newLocalDeclaration = newLocalDeclaration.WithLeadingTrivia(newLocalDeclaration.GetLeadingTrivia().Concat(leadingTrivia)); } SyntaxList <StatementSyntax> newStatements = statements .Replace(localDeclaration, newLocalDeclaration) .RemoveAt(index + 1); return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken)); }
public static Task <Document> RefactorAsync( Document document, VariableDeclaratorSyntax declarator, CancellationToken cancellationToken) { var declaration = (VariableDeclarationSyntax)declarator.Parent; var localDeclaration = (LocalDeclarationStatementSyntax)declaration.Parent; StatementContainer container = StatementContainer.Create(localDeclaration); SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(localDeclaration); StatementSyntax nextStatement = statements[index + 1]; var expressionStatement = (ExpressionStatementSyntax)nextStatement; var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression; ExpressionSyntax right = assignment.Right; EqualsValueClauseSyntax initializer = declarator.Initializer; ExpressionSyntax value = initializer?.Value; VariableDeclaratorSyntax newDeclarator = (value != null) ? declarator.ReplaceNode(value, right) : declarator.WithInitializer(EqualsValueClause(right)); LocalDeclarationStatementSyntax newLocalDeclaration = localDeclaration.ReplaceNode(declarator, newDeclarator); SyntaxTriviaList trailingTrivia = localDeclaration.GetTrailingTrivia(); if (trailingTrivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(trailingTrivia.Concat(nextStatement.GetTrailingTrivia())); } else { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(nextStatement.GetTrailingTrivia()); } SyntaxTriviaList leadingTrivia = nextStatement.GetLeadingTrivia(); if (leadingTrivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { newLocalDeclaration = newLocalDeclaration.WithLeadingTrivia(newLocalDeclaration.GetLeadingTrivia().Concat(leadingTrivia)); } SyntaxList <StatementSyntax> newStatements = statements .Replace(localDeclaration, newLocalDeclaration) .RemoveAt(index + 1); return(document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken)); }
private FieldVariableTransformationResult TransformFieldVariable( VariableDeclaratorSyntax variableNode, FieldVariableTransformationResult result, bool canCopy) { var variableResult = result.AnalyzationResult; var conversion = variableResult.Conversion; if (conversion == FieldVariableConversion.Ignore || !canCopy) { return(result); } var startVariableSpan = variableResult.Node.Span.Start; variableNode = variableNode.WithAdditionalAnnotations(new SyntaxAnnotation(result.Annotation)); startVariableSpan -= variableNode.SpanStart; // First we need to annotate nodes that will be modified in order to find them later on. // We cannot rely on spans after the first modification as they will change var typeReferencesAnnotations = new List <string>(); foreach (var typeReference in variableResult.TypeReferences.Where(o => o.TypeAnalyzationResult.Conversion == TypeConversion.NewType)) { var reference = typeReference.ReferenceLocation; var refSpanStart = reference.Location.SourceSpan.Start - startVariableSpan; var nameNode = variableNode.GetSimpleName(refSpanStart, reference.Location.SourceSpan.Length, typeReference.IsCref); var annotation = Guid.NewGuid().ToString(); variableNode = variableNode.ReplaceNode(nameNode, nameNode.WithAdditionalAnnotations(new SyntaxAnnotation(annotation))); typeReferencesAnnotations.Add(annotation); } // Modify references foreach (var refAnnotation in typeReferencesAnnotations) { var nameNode = variableNode.GetAnnotatedNodes(refAnnotation).OfType <SimpleNameSyntax>().First(); variableNode = variableNode .ReplaceNode(nameNode, nameNode.WithIdentifier(Identifier(nameNode.Identifier.Value + "Async").WithTriviaFrom(nameNode.Identifier))); } result.Transformed = variableNode; return(result); }
public override SyntaxNode VisitVariableDeclarator(VariableDeclaratorSyntax node) { String name = node.Identifier.ValueText; String replaceName = ""; if (SharedContainer.Instance.nameMap.ContainsKey(name)) { replaceName = SharedContainer.Instance.nameMap[name]; } else { replaceName = SharedContainer.Instance.RandomString(SharedContainer.Instance.nameMap.Count() + 1); SharedContainer.Instance.nameMap[name] = replaceName; } VariableDeclaratorSyntax newSyntax = node.WithIdentifier(Identifier(replaceName)).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()); return(node.ReplaceNode(node, newSyntax)); }
public static async Task <Document> RefactorAsync( Document document, VariableDeclaratorSyntax declarator, CancellationToken cancellationToken) { var declaration = (VariableDeclarationSyntax)declarator.Parent; var localDeclaration = (LocalDeclarationStatementSyntax)declaration.Parent; StatementContainer container; if (StatementContainer.TryCreate(localDeclaration, out container)) { SyntaxList <StatementSyntax> statements = container.Statements; int index = statements.IndexOf(localDeclaration); StatementSyntax nextStatement = statements[index + 1]; var expressionStatement = (ExpressionStatementSyntax)nextStatement; var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression; ExpressionSyntax right = assignment.Right; EqualsValueClauseSyntax initializer = declarator.Initializer; ExpressionSyntax value = initializer?.Value; VariableDeclaratorSyntax newDeclarator = (value != null) ? declarator.ReplaceNode(value, right) : declarator.WithInitializer(EqualsValueClause(right)); LocalDeclarationStatementSyntax newLocalDeclaration = localDeclaration.ReplaceNode(declarator, newDeclarator); SyntaxTriviaList trailingTrivia = nextStatement.GetTrailingTrivia(); IEnumerable <SyntaxTrivia> trivia = container .Node .DescendantTrivia(TextSpan.FromBounds(localDeclaration.Span.End, right.SpanStart)); if (!trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia())) { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(trivia.Concat(trailingTrivia)); } else { newLocalDeclaration = newLocalDeclaration.WithTrailingTrivia(trailingTrivia); } SyntaxList <StatementSyntax> newStatements = statements .Replace(localDeclaration, newLocalDeclaration) .RemoveAt(index + 1); return(await document.ReplaceNodeAsync(container.Node, container.NodeWithStatements(newStatements), cancellationToken).ConfigureAwait(false)); } Debug.Assert(false, ""); return(document); }