Sort and remove (unused) using statements Roslyn script/code? I\'m looking for some .NET/Roslyn (compiler as service) code that can run through a project and sort and remov
For removing statements, check out the FAQ item 30 in the solution in the following directory, in the FAQ.cs file: (Note this is for the June 2012 CTP version of Roslyn).
%userprofile%\Documents\Microsoft Roslyn CTP - June 2012\CSharp\APISampleUnitTestsCS
There is also a FAQ which refers to this project:
http://www.codeplex.com/Download?ProjectName=dlr&DownloadId=386858
Here is the sample rewriter from the sample code, which removes assignment statements.
// Below SyntaxRewriter removes multiple assignement statements from under the
// SyntaxNode being visited.
public class AssignmentStatementRemover : SyntaxRewriter
{
public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
{
SyntaxNode updatedNode = base.VisitExpressionStatement(node);
if (node.Expression.Kind == SyntaxKind.AssignExpression)
{
if (node.Parent.Kind == SyntaxKind.Block)
{
// There is a parent block so it is ok to remove the statement completely.
updatedNode = null;
}
else
{
// The parent context is some statement like an if statement without a block.
// Return an empty statement.
updatedNode = Syntax.EmptyStatement()
.WithLeadingTrivia(updatedNode.GetLeadingTrivia())
.WithTrailingTrivia(updatedNode.GetTrailingTrivia());
}
}
return updatedNode;
}
}