Skip to content

Commit

Permalink
Merge pull request #46 from nenoNaninu/support_const_field
Browse files Browse the repository at this point in the history
Support const string
  • Loading branch information
nenoNaninu authored Nov 3, 2023
2 parents 10317b0 + 539e25b commit 190e3c7
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/TypedSignalR.Client.DevTools.Specification/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -104,6 +98,33 @@ private static SourceSymbol TransformToSourceSymbol(GeneratorSyntaxContext conte
return new SourceSymbol(methodSymbol, target.GetLocation(), path!);
}

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
&& field.IsConst
&& field.ConstantValue is string fieldValue)
{
return fieldValue;
}

if (symbol is ILocalSymbol local
&& local.IsConst
&& local.ConstantValue is string localValue)
{
return localValue;
}

return null;
}

private static ValidatedSourceSymbol ValidateMapHubMethodSymbol((SourceSymbol, SpecialSymbols) pair, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down

0 comments on commit 190e3c7

Please sign in to comment.