Skip to content

Commit

Permalink
Fixed source-generated class names (#7819)
Browse files Browse the repository at this point in the history
  • Loading branch information
CronKz authored and michaelstaib committed Dec 16, 2024
1 parent 3d5716c commit 40c64e8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void Generate(
{
if (syntaxInfo is DataLoaderModuleInfo module)
{
return module;
return new DataLoaderModuleInfo(GeneratorUtils.SanitizeIdentifier(module.ModuleName));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using HotChocolate.Types.Analyzers.Models;
using Microsoft.CodeAnalysis;

Expand All @@ -16,7 +17,7 @@ public static ModuleInfo GetModuleInfo(
if (syntaxInfo is ModuleInfo module)
{
defaultModule = false;
return module;
return new ModuleInfo(SanitizeIdentifier(module.ModuleName), module.Options);
}
}

Expand Down Expand Up @@ -61,7 +62,7 @@ public static DataLoaderDefaultsInfo GetDataLoaderDefaults(
public static string CreateModuleName(string? assemblyName)
=> assemblyName is null
? "AssemblyTypes"
: assemblyName.Split('.').Last() + "Types";
: SanitizeIdentifier(assemblyName.Split('.').Last()) + "Types";

public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymbol type)
{
Expand Down Expand Up @@ -104,4 +105,18 @@ public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymb

return defaultValue.ToString();
}

public static string SanitizeIdentifier(string input)
{
Regex invalidCharsRegex = new("[^a-zA-Z0-9]", RegexOptions.Compiled);

var sanitized = invalidCharsRegex.Replace(input, "_");

if (!char.IsLetter(sanitized[0]))
{
sanitized = "_" + sanitized.Substring(1);
}

return sanitized;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Snapshot GetGeneratedSourceSnapshot([StringSyntax("csharp")] strin
return GetGeneratedSourceSnapshot([sourceText]);
}

public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts)
public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string? assemblyName = "Tests")
{
IEnumerable<PortableExecutableReference> references =
[
Expand All @@ -49,7 +49,7 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts)

// Create a Roslyn compilation for the syntax tree.
var compilation = CSharpCompilation.Create(
assemblyName: "Tests",
assemblyName: assemblyName,
syntaxTrees: sourceTexts.Select(s => CSharpSyntaxTree.ParseText(s)),
references);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,20 @@ public static async Task<IReadOnlyDictionary<int, object>> GetObjectByIdAAsync(
"""
]).MatchMarkdownAsync();
}

[Fact]
public async Task GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot()
{
await TestHelper.GetGeneratedSourceSnapshot(
[
"""
using HotChocolate.Types;
namespace TestNamespace;
internal class ATestBType: ObjectType<ATestB>;
internal record ATestB(int Id);
"""
], assemblyName:"Custom-Module").MatchMarkdownAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot

```csharp
// <auto-generated/>
#nullable enable
#pragma warning disable

using System;
using System.Runtime.CompilerServices;
using HotChocolate;
using HotChocolate.Types;
using HotChocolate.Execution.Configuration;

namespace Microsoft.Extensions.DependencyInjection
{
public static partial class Custom_ModuleTypesRequestExecutorBuilderExtensions
{
public static IRequestExecutorBuilder AddCustom_ModuleTypes(this IRequestExecutorBuilder builder)
{
builder.AddType<global::TestNamespace.ATestBType>();
return builder;
}
}
}

```

0 comments on commit 40c64e8

Please sign in to comment.