Skip to content

Commit

Permalink
Fix: Don't use using in generated code (#129)
Browse files Browse the repository at this point in the history
* Remove usage of using System and replace with Global

* Update snapshots

* Test 2.1 just in case
  • Loading branch information
andrewlock authored Jan 26, 2025
1 parent 07143f8 commit 941ca0b
Show file tree
Hide file tree
Showing 44 changed files with 1,046 additions and 1,189 deletions.
1 change: 1 addition & 0 deletions .github/workflows/BuildAndPack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
7.0.x
6.0.x
3.1.x
2.1.x
- run: dotnet new globaljson --sdk-version "8.0.402" --force
- name: Cache .nuke/temp, ~/.nuget/packages
Expand Down
56 changes: 25 additions & 31 deletions src/NetEscapades.EnumGenerators/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ public class EnumExtensionsAttribute<T> : System.Attribute
public static (string Content, string HintName) GenerateExtensionClass(in EnumToGenerate enumToGenerate)
{
var sb = new StringBuilder();
sb
.Append(Header)
.Append(@"
#if NETCOREAPP && !NETCOREAPP2_0 && !NETCOREAPP1_1 && !NETCOREAPP1_0
using System;
#endif
");
sb.AppendLine(Header);
if (!string.IsNullOrEmpty(enumToGenerate.Namespace))
{
sb.Append(@"
Expand Down Expand Up @@ -312,7 +306,7 @@ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
/// </summary>
/// <param name=""name"">The name to check if it's defined</param>
/// <returns><c>true</c> if a member with the name exists in the enumeration, <c>false</c> otherwise</returns>
public static bool IsDefined(in ReadOnlySpan<char> name) => IsDefined(name, allowMatchingMetadataAttribute: false);
public static bool IsDefined(in global::System.ReadOnlySpan<char> name) => IsDefined(name, allowMatchingMetadataAttribute: false);
/// <summary>
/// Returns a boolean telling whether an enum with the given name exists in the enumeration,
Expand All @@ -324,7 +318,7 @@ public static bool IsDefined(string name, bool allowMatchingMetadataAttribute)
/// <param name=""allowMatchingMetadataAttribute"">If <c>true</c>, considers the value of metadata attributes,otherwise ignores them</param>
/// <returns><c>true</c> if a member with the name exists in the enumeration, or a member is decorated
/// with a <c>[Display]</c> attribute with the name, <c>false</c> otherwise</returns>
public static bool IsDefined(in ReadOnlySpan<char> name, bool allowMatchingMetadataAttribute)
public static bool IsDefined(in global::System.ReadOnlySpan<char> name, bool allowMatchingMetadataAttribute)
{");

if (enumToGenerate.IsDisplayAttributeUsed)
Expand All @@ -340,9 +334,9 @@ public static bool IsDefined(in ReadOnlySpan<char> name, bool allowMatchingMetad
if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true })
{
sb.Append(@"
ReadOnlySpan<char> current when current.Equals(")
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, ")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true))
.Append(@".AsSpan(), global::System.StringComparison.Ordinal) => true,");
.Append(@", global::System.StringComparison.Ordinal) => true,");
}
}

Expand All @@ -364,9 +358,9 @@ ReadOnlySpan<char> current when current.Equals(")
foreach (var member in enumToGenerate.Names)
{
sb.Append(@"
ReadOnlySpan<char> current when current.Equals(nameof(").Append(fullyQualifiedName).Append('.')
global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(").Append(fullyQualifiedName).Append('.')
.Append(member.Key)
.Append(@").AsSpan(), global::System.StringComparison.Ordinal) => true,");
.Append(@"), global::System.StringComparison.Ordinal) => true,");
}

sb.Append(@"
Expand Down Expand Up @@ -630,7 +624,7 @@ private static bool TryParseWithCase(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name)
in global::System.ReadOnlySpan<char> name)
=> TryParse(name, out var value, false, false) ? value : ThrowValueNotFound(name.ToString());
/// <summary>
Expand All @@ -645,7 +639,7 @@ private static bool TryParseWithCase(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
bool ignoreCase)
=> TryParse(name, out var value, ignoreCase, false) ? value : ThrowValueNotFound(name.ToString());
Expand All @@ -663,7 +657,7 @@ private static bool TryParseWithCase(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
bool ignoreCase,
bool allowMatchingMetadataAttribute)
=> TryParse(name, out var value, ignoreCase, allowMatchingMetadataAttribute) ? value : ThrowValueNotFound(name.ToString());
Expand All @@ -684,7 +678,7 @@ public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
out ").Append(fullyQualifiedName).Append(@" value)
=> TryParse(name, out value, false, false);");
sb.Append(@"
Expand All @@ -706,7 +700,7 @@ public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
out ").Append(fullyQualifiedName).Append(@" value,
bool ignoreCase)
=> TryParse(name, out value, ignoreCase, false);");
Expand All @@ -732,7 +726,7 @@ public static bool TryParse(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
out ").Append(fullyQualifiedName).Append(@" result,
bool ignoreCase,
bool allowMatchingMetadataAttribute)
Expand All @@ -744,7 +738,7 @@ private static bool TryParseIgnoreCase(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
out ").Append(fullyQualifiedName).Append(@" result,
bool allowMatchingMetadataAttribute)
{");
Expand All @@ -761,9 +755,9 @@ private static bool TryParseIgnoreCase(
if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true })
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(")
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, ")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(
@".AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
@", global::System.StringComparison.OrdinalIgnoreCase):
result = ").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand All @@ -783,15 +777,15 @@ private static bool TryParseIgnoreCase(
foreach (var member in enumToGenerate.Names)
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(nameof(").Append(fullyQualifiedName).Append('.')
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(").Append(fullyQualifiedName).Append('.')
.Append(member.Key).Append(
@").AsSpan(), global::System.StringComparison.OrdinalIgnoreCase):
@"), global::System.StringComparison.OrdinalIgnoreCase):
result = ").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}

sb.Append(@"
case ReadOnlySpan<char> current when ").Append(enumToGenerate.UnderlyingType).Append(
case global::System.ReadOnlySpan<char> current when ").Append(enumToGenerate.UnderlyingType).Append(
@".TryParse(name, out var numericResult):
result = (").Append(fullyQualifiedName).Append(@")numericResult;
return true;
Expand All @@ -805,7 +799,7 @@ private static bool TryParseWithCase(
#if NETCOREAPP3_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]
#endif
in ReadOnlySpan<char> name,
in global::System.ReadOnlySpan<char> name,
out ").Append(fullyQualifiedName).Append(@" result,
bool allowMatchingMetadataAttribute)
{");
Expand All @@ -822,9 +816,9 @@ private static bool TryParseWithCase(
if (member.Value is { DisplayName: { } dn, IsDisplayNameTheFirstPresence: true })
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(")
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, ")
.Append(SymbolDisplay.FormatLiteral(dn, quote: true)).Append(
@".AsSpan(), global::System.StringComparison.Ordinal):
@", global::System.StringComparison.Ordinal):
result = ").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}
Expand All @@ -844,15 +838,15 @@ private static bool TryParseWithCase(
foreach (var member in enumToGenerate.Names)
{
sb.Append(@"
case ReadOnlySpan<char> current when current.Equals(nameof(").Append(fullyQualifiedName).Append('.')
case global::System.ReadOnlySpan<char> current when global::System.MemoryExtensions.Equals(current, nameof(").Append(fullyQualifiedName).Append('.')
.Append(member.Key).Append(
@").AsSpan(), global::System.StringComparison.Ordinal):
@"), global::System.StringComparison.Ordinal):
result = ").Append(fullyQualifiedName).Append('.').Append(member.Key).Append(@";
return true;");
}

sb.Append(@"
case ReadOnlySpan<char> current when ").Append(enumToGenerate.UnderlyingType).Append(
case global::System.ReadOnlySpan<char> current when ").Append(enumToGenerate.UnderlyingType).Append(
@".TryParse(name, out var numericResult):
result = (").Append(fullyQualifiedName).Append(@")numericResult;
return true;
Expand Down
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<NoWarn>NU1901;NU1902;NU1903;NU1904</NoWarn>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net48;netcoreapp3.1;$(TargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net48;netcoreapp2.1;netcoreapp3.1;$(TargetFrameworks)</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<!-- Ignore 2.1.0 advisories -->
Expand Down
Loading

0 comments on commit 941ca0b

Please sign in to comment.