Skip to content

Commit

Permalink
Merge pull request #5 from ShaharPrishMSFT/shaharp/keepobsvisible
Browse files Browse the repository at this point in the history
Add option for keeping obsolete members visible.
  • Loading branch information
ShaharPrishMSFT authored Sep 29, 2024
2 parents f819f44 + 891577e commit fbdecfe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

public class CorrectObsolescenceCodeFixTests
{
[Fact]
public async Task ObsoleteMethodWithoutEditorBrowsable_ShouldAddAttribute()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task ObsoleteMethodWithoutEditorBrowsable_ShouldAddAttribute(bool hide)
{
string expectedBrowsable = hide ? "Never" : "Always";
const string code = """
using System;
Expand All @@ -25,14 +28,14 @@ class TestClass
}
""";

const string fixedCode = """
string fixedCode = $$"""
using System;
using System.ComponentModel;
class TestClass
{
[Obsolete]
[EditorBrowsable(EditorBrowsableState.Never)]
[EditorBrowsable(EditorBrowsableState.{{expectedBrowsable}})]
public void ObsoleteMethod()
{
}
Expand All @@ -46,10 +49,10 @@ public void ObsoleteMethod()
.WithArguments("ObsoleteMethod")
};

var sut = CreateSut(code, fixedCode, expectedDiagnostics);
var sut = CreateSut(code, fixedCode, expectedDiagnostics, hide ? DiagnosticsDetails.Obsolescence.HideEquivalenceKey: DiagnosticsDetails.Obsolescence.KeepEquivalenceKey);

sut.CodeFixTestBehaviors = CodeFixTestBehaviors.SkipFixAllInDocumentCheck; // Ensures only the specific fix is applied
sut.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; // If suppression is involved
//sut.CodeFixTestBehaviors = CodeFixTestBehaviors.SkipFixAllInDocumentCheck; // Ensures only the specific fix is applied
//sut.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; // If suppression is involved

await sut.RunAsync();
}
Expand Down Expand Up @@ -377,7 +380,7 @@ public void ObsoleteMethod()
await sut.RunAsync();
}

private VerifyCf.Test CreateSut(string code, string fixedCode, List<DiagnosticResult> expected)
private VerifyCf.Test CreateSut(string code, string fixedCode, List<DiagnosticResult> expected, string equivalencyKey = DiagnosticsDetails.Obsolescence.HideEquivalenceKey)
{
var test = new VerifyCf.Test
{
Expand All @@ -392,6 +395,7 @@ private VerifyCf.Test CreateSut(string code, string fixedCode, List<DiagnosticRe
//MetadataReference.CreateFromFile(typeof(System.ComponentModel.EditorBrowsableAttribute).Assembly.Location),
},
},
CodeActionEquivalenceKey = equivalencyKey,
};

test.ExpectedDiagnostics.AddRange(expected);
Expand Down
3 changes: 3 additions & 0 deletions src/SubtleEngineering.Analyzers/DiagnosticsDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class ExhaustiveInitialization // TVM1030+
public class Obsolescence // TVM 1040+
{
public const string HideObsoleteElementsId = "SE1040";

public const string HideEquivalenceKey = "AddHideEditorBrowsableAttribute";
public const string KeepEquivalenceKey = "AddKeepEditorBrowsableAttribute";
}

public class ExhaustiveEnumSwitch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ n is EnumDeclarationSyntax ||
context.RegisterCodeFix(
CodeAction.Create(
title: "Hide obsolete element from IntelliSense",
createChangedDocument: c => AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c),
equivalenceKey: "AddEditorBrowsableAttribute"),
createChangedDocument: c => AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c, true),
equivalenceKey: DiagnosticsDetails.Obsolescence.HideEquivalenceKey),
diagnostic);
context.RegisterCodeFix(
CodeAction.Create(
title: "Keep obsolete element visible for IntelliSense",
createChangedDocument: c => AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c, false),
equivalenceKey: DiagnosticsDetails.Obsolescence.KeepEquivalenceKey),
diagnostic);
}

private async Task<Document> AddEditorBrowsableAttributeAsync(Document document, SyntaxNode declarationNode, CancellationToken cancellationToken)
private async Task<Document> AddEditorBrowsableAttributeAsync(Document document, SyntaxNode declarationNode, CancellationToken cancellationToken, bool hide)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

Expand All @@ -82,7 +88,7 @@ private async Task<Document> AddEditorBrowsableAttributeAsync(Document document,
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("EditorBrowsableState"),
SyntaxFactory.IdentifierName("Never"))))));
SyntaxFactory.IdentifierName(hide ? "Never" : "Always"))))));

var newAttributeList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(editorBrowsableAttribute));

Expand Down

0 comments on commit fbdecfe

Please sign in to comment.