Skip to content

Commit

Permalink
Merge pull request #83 from nenoNaninu/method_naming_style
Browse files Browse the repository at this point in the history
Method naming style
  • Loading branch information
nenoNaninu authored Feb 9, 2023
2 parents 2c3f671 + a102fd1 commit 6651cb1
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
<DevelopmentDependency>true</DevelopmentDependency>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>

<!-- NuGet -->
<PackageId>TypedSignalR.Client.TypeScript.Analyzer</PackageId>
Expand Down
8 changes: 6 additions & 2 deletions src/TypedSignalR.Client.TypeScript.Generator/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public async Task Transpile(
NamingStyle namingStyle = NamingStyle.CamelCase,
[Option("en", "value (default) / nameString / nameCamel / NamePascal / union / unionCamel / UnionPascal")]
EnumStyle @enum = EnumStyle.Value,
[Option("m", "camelCase (default) / PascalCase / none (The name in C# is used as it is.)")]
MethodStyle method = MethodStyle.CamelCase,
[Option("attr", "The flag whether attributes such as JsonPropertyName should affect transpilation.")]
bool attribute = true)
{
Expand All @@ -50,7 +52,7 @@ public async Task Transpile(
{
var compilation = await this.CreateCompilationAsync(project);

await TranspileCore(compilation, output, newLine, 4, assemblies, serializer, namingStyle, @enum, attribute);
await TranspileCore(compilation, output, newLine, 4, assemblies, serializer, namingStyle, @enum, method, attribute);

_logger.Log(LogLevel.Information, "======== Transpilation is completed. ========");
_logger.Log(LogLevel.Information, "Please check the output folder: {output}", output);
Expand Down Expand Up @@ -89,6 +91,7 @@ private async Task TranspileCore(
SerializerOption serializerOption,
NamingStyle namingStyle,
EnumStyle enumStyle,
MethodStyle methodStyle,
bool enableAttributeReference)
{
var typeMapperProvider = new DefaultTypeMapperProvider(compilation, referencedAssembliesTranspilation);
Expand All @@ -98,12 +101,13 @@ private async Task TranspileCore(
typeMapperProvider.AddTypeMapper(new AsyncEnumerableTypeMapper(compilation));
typeMapperProvider.AddTypeMapper(new ChannelReaderTypeMapper(compilation));

var options = new TranspilationOptions(
var options = new TypedSignalRTranspilationOptions(
compilation,
typeMapperProvider,
serializerOption,
namingStyle,
enumStyle,
methodStyle,
newLine,
indent,
referencedAssembliesTranspilation,
Expand Down
8 changes: 4 additions & 4 deletions src/TypedSignalR.Client.TypeScript/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace TypedSignalR.Client.TypeScript;

internal class ApiGenerator
{
private readonly ITranspilationOptions _transpilationOptions;
private readonly ITypedSignalRTranspilationOptions _options;
private readonly ILogger _logger;
private readonly SpecialSymbols _specialSymbols;

public ApiGenerator(SpecialSymbols specialSymbols, ITranspilationOptions transpilationOptions, ILogger logger)
public ApiGenerator(SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options, ILogger logger)
{
_specialSymbols = specialSymbols;
_transpilationOptions = transpilationOptions;
_options = options;
_logger = logger;
}

Expand All @@ -32,7 +32,7 @@ public IReadOnlyList<GeneratedSourceCode> Generate(IReadOnlyList<INamedTypeSymbo
HubTypes = hubTypes.Select(static x => new TypeMetadata(x)).ToArray(),
ReceiverTypes = receiverTypes.Select(static x => new TypeMetadata(x)).ToArray(),
SpecialSymbols = _specialSymbols,
TranspilationOptions = _transpilationOptions,
Options = _options,
};

return new[] { new GeneratedSourceCode("TypedSignalR.Client/index.ts", template.TransformText().NormalizeNewLines("\n")) };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Tapper;

namespace TypedSignalR.Client.TypeScript;

public interface ITypedSignalRTranspilationOptions : ITranspilationOptions
{
MethodStyle MethodStyle { get; }
}
12 changes: 6 additions & 6 deletions src/TypedSignalR.Client.TypeScript/InterfaceTranspiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace TypedSignalR.Client.TypeScript;
internal class InterfaceTranspiler
{
private readonly SpecialSymbols _specialSymbols;
private readonly ITranspilationOptions _transpilationOptions;
private readonly ITypedSignalRTranspilationOptions _options;
private readonly ILogger _logger;

public InterfaceTranspiler(SpecialSymbols specialSymbols, ITranspilationOptions transpilationOptions, ILogger logger)
public InterfaceTranspiler(SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options, ILogger logger)
{
_specialSymbols = specialSymbols;
_transpilationOptions = transpilationOptions;
_options = options;
_logger = logger;
}

Expand All @@ -37,7 +37,7 @@ public IReadOnlyList<GeneratedSourceCode> Transpile(IEnumerable<INamedTypeSymbol
{
_logger.Log(LogLevel.Information, "Transpile {typename}...", type.ToDisplayString());

AddInterface(type, _specialSymbols, _transpilationOptions, ref codeWriter);
AddInterface(type, _specialSymbols, _options, ref codeWriter);
}

var code = codeWriter.ToString().NormalizeNewLines("\n");
Expand Down Expand Up @@ -85,15 +85,15 @@ private void AddHeader(IGrouping<INamespaceSymbol, INamedTypeSymbol> interfaceTy
private static void AddInterface(
INamedTypeSymbol interfaceSymbol,
SpecialSymbols specialSymbols,
ITranspilationOptions options,
ITypedSignalRTranspilationOptions options,
ref CodeWriter codeWriter)
{
codeWriter.AppendLine($"export type {interfaceSymbol.Name} = {{");

foreach (var method in interfaceSymbol.GetMethods())
{
WriteJSDoc(method, ref codeWriter);
codeWriter.Append($" {method.Name.Format(options.NamingStyle)}(");
codeWriter.Append($" {method.Name.Format(options.MethodStyle)}(");
WriteParameters(method, options, specialSymbols, ref codeWriter);
codeWriter.Append("): ");
WriteReturnType(method, options, specialSymbols, ref codeWriter);
Expand Down
8 changes: 8 additions & 0 deletions src/TypedSignalR.Client.TypeScript/MethodStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TypedSignalR.Client.TypeScript;

public enum MethodStyle
{
None,
CamelCase,
PascalCase
}
31 changes: 23 additions & 8 deletions src/TypedSignalR.Client.TypeScript/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,39 @@ namespace TypedSignalR.Client.TypeScript;

internal static class StringExtensions
{
public static string NormalizeNewLines(this string source, string newline)
public static string NormalizeNewLines(this string text, string newline)
{
return source.Replace("\r\n", "\n").Replace("\n", newline);
return text.Replace("\r\n", "\n").Replace("\n", newline);
}

public static string NormalizeNewLines(this string source)
public static string NormalizeNewLines(this string text)
{
return source.NormalizeNewLines(Environment.NewLine);
return text.NormalizeNewLines(Environment.NewLine);
}

public static string Format(this string source, NamingStyle namingStyle)
public static string Format(this string text, NamingStyle namingStyle)
{
return namingStyle switch
{
NamingStyle.None => source,
NamingStyle.CamelCase => $"{char.ToLower(source[0])}{source[1..]}",
NamingStyle.PascalCase => $"{char.ToUpper(source[0])}{source[1..]}",
NamingStyle.None => text,
NamingStyle.CamelCase => ToCamel(text),
NamingStyle.PascalCase => ToPascal(text),
_ => throw new InvalidOperationException(),
};
}

public static string Format(this string text, MethodStyle methodStyle)
{
return methodStyle switch
{
MethodStyle.None => text,
MethodStyle.CamelCase => ToCamel(text),
MethodStyle.PascalCase => ToPascal(text),
_ => throw new InvalidOperationException(),
};
}

private static string ToCamel(string text) => $"{char.ToLower(text[0])}{text[1..]}";

private static string ToPascal(string text) => $"{char.ToUpper(text[0])}{text[1..]}";
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public partial class ApiTemplate
internal IReadOnlyList<TypeMetadata> HubTypes { get; init; } = default!;
internal IReadOnlyList<TypeMetadata> ReceiverTypes { get; init; } = default!;
internal SpecialSymbols SpecialSymbols { get; init; } = default!;
internal ITranspilationOptions TranspilationOptions { get; init; } = default!;
internal ITypedSignalRTranspilationOptions Options { get; init; } = default!;
}
10 changes: 5 additions & 5 deletions src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public constructor(
this.Write(this.ToStringHelper.ToStringWithCulture(hubType.Name));
this.Write(" {\r\n\r\n public constructor(private connection: HubConnection) {\r\n }\r\n");
foreach(var method in hubType.Methods) {
this.Write(this.ToStringHelper.ToStringWithCulture(method.CreateMethodString(SpecialSymbols, TranspilationOptions)));
this.Write(this.ToStringHelper.ToStringWithCulture(method.CreateMethodString(SpecialSymbols, Options)));
this.Write("\r\n");
}
this.Write("}\r\n\r\n");
Expand All @@ -136,25 +136,25 @@ public constructor(
this.Write("): Disposable => {\r\n\r\n");
foreach(var method in receiverType.Methods) {
this.Write(" const __");
this.Write(this.ToStringHelper.ToStringWithCulture(method.Name.Format(TranspilationOptions.NamingStyle)));
this.Write(this.ToStringHelper.ToStringWithCulture(method.Name.Format(Options.NamingStyle)));
this.Write(" = ");
this.Write(this.ToStringHelper.ToStringWithCulture(method.WrapLambdaExpressionSyntax(TranspilationOptions)));
this.Write(this.ToStringHelper.ToStringWithCulture(method.WrapLambdaExpressionSyntax(Options)));
this.Write(";\r\n");
}
this.Write("\r\n");
foreach(var method in receiverType.Methods) {
this.Write(" connection.on(\"");
this.Write(this.ToStringHelper.ToStringWithCulture(method.Name));
this.Write("\", __");
this.Write(this.ToStringHelper.ToStringWithCulture(method.Name.Format(TranspilationOptions.NamingStyle)));
this.Write(this.ToStringHelper.ToStringWithCulture(method.Name.Format(Options.NamingStyle)));
this.Write(");\r\n");
}
this.Write("\r\n const methodList: ReceiverMethod[] = [\r\n");
for(int i = 0; i < receiverType.Methods.Count; i++) {
this.Write(" { methodName: \"");
this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].Name));
this.Write("\", method: __");
this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].Name.Format(TranspilationOptions.NamingStyle)));
this.Write(this.ToStringHelper.ToStringWithCulture(receiverType.Methods[i].Name.Format(Options.NamingStyle)));
this.Write(" }");
this.Write(this.ToStringHelper.ToStringWithCulture(i != receiverType.Methods.Count - 1 ? "," : ""));
this.Write("\r\n");
Expand Down
8 changes: 4 additions & 4 deletions src/TypedSignalR.Client.TypeScript/Templates/ApiTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class <#= hubType.Name #>_HubProxy implements <#= hubType.Name #> {
public constructor(private connection: HubConnection) {
}
<# foreach(var method in hubType.Methods) { #>
<#= method.CreateMethodString(SpecialSymbols, TranspilationOptions) #>
<#= method.CreateMethodString(SpecialSymbols, Options) #>
<# } #>
}

Expand All @@ -110,16 +110,16 @@ class <#= receiverType.Name #>_Binder implements ReceiverRegister<<#= receiverTy
public readonly register = (connection: HubConnection, receiver: <#= receiverType.Name #>): Disposable => {

<# foreach(var method in receiverType.Methods) { #>
const __<#= method.Name.Format(TranspilationOptions.NamingStyle) #> = <#= method.WrapLambdaExpressionSyntax(TranspilationOptions) #>;
const __<#= method.Name.Format(Options.NamingStyle) #> = <#= method.WrapLambdaExpressionSyntax(Options) #>;
<# } #>

<# foreach(var method in receiverType.Methods) { #>
connection.on("<#= method.Name #>", __<#= method.Name.Format(TranspilationOptions.NamingStyle) #>);
connection.on("<#= method.Name #>", __<#= method.Name.Format(Options.NamingStyle) #>);
<# } #>

const methodList: ReceiverMethod[] = [
<# for(int i = 0; i < receiverType.Methods.Count; i++) { #>
{ methodName: "<#= receiverType.Methods[i].Name #>", method: __<#= receiverType.Methods[i].Name.Format(TranspilationOptions.NamingStyle) #> }<#= i != receiverType.Methods.Count - 1 ? "," : "" #>
{ methodName: "<#= receiverType.Methods[i].Name #>", method: __<#= receiverType.Methods[i].Name.Format(Options.NamingStyle) #> }<#= i != receiverType.Methods.Count - 1 ? "," : "" #>
<# } #>
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using System.Linq;
using Microsoft.CodeAnalysis;
using Tapper;
using Tapper.TypeMappers;

namespace TypedSignalR.Client.TypeScript.Templates;

internal static class MethodSymbolExtensions
{
public static string WrapLambdaExpressionSyntax(this IMethodSymbol methodSymbol, ITranspilationOptions options)
public static string WrapLambdaExpressionSyntax(this IMethodSymbol methodSymbol, ITypedSignalRTranspilationOptions options)
{
if (methodSymbol.Parameters.Length == 0)
{
return $"() => receiver.{methodSymbol.Name.Format(options.NamingStyle)}()";
return $"() => receiver.{methodSymbol.Name.Format(options.MethodStyle)}()";
}

var parameters = ParametersToTypeArray(methodSymbol, options);
return $"(...args: {parameters}) => receiver.{methodSymbol.Name.Format(options.NamingStyle)}(...args)";
return $"(...args: {parameters}) => receiver.{methodSymbol.Name.Format(options.MethodStyle)}(...args)";
}

public static string CreateMethodString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
public static string CreateMethodString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var methodType = methodSymbol.SelectHubMethodType(specialSymbols);
return methodType switch
Expand All @@ -30,7 +29,7 @@ public static string CreateMethodString(this IMethodSymbol methodSymbol, Special
};
}

private static string ParametersToTypeScriptString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string ParametersToTypeScriptString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var parameters = methodSymbol.Parameters
.Where(x => !SymbolEqualityComparer.Default.Equals(x.Type, specialSymbols.CancellationTokenSymbol))
Expand All @@ -39,7 +38,7 @@ private static string ParametersToTypeScriptString(this IMethodSymbol methodSymb
return string.Join(", ", parameters);
}

private static string ParametersToTypeScriptArgumentString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string ParametersToTypeScriptArgumentString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var args = methodSymbol.Parameters
.Where(x => !SymbolEqualityComparer.Default.Equals(x.Type, specialSymbols.CancellationTokenSymbol))
Expand All @@ -50,7 +49,7 @@ private static string ParametersToTypeScriptArgumentString(this IMethodSymbol me
: string.Empty;
}

private static string ReturnTypeToTypeScriptString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string ReturnTypeToTypeScriptString(this IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var returnType = methodSymbol.ReturnType;
// for sever-to-client streaming
Expand Down Expand Up @@ -79,15 +78,15 @@ private static string ReturnTypeToTypeScriptString(this IMethodSymbol methodSymb
return TypeMapper.MapTo(methodSymbol.ReturnType, options);
}

private static string ParametersToTypeArray(IMethodSymbol methodSymbol, ITranspilationOptions options)
private static string ParametersToTypeArray(IMethodSymbol methodSymbol, ITypedSignalRTranspilationOptions options)
{
var parameters = methodSymbol.Parameters.Select(x => TypeMapper.MapTo(x.Type, options));
return $"[{string.Join(", ", parameters)}]";
}

private static string CreateUnaryMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string CreateUnaryMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var name = methodSymbol.Name.Format(options.NamingStyle);
var name = methodSymbol.Name.Format(options.MethodStyle);
var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options);
var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options);
var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options);
Expand All @@ -98,9 +97,9 @@ private static string CreateUnaryMethodString(IMethodSymbol methodSymbol, Specia
}}";
}

private static string CreateServerToClientStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string CreateServerToClientStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var name = methodSymbol.Name.Format(options.NamingStyle);
var name = methodSymbol.Name.Format(options.MethodStyle);
var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options);
var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options);
var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options);
Expand All @@ -111,9 +110,9 @@ private static string CreateServerToClientStreamingMethodString(IMethodSymbol me
}}";
}

private static string CreateClientToServerStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITranspilationOptions options)
private static string CreateClientToServerStreamingMethodString(IMethodSymbol methodSymbol, SpecialSymbols specialSymbols, ITypedSignalRTranspilationOptions options)
{
var name = methodSymbol.Name.Format(options.NamingStyle);
var name = methodSymbol.Name.Format(options.MethodStyle);
var parameters = methodSymbol.ParametersToTypeScriptString(specialSymbols, options);
var returnType = methodSymbol.ReturnTypeToTypeScriptString(specialSymbols, options);
var args = methodSymbol.ParametersToTypeScriptArgumentString(specialSymbols, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace TypedSignalR.Client.TypeScript;

public class TypedSignalRCodeGenerator
{
private readonly ITranspilationOptions _options;
private readonly ITypedSignalRTranspilationOptions _options;
private readonly Compilation _compilation;

private readonly ILogger _logger;

public TypedSignalRCodeGenerator(
Compilation compilation,
ITranspilationOptions options,
ITypedSignalRTranspilationOptions options,
ILogger logger)
{
_compilation = compilation;
Expand Down
Loading

0 comments on commit 6651cb1

Please sign in to comment.