naturecodevoid (ping on reply)
naturecodevoid (ping on reply)
Explore posts from servers
CC#
Created by naturecodevoid (ping on reply) on 6/17/2023 in #help
❔ 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?
3 replies