Skip to content

Commit c4b045b

Browse files
authored
fix: avoid duplicate using statements (#57)
This PR eliminates duplicate using statements in generated C# code by consolidating namespace declarations and filtering out duplicates. ### Key changes: - Filters out "System" namespace from dynamic using statements to avoid duplication with hardcoded System import - Consolidates hardcoded Mockerade namespace imports with dynamic namespace arrays - Applies deduplication and sorting to all using statement generation
1 parent 0ad80bc commit c4b045b

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

Source/Mockerade.SourceGenerators/Internals/SourceGeneration.ExtensionsClass.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ internal static partial class SourceGeneration
1010
{
1111
public static string GetExtensionClass(string name, Class @class)
1212
{
13-
string[] namespaces = @class.GetClassNamespaces();
13+
string[] namespaces = [.. @class.GetClassNamespaces(), "Mockerade.Checks", "Mockerade.Events", "Mockerade.Setup"];
1414
StringBuilder sb = new();
1515
sb.AppendLine(Header);
16-
foreach (string @namespace in namespaces)
16+
foreach (string @namespace in namespaces.Distinct().OrderBy(n => n))
1717
{
1818
sb.Append("using ").Append(@namespace).AppendLine(";");
1919
}
2020

2121
sb.Append("""
22-
using Mockerade.Checks;
23-
using Mockerade.Events;
24-
using Mockerade.Setup;
2522
2623
namespace Mockerade;
2724

Source/Mockerade.SourceGenerators/Internals/SourceGeneration.MockClass.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ internal static partial class SourceGeneration
1010
{
1111
public static string GetMockClass(string name, MockClass mockClass)
1212
{
13-
string[] namespaces = mockClass.GetAllNamespaces();
13+
string[] namespaces = [.. mockClass.GetAllNamespaces(), "Mockerade.Checks", "Mockerade.Events", "Mockerade.Setup"];
1414
StringBuilder sb = new();
1515
sb.AppendLine(Header);
16-
foreach (string @namespace in namespaces)
16+
foreach (string @namespace in namespaces.Distinct().OrderBy(n => n))
1717
{
1818
sb.Append("using ").Append(@namespace).AppendLine(";");
1919
}
2020

2121
sb.Append("""
22-
using Mockerade.Checks;
23-
using Mockerade.Events;
24-
using Mockerade.Setup;
2522
2623
namespace Mockerade;
2724

Source/Mockerade.SourceGenerators/Internals/SourceGeneration.RegisterMocks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static string RegisterMocks(ICollection<(string Name, MockClass MockClass
1010
var sb = new StringBuilder();
1111
sb.AppendLine(Header);
1212
sb.AppendLine("using System;");
13-
foreach (string? @namespace in mocks.Select(x => x.MockClass.Namespace).Distinct().OrderBy(x => x))
13+
foreach (string? @namespace in mocks.Select(x => x.MockClass.Namespace).Where(x => x != "System").Distinct().OrderBy(x => x))
1414
{
1515
sb.Append("using ").Append(@namespace).AppendLine(";");
1616
}

0 commit comments

Comments
 (0)