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

Fixed source-generated class names #7819

Merged
merged 4 commits into from
Dec 12, 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
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 @@ -22,7 +22,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 @@ -47,7 +47,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;
}
}
}

```
Loading