From ad59f2dea0c85945e79dc2960774af1e14cbcb9f Mon Sep 17 00:00:00 2001 From: nenoNaninu Date: Fri, 3 Nov 2023 01:46:43 +0900 Subject: [PATCH 1/3] support const field --- .../SourceGenerator.cs | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs index 294b48e..803d614 100644 --- a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs +++ b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs @@ -81,13 +81,7 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte return default; } - if (arguments[0].Expression.Kind() != SyntaxKind.StringLiteralExpression) - { - return default; - } - - var literal = arguments[0].Expression as LiteralExpressionSyntax; - var path = literal?.Token.ValueText; + var path = GetPath(context, arguments[0].Expression); if (string.IsNullOrEmpty(path)) { @@ -104,6 +98,26 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte return new SourceSymbol(methodSymbol, target.GetLocation(), path!); } + private static string? GetPath(GeneratorSyntaxContext context, ExpressionSyntax syntax) + { + var symbol = context.SemanticModel.GetSymbolInfo(syntax).Symbol; + + if (symbol is IFieldSymbol field + && field.IsConst + && field.ConstantValue is string value) + { + return value; + } + + if (syntax.Kind() == SyntaxKind.StringLiteralExpression + && syntax is LiteralExpressionSyntax literal) + { + return literal.Token.ValueText; + } + + return null; + } + private static ValidatedSourceSymbol ValidateMapHubMethodSymbol((SourceSymbol, SpecialSymbols) pair, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); From d5ac1112405815bb8f46cf9b3880877391a2d09a Mon Sep 17 00:00:00 2001 From: nenoNaninu Date: Fri, 3 Nov 2023 02:07:55 +0900 Subject: [PATCH 2/3] refactor --- .../SourceGenerator.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs index 803d614..cc44b8d 100644 --- a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs +++ b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs @@ -100,6 +100,12 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte private static string? GetPath(GeneratorSyntaxContext context, ExpressionSyntax syntax) { + if (syntax.Kind() == SyntaxKind.StringLiteralExpression + && syntax is LiteralExpressionSyntax literal) + { + return literal.Token.ValueText; + } + var symbol = context.SemanticModel.GetSymbolInfo(syntax).Symbol; if (symbol is IFieldSymbol field @@ -109,12 +115,6 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte return value; } - if (syntax.Kind() == SyntaxKind.StringLiteralExpression - && syntax is LiteralExpressionSyntax literal) - { - return literal.Token.ValueText; - } - return null; } From 539e25b4ab4d1c1f8a03de133930975c35fa31f4 Mon Sep 17 00:00:00 2001 From: nenoNaninu Date: Fri, 3 Nov 2023 11:54:56 +0900 Subject: [PATCH 3/3] support local constant --- .../SourceGenerator.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs index cc44b8d..34d33a2 100644 --- a/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs +++ b/src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs @@ -110,9 +110,16 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte if (symbol is IFieldSymbol field && field.IsConst - && field.ConstantValue is string value) + && field.ConstantValue is string fieldValue) { - return value; + return fieldValue; + } + + if (symbol is ILocalSymbol local + && local.IsConst + && local.ConstantValue is string localValue) + { + return localValue; } return null;