❔ CSharpSyntaxRewriter seems to not visit all types of SyntaxNode

Hi, I'm trying to use CSharpSyntaxRewriter to refactor/rename a class. It was working fine when I have either VisitClassDeclaration or VisitIdentifierName overridden and implemented. However, if I have both of the implemented at the same time, only VisitClassDeclaration gets run.
public class Rewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
Console.WriteLine("class " + node.Identifier.Text);
if (node.Identifier.Text == "Test")
{
return node.WithIdentifier(Identifier("NotTest"));
}
return node;
}

public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
{
Console.WriteLine("id " + node.Identifier.Text);
if (node.Identifier.Text == "Test")
{
return node.WithIdentifier(Identifier("NotTest"));
}
return node;
}
}
public class Rewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
{
Console.WriteLine("class " + node.Identifier.Text);
if (node.Identifier.Text == "Test")
{
return node.WithIdentifier(Identifier("NotTest"));
}
return node;
}

public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node)
{
Console.WriteLine("id " + node.Identifier.Text);
if (node.Identifier.Text == "Test")
{
return node.WithIdentifier(Identifier("NotTest"));
}
return node;
}
}
Only the class logs are triggered, but if I comment out VisitClassDeclaration, identifier logs are triggered Do I need to make a new class that extends CSharpSyntaxRewriter for each SyntaxNode I want to visit?
2 Replies
naturecodevoid (ping on reply)
nevermind, found out that Roslynator has rename symbol capabilities
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server