Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore obsolete usages in the enum extensions #111

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ namespace ").Append(enumToGenerate.Namespace).Append(@"
var fullyQualifiedName = $"global::{enumToGenerate.FullyQualifiedName}";

sb.Append(@"
#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref=""").Append(fullyQualifiedName).Append($@""" />
/// </summary>
Expand Down Expand Up @@ -887,7 +889,9 @@ public static string[] GetNames()
sb.Append(@"
};
}
}");
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums");
if (!string.IsNullOrEmpty(enumToGenerate.Namespace))
{
sb.Append(@"
Expand Down Expand Up @@ -919,6 +923,9 @@ public InterceptsLocationAttribute(int version, string data)
}
}
}

#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
namespace NetEscapades.EnumGenerators
{
static file class EnumInterceptors
Expand Down Expand Up @@ -970,6 +977,8 @@ static file class EnumInterceptors
$$"""
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
""");
return sb.ToString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public enum StringTesting
{
[System.ComponentModel.Description("Quotes \"")] Quotes,
[System.ComponentModel.Description(@"Literal Quotes """)] LiteralQuotes,
[Obsolete]
[System.ComponentModel.Description("Backslash \\")] Backslash,
[System.ComponentModel.Description(@"LiteralBackslash \")] BackslashLiteral,
[System.ComponentModel.Description("Line\nBreak")] LineBreak,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public enum StringTesting

[System.ComponentModel.Description(@"LiteralBackslash \")]
BackslashLiteral,

[System.ComponentModel.Description("Line\nBreak")]
LineBreak,
}

[EnumExtensions(IsInterceptable = false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ void AssertValue(EnumWithDisplayNameInNamespace value)
#endif
public void CallingToStringIsIntercepted_StringTesting()
{
#pragma warning disable CS0612
var result1 = StringTesting.Backslash.ToString();
var result2 = StringTesting.Backslash.ToStringFast();
#pragma warning restore CS0612
Assert.Equal(result1, result2);
}

Expand Down
92 changes: 92 additions & 0 deletions tests/NetEscapades.EnumGenerators.Tests/EnumGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,96 @@ public Task CanGenerateExternalEnumExtensionsWithCustomNamespaceAndName()
Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotGenerateWarningsForObsoleteMembers_CS0612_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[EnumExtensions]
public enum MyEnum
{
First,
[Obsolete]
Second,
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotGenerateWarningsForObsoleteMembers_CS0618_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[EnumExtensions]
public enum MyEnum
{
First,
[Obsolete("This is obsolete")]
Second,
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotGenerateWarningsForObsoleteEnums_CS0612_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[Obsolete]
[EnumExtensions]
public enum MyEnum
{
First,
Second,
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotGenerateWarningsForObsoleteEnums_CS0618_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[Obsolete("This is obsolete", false)]
[EnumExtensions]
public enum MyEnum
{
First,
Second,
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}
}
88 changes: 88 additions & 0 deletions tests/NetEscapades.EnumGenerators.Tests/InterceptorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,94 @@ public void MyTest()
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0612_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[EnumExtensions]
public enum MyEnum
{
First,
[Obsolete]
Second,
}

[EnumExtensions]
public enum OtherEnum
{
First,
Second,
}

internal class InnerClass
{
public void MyTest()
{
#pragma warning disable CS0612
#pragma warning disable CS0618
var var1 = OtherEnum.Second.ToString();
var var2 = MyEnum.Second.ToString();
#pragma warning restore CS0612
#pragma warning restore CS0618
}
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}


[Fact]
public Task DoesNotGenerateWarningsForUseOfObsoleteEnums_CS0618_Issue97()
{
const string input =
"""
using System;
using NetEscapades.EnumGenerators;

[EnumExtensions]
public enum MyEnum
{
First,
[Obsolete("This is obsolete")]
Second,
}

[EnumExtensions]
[Obsolete("This is obsolete")]
public enum OtherEnum
{
First,
Second,
}

internal class InnerClass
{
public void MyTest()
{
#pragma warning disable CS0612
#pragma warning disable CS0618
var var1 = OtherEnum.Second.ToString();
var var2 = MyEnum.Second.ToString();
#pragma warning restore CS0612
#pragma warning restore CS0618
}
}

""";
var (diagnostics, output) = TestHelpers.GetGeneratedOutput<EnumGenerator>(new(input));

Assert.Empty(diagnostics);
return Verifier.Verify(output).ScrubExpectedChanges().UseDirectory("Snapshots");
}

[Fact]
public Task DoesNotInterceptToStringWhenOldCsharp()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the NetEscapades.EnumGenerators source generator
//
Expand All @@ -14,6 +14,8 @@ using System;

namespace MyTestNameSpace
{
#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref="global::MyTestNameSpace.MyEnum" />
/// </summary>
Expand Down Expand Up @@ -518,4 +520,6 @@ namespace MyTestNameSpace
};
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the NetEscapades.EnumGenerators source generator
//
Expand All @@ -14,6 +14,8 @@ using System;

namespace MyTestNameSpace
{
#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref="global::MyTestNameSpace.MyEnum" />
/// </summary>
Expand Down Expand Up @@ -488,4 +490,6 @@ namespace MyTestNameSpace
};
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the NetEscapades.EnumGenerators source generator
//
Expand All @@ -12,6 +12,8 @@
using System;
#endif

#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref="global::MyEnum" />
/// </summary>
Expand Down Expand Up @@ -485,4 +487,6 @@ using System;
nameof(global::MyEnum.Second),
};
}
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the NetEscapades.EnumGenerators source generator
//
Expand All @@ -14,6 +14,8 @@ using System;

namespace MyTestNameSpace
{
#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref="global::MyTestNameSpace.InnerClass.MyEnum" />
/// </summary>
Expand Down Expand Up @@ -488,4 +490,6 @@ namespace MyTestNameSpace
};
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the NetEscapades.EnumGenerators source generator
//
Expand All @@ -14,6 +14,8 @@ using System;

namespace MyTestNameSpace
{
#pragma warning disable CS0612 // Ignore usages of obsolete members or enums
#pragma warning disable CS0618 // Ignore usages of obsolete members or enums
/// <summary>
/// Extension methods for <see cref="global::MyTestNameSpace.MyEnum" />
/// </summary>
Expand Down Expand Up @@ -488,4 +490,6 @@ namespace MyTestNameSpace
};
}
}
#pragma warning restore CS0612 // Ignore usages of obsolete members or enums
#pragma warning restore CS0618 // Ignore usages of obsolete members or enums
}
Loading
Loading