@@ -59,22 +59,22 @@ private static async Task<Document> ConvertCodeAsync(Document document, Cancella
5959
6060 // Always use the latest updatedRoot as input for the next transformation
6161 var updatedRoot = UpdateInitializeDispose ( compilation , root ) ;
62- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
62+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
6363
6464 updatedRoot = UpdateClassAttributes ( compilation , updatedRoot ) ;
65- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
65+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
6666
6767 updatedRoot = RemoveInterfacesAndBaseClasses ( compilation , updatedRoot ) ;
68- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
68+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
6969
7070 updatedRoot = ConvertTheoryData ( compilation , updatedRoot ) ;
71- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
71+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
7272
7373 updatedRoot = ConvertTestOutputHelpers ( ref compilation , ref syntaxTree , updatedRoot ) ;
74- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
74+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
7575
7676 updatedRoot = RemoveUsingDirectives ( updatedRoot ) ;
77- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , updatedRoot ) ;
77+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref updatedRoot ) ;
7878
7979 // Apply all changes in one step
8080 return document . WithSyntaxRoot ( updatedRoot ) ;
@@ -104,7 +104,7 @@ private static SyntaxNode ConvertTestOutputHelpers(ref Compilation compilation,
104104 )
105105 ) ;
106106
107- UpdateSyntaxTrees ( ref compilation , ref syntaxTree , currentRoot ) ;
107+ UpdateSyntaxTrees ( ref compilation , ref syntaxTree , ref currentRoot ) ;
108108 compilationValue = compilation ;
109109 }
110110
@@ -494,10 +494,15 @@ public override SyntaxNode VisitAttributeList(AttributeListSyntax node)
494494 SyntaxFactory . IdentifierName ( "Obsolete" ) ) ) ] ,
495495 _ => [ attr ]
496496 } ;
497-
497+
498498 newAttributes . AddRange ( converted ) ;
499499 }
500500
501+ if ( node . Attributes . SequenceEqual ( newAttributes ) )
502+ {
503+ return node ;
504+ }
505+
501506 // Preserve original trivia instead of forcing elastic trivia
502507 return SyntaxFactory . AttributeList ( SyntaxFactory . SeparatedList ( newAttributes ) )
503508 . WithLeadingTrivia ( node . GetLeadingTrivia ( ) )
@@ -718,9 +723,24 @@ public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
718723 }
719724 }
720725
721- private static void UpdateSyntaxTrees ( ref Compilation compilation , ref SyntaxTree syntaxTree , SyntaxNode updatedRoot )
726+ private static void UpdateSyntaxTrees ( ref Compilation compilation , ref SyntaxTree syntaxTree , ref SyntaxNode updatedRoot )
722727 {
723- compilation = compilation . ReplaceSyntaxTree ( syntaxTree , updatedRoot . SyntaxTree ) ;
724- syntaxTree = updatedRoot . SyntaxTree ;
728+ var parseOptions = syntaxTree . Options ;
729+ var newSyntaxTree = updatedRoot . SyntaxTree ;
730+
731+ // If the parse options differ, re-parse the updatedRoot with the correct options
732+ if ( ! Equals ( newSyntaxTree . Options , parseOptions ) )
733+ {
734+ newSyntaxTree = CSharpSyntaxTree . ParseText (
735+ updatedRoot . ToFullString ( ) ,
736+ ( CSharpParseOptions ) parseOptions ,
737+ syntaxTree . FilePath
738+ ) ;
739+ }
740+
741+ compilation = compilation . ReplaceSyntaxTree ( syntaxTree , newSyntaxTree ) ;
742+ syntaxTree = newSyntaxTree ;
743+
744+ updatedRoot = newSyntaxTree . GetRoot ( ) ;
725745 }
726746}
0 commit comments