Skip to content

Commit

Permalink
Fix issue with CodeFix.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaharPrishMSFT committed Sep 26, 2024
1 parent 95d09d2 commit 9f6d74d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public async Task ObsoleteMethodWithoutEditorBrowsable_ShouldAddAttribute()
{
const string code = """
using System;
using System.ComponentModel;
class TestClass
{
Expand Down Expand Up @@ -61,7 +60,6 @@ public async Task ObsoleteClassWithoutEditorBrowsable_ShouldAddAttribute()
{
const string code = """
using System;
using System.ComponentModel;
[Obsolete]
public class {|#0:ObsoleteClass|}
Expand All @@ -72,7 +70,7 @@ public class {|#0:ObsoleteClass|}
const string fixedCode = """
using System;
using System.ComponentModel;
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
public class ObsoleteClass
Expand All @@ -96,7 +94,6 @@ public async Task ObsoletePropertyWithoutEditorBrowsable_ShouldAddAttribute()
{
const string code = """
using System;
using System.ComponentModel;
class TestClass
{
Expand Down Expand Up @@ -133,7 +130,6 @@ public async Task ObsoleteEventWithoutEditorBrowsable_ShouldAddAttribute()
{
const string code = """
using System;
using System.ComponentModel;
class TestClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ private async Task<Document> AddEditorBrowsableAttributeAsync(Document document,
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

// Track the declaration node so we can find it after modifying the root
root = root.TrackNodes(declarationNode);

// Add using directive for System.ComponentModel if it's not already present
var rootWithUsing = AddUsingDirectiveIfMissing(root, "System.ComponentModel");

// Get the updated declaration node from the new root
var updatedDeclarationNode = rootWithUsing.GetCurrentNode(declarationNode);

// Create the [EditorBrowsable(EditorBrowsableState.Never)] attribute
var editorBrowsableAttribute = SyntaxFactory.Attribute(
SyntaxFactory.ParseName("EditorBrowsable"),
Expand All @@ -75,16 +84,12 @@ private async Task<Document> AddEditorBrowsableAttributeAsync(Document document,
SyntaxFactory.IdentifierName("Never"))))));

var newAttributeList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(editorBrowsableAttribute));
//.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

// Add using directive for System.ComponentModel if it's not already present
var rootWithUsing = AddUsingDirectiveIfMissing(root, "System.ComponentModel");

// Add the attribute to the declaration node
SyntaxNode newDeclarationNode = AddAttributeToDeclaration(declarationNode, newAttributeList);
// Add the attribute to the updated declaration node
var modifiedDeclarationNode = AddAttributeToDeclaration(updatedDeclarationNode, newAttributeList);

// Replace the old node with the new node in the syntax tree
var newRoot = rootWithUsing.ReplaceNode(declarationNode, newDeclarationNode);
var newRoot = rootWithUsing.ReplaceNode(updatedDeclarationNode, modifiedDeclarationNode);

// Return the new document
return document.WithSyntaxRoot(newRoot);
Expand All @@ -97,8 +102,8 @@ private SyntaxNode AddUsingDirectiveIfMissing(SyntaxNode root, string namespaceN
var hasUsingDirective = compilationUnit.Usings.Any(u => u.Name.ToString() == namespaceName);
if (!hasUsingDirective)
{
var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(namespaceName))
.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(namespaceName));
//.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);

compilationUnit = compilationUnit.AddUsings(usingDirective);
return compilationUnit;
Expand Down

0 comments on commit 9f6d74d

Please sign in to comment.