public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) { if (ShouldRewriteClass(node)) { return(MocklisClass.UpdateMocklisClass(Model, node, MocklisSymbols, Model.ClassIsInNullableContext(node))); } return(base.VisitClassDeclaration(node)); }
public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) { if (ShouldRewriteClass(node)) { FoundMocklisClass = true; return(MocklisClass.EmptyMocklisClass(node)); } return(base.VisitClassDeclaration(node)); }
private async Task <Solution> UpdateMocklisClassAsync(Document document, TypeDeclarationSyntax typeDecl, CancellationToken cancellationToken) { if (!(typeDecl is ClassDeclarationSyntax classDecl)) { return(document.Project.Solution); } var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); MocklisSymbols mocklisSymbols = new MocklisSymbols(semanticModel.Compilation); bool isMocklisClass = classDecl.AttributeLists.SelectMany(al => al.Attributes) .Any(a => semanticModel.GetSymbolInfo(a).Symbol.ContainingType.Equals(mocklisSymbols.MocklisClassAttribute)); if (!isMocklisClass) { return(document.Project.Solution); } var classIsInNullableContext = semanticModel.ClassIsInNullableContext(classDecl); var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var emptyClassDecl = MocklisClass.EmptyMocklisClass(classDecl).WithAdditionalAnnotations(new SyntaxAnnotation(MocklisEmptiedClass)); var emptyRoot = oldRoot.ReplaceNode(classDecl, emptyClassDecl); var emptyDoc = document.WithSyntaxRoot(emptyRoot); emptyRoot = await emptyDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); semanticModel = await emptyDoc.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); // And find the class again emptyClassDecl = emptyRoot.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>() .FirstOrDefault(n => n.GetAnnotations(MocklisEmptiedClass).Any()); if (emptyClassDecl == null) { return(document.Project.Solution); } var newClassDecl = MocklisClass.UpdateMocklisClass(semanticModel, emptyClassDecl, mocklisSymbols, classIsInNullableContext); var newRoot = emptyRoot.ReplaceNode(emptyClassDecl, newClassDecl); return(emptyDoc.WithSyntaxRoot(newRoot).Project.Solution); }