【发布时间】:2014-08-14 22:55:44
【问题描述】:
我正在尝试使用自定义语法重写器删除代码中多余的分号。
public class Sample
{
public void Foo()
{
Console.WriteLine("Foo");
;
}
}
以下语法重写器涵盖了 Sample 类中的大多数场景。
public class EmptyStatementRemoval : CSharpSyntaxRewriter
{
public override SyntaxNode VisitEmptyStatement(EmptyStatementSyntax node)
{
return null;
}
}
但是,当分号有前导或尾随琐事时,从 VisitEmptyStatement 方法返回 null 会删除琐事,这是无意的。
public class Sample
{
public void Foo()
{
Console.WriteLine("Foo");
#region SomeRegion
//Some other code
#endregion
;
}
}
我无法确定如何返回只有前导和尾随琐事删除分号的节点。我尝试使用 node.WithSemicolonToken(SyntaxToken) 方法将分号标记替换为另一个标记,结果证明只接受 SyntaxKind.SemicolonToken 类型的标记或抛出 ArgumentException。
【问题讨论】:
-
分号是否被认为是
node.GetLeadingTrivia()的一部分? -
不,分号不是琐事。